Class: BitClust::RRDToMarkdown

Inherits:
Object
  • Object
show all
Defined in:
lib/bitclust/rrd_to_markdown.rb

Constant Summary collapse

EXTRA_FRONT_MATTER_KEYS =

ファイル単体からは決められない front matter(library 所属・構造 since/until)を 注入するための口。include グラフを解析したオーケストレータが値を計算して渡す。 name はファイル名の大文字小文字衝突回避で改名されたライブラリファイルの名前保持用

%w[type name library since until].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rrd, extra_front_matter: {}, capi: false) ⇒ RRDToMarkdown

capi: C API リファレンス(refm/capi)モード。シグネチャは C の 「--- <型付きシグネチャ>」で、def 等のキーワードを付けずに ### へ変換する



66
67
68
69
70
71
72
73
74
# File 'lib/bitclust/rrd_to_markdown.rb', line 66

def initialize(rrd, extra_front_matter: {}, capi: false)
  @src = rrd
  @capi = capi
  @extra_front_matter = extra_front_matter.transform_keys(&:to_s)
  unknown = @extra_front_matter.keys - EXTRA_FRONT_MATTER_KEYS
  unless unknown.empty?
    raise ArgumentError, "unknown extra front matter keys: #{unknown.join(', ')}"
  end
end

Class Method Details

.convert(rrd, extra_front_matter: {}, capi: false) ⇒ Object



5
6
7
8
# File 'lib/bitclust/rrd_to_markdown.rb', line 5

def self.convert(rrd, extra_front_matter: {}, capi: false)
  rrd = normalize_dlist_colon_spacing(rrd)
  new(rrd, extra_front_matter: extra_front_matter, capi: capi).convert
end

.normalize_dlist_colon_spacing(rrd) ⇒ Object

RDCompiler の dlist 継続は /\A:/(スペース不要)で dt になる (spec/operator の「:再定義できない演算子」)。dlist 文脈にある 「:term」だけを正規形「: term」に直す。段落継続の「:SYMBOL」行 (openssl/ASN1 等)は RDCompiler では段落テキストなので触らない。 文脈判定は RDCompiler のディスパッチを行単位で再現する



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bitclust/rrd_to_markdown.rb', line 15

def self.normalize_dlist_colon_spacing(rrd)
  state = :none      # :none | :para | :dlist
  in_code = nil      # コードブロックの終端パターン
  rrd.lines.map { |l|
    if in_code
      in_code = nil if l =~ in_code
      next l
    end
    case l
    when /\A\#@samplecode/
      in_code = /\A\#@end/
      l
    when /\A\/\/emlist.*\{/
      in_code = %r<\A//\}>
      state = :none unless state == :dlist   # dd 内の emlist は dlist 継続
      l
    when /\A\#@/
      l                                      # ディレクティブは文脈に透明
    when /\A:\s/
      state = :dlist unless state == :para   # 段落継続中の「: 」行は段落
      l
    when /\A:(?=\S)/
      if state == :dlist
        ": #{l[1..]}"
      else
        state = :para                        # 段落(開始または継続)
        l
      end
    when /\A[ \t]*\n?\z/
      state = :none unless state == :dlist   # 空行: dd は空行を跨ぐ
      l
    when /\A[ \t]/
      state = :none unless state == :dlist   # dd 説明は継続、段落は終了
      l
    when /\A(?:---|=)/
      state = :none
      l
    else
      state = :para
      l
    end
  }.join
end

Instance Method Details

#convertObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bitclust/rrd_to_markdown.rb', line 76

def convert
  @lines = @src.lines
  @out = []
  @index = 0
  @front_matter = {}
  @current_section = nil
  # front matter はファイル単位。複数エンティティを含むファイルでは
  # include/extend/alias の帰属が曖昧になるため、単一エンティティ時のみ front matter 化する
  # (分割前の安全策。分割後は全ファイルが単一エンティティになる)。
  @single_entity = @lines.count { |l| l =~ /\A= / } == 1

  
  process_body
  @front_matter.merge!(@extra_front_matter)
  emit_front_matter + @out.join
end