Class: BitClust::MarkdownOrchestrator

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

Overview

RD ツリー → Markdown ツリー変換のクロスファイル方針を束ねるオーケストレータ。

単一ファイル記法変換(RRDToMarkdown)はそのままに、include グラフの解析結果から 各ファイルへ次を適用する:

  • grouping include の prune(エンティティ取り込みは front matter 発見へ移行)
  • ファイル全体を包む版ゲートの解除(front matter の since/until へ移行)
  • front matter 注入(member: library/構造ゲート、library ルート: type/版ゲート)

スコープ(対象版範囲)はパラメータ。旧版サルベージ時は別スコープで再実行する。

Defined Under Namespace

Classes: Unit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_root, scope: IncludeGraph::Scope.new('3.0', '4.2')) ⇒ MarkdownOrchestrator

Returns a new instance of MarkdownOrchestrator.



25
26
27
28
29
30
31
32
33
# File 'lib/bitclust/markdown_orchestrator.rb', line 25

def initialize(src_root, scope: IncludeGraph::Scope.new('3.0', '4.2'))
  @graph = IncludeGraph.analyze(src_root)
  @scope = scope
  @extra = @graph.front_matter_map(scope)
  @graph.library_front_matter_map(scope).each do |path, fm|
    (@extra[path] ||= {}).merge!(fm)
  end
  @prune_sites = @graph.grouping_include_sites
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



23
24
25
# File 'lib/bitclust/markdown_orchestrator.rb', line 23

def graph
  @graph
end

#scopeObject (readonly)

Returns the value of attribute scope.



23
24
25
# File 'lib/bitclust/markdown_orchestrator.rb', line 23

def scope
  @scope
end

Instance Method Details

#convert(relpath, rrd) ⇒ Object

relpath の RD を新パイプライン形の Markdown へ変換する(分割なしファイル用)

Raises:

  • (ArgumentError)


99
100
101
102
103
# File 'lib/bitclust/markdown_orchestrator.rb', line 99

def convert(relpath, rrd)
  us = units(relpath, rrd)
  raise ArgumentError, "#{relpath} splits into #{us.size} files, use units" if us.size > 1
  convert_unit(us.first)
end

#convert?(relpath) ⇒ Boolean

変換対象か。LIBRARIES は front matter による発見に置き換わるため対象外

Returns:

  • (Boolean)


40
41
42
# File 'lib/bitclust/markdown_orchestrator.rb', line 40

def convert?(relpath)
  relpath != 'LIBRARIES'
end

#convert_unit(unit) ⇒ Object



94
95
96
# File 'lib/bitclust/markdown_orchestrator.rb', line 94

def convert_unit(unit)
  RRDToMarkdown.convert(unit.rrd, extra_front_matter: unit.front_matter)
end

#reduce(relpath, rrd) ⇒ Object

変換の rd 側到達点(prune + 全体ゲート解除 + 定数 H1 ゲート解決後の RD)と front matter。MarkdownToRRD.convert(convert(...)) はこの RD と一致する



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/bitclust/markdown_orchestrator.rb', line 107

def reduce(relpath, rrd)
  front_matter = (@extra[relpath] || {}).dup
  rrd = IncludePruner.prune(rrd, @prune_sites[relpath] || [])
  if (unwrapped = WholeFileGate.unwrap_for_scope(rrd, @scope))
    if unwrap_allowed?(front_matter, unwrapped[1])
      rrd = unwrapped[0]
      merge_gate(front_matter, unwrapped[1])
    elsif front_matter['type'] == 'library' &&
          (regated = WholeFileGate.(rrd))
      # 据え置きゲートの library ルート: メタデータ領域を独立ゲートに分離して
      # front matter 化できるようにする(本文はゲートのまま)
      rrd = regated
    end
  end
  rrd = EntitySplitter.resolve_header_gates(rrd, @scope)
  rrd = normalize_entity_h1(rrd)
  rrd = normalize_signature_spacing(rrd)
  rrd = RRDToMarkdown.normalize_dlist_colon_spacing(rrd)
  rrd = rrd.sub(/\A(?:[ \t]*\n)+/, '')   # 先頭空行(ゲート解決の残り)を除去
  [normalize_header_regions(rrd), front_matter]
end

#units(relpath, rrd) ⇒ Object

relpath の RD を出力単位の列に還元する。 ヘッダ関係(include/extend/alias)を持つマルチエンティティファイルは エンティティ単位に分割する(関係の front matter 一元化のため)。 関係を持たない束ね(Errno 族等)と lib+単一エンティティ兼用ファイルは 1 単位のまま。ライブラリファイルの分割ではエンティティを / 配下に 置き、library と版ゲートを注入する



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bitclust/markdown_orchestrator.rb', line 55

def units(relpath, rrd)
  reduced, front_matter = reduce(relpath, rrd)
  # スコープ外ファイル(front matter 注入なし)は分割しない。
  # サルベージは別スコープの再実行で扱う
  segments = front_matter.empty? ? nil : split_segments(reduced)
  return [Unit.new(output_path(relpath, front_matter), reduced, front_matter)] unless segments

  library = front_matter['type'] == 'library'
  dir = library ? relpath.sub(/\.rd\z/, '') : File.dirname(relpath)
  entity_fm =
    if library
      fm = { 'library' => relpath.sub(/\.rd\z/, '') } #: IncludeGraph::front_matter
      %w[since until].each { |k| fm[k] = front_matter[k] if front_matter[k] }
      fm
    else
      front_matter
    end

  source_dir = File.dirname(relpath)
  units = segments.map do |name, text|
    next Unit.new(output_path(relpath, front_matter), text, front_matter) if name.nil?   # 概要部

    seg_fm = entity_fm.dup
    if (unwrapped = WholeFileGate.unwrap_for_scope(text, @scope))
      # エンティティセグメントのゲートは自身の存在ゲート → 常に front matter へ
      text = unwrapped[0]
      merge_gate(seg_fm, unwrapped[1])
    end
    text = rewrite_includes(text, source_dir, dir)
    filename = "#{EntitySplitter.entity_filename(name)}.md"
    Unit.new(dir == '.' ? filename : File.join(dir, filename), text, seg_fm)
  end
  if library && segments.none? { |name, _| name.nil? }
    # 概要部が無くてもライブラリ自体が発見から消えないよう front matter のみ合成
    units.unshift(Unit.new(output_path(relpath, front_matter), '', front_matter))
  end
  units
end

#warningsObject



35
36
37
# File 'lib/bitclust/markdown_orchestrator.rb', line 35

def warnings
  @graph.warnings
end