Class: BitClust::MarkdownBridge
- Defined in:
- lib/bitclust/markdown_bridge.rb
Overview
md ツリー → 旧形式の rd ツリー(LIBRARIES + .rd)を生成するブリッジ。
既存の update 機構(LIBRARIES → RRDParser/Preprocessor)を一切変更せずに md ツリーから DB を組み立てるための変換層。MarkdownTree の発見結果から:
- LIBRARIES を再生成(ライブラリの since/until は #@ ゲートとして再具現化)
- ライブラリ .rd = 変換済み本文 + メンバーへの #@include を再生成
- メンバー = 変換済み本文を front matter の since/until で #@ ラップ (版指定ビルドで構造ゲートが効くように)
- 断片 = そのまま変換 出力ファイル名は「md 名から .md を剥いだもの」(ライブラリのみ .rd 付き)に 統一し、本文中の #@include ターゲットも emit 名へ書き換える (Preprocessor はリテラルパスで解決するため)。
Constant Summary collapse
- INCLUDE_RE =
/^(\#@include\s*\()(.*?)(\))/
Instance Attribute Summary collapse
-
#source_map ⇒ Object
readonly
出力相対パス(emit 名) => 入力 md 相対パス。 source_location を manual/ の md へ再マップするために使う.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Class Method Summary collapse
- .build(md_root, out_root) ⇒ Object
-
.build_doc(md_doc_root, out_doc_root) ⇒ Object
doc(散文ページ)の md ツリー → 旧形式の doc/.rd。 - クロスツリー include(api 断片の transclude)は旧レイアウト (../api/src/)へ戻す(ブリッジの api ツリーは api/src に置かれるため) - doc 内ローカル include から参照される断片(spec/regexp19 等)は 拡張子なしで emit する(copy_doc は **/.rd だけをページとして読む) 戻り値は source_map(出力相対パス => 入力 md 相対パス).
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(md_root, out_root) ⇒ MarkdownBridge
constructor
A new instance of MarkdownBridge.
Constructor Details
#initialize(md_root, out_root) ⇒ MarkdownBridge
Returns a new instance of MarkdownBridge.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bitclust/markdown_bridge.rb', line 63 def initialize(md_root, out_root) @md_root = md_root @out_root = out_root @tree = MarkdownTree.scan(md_root) @warnings = @tree.warnings.dup @source_map = {} # 名前はパス由来とは限らない(ファイル名衝突回避で改名された # rdoc/rdoc.lib.md は front matter の name: が正)ので、パスから引く @lib_by_path = @tree.libraries.to_h { |name, lib| [lib[:path], name] } end |
Instance Attribute Details
#source_map ⇒ Object (readonly)
出力相対パス(emit 名) => 入力 md 相対パス。 source_location を manual/ の md へ再マップするために使う
61 62 63 |
# File 'lib/bitclust/markdown_bridge.rb', line 61 def source_map @source_map end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
57 58 59 |
# File 'lib/bitclust/markdown_bridge.rb', line 57 def warnings @warnings end |
Class Method Details
.build(md_root, out_root) ⇒ Object
25 26 27 |
# File 'lib/bitclust/markdown_bridge.rb', line 25 def self.build(md_root, out_root) new(md_root, out_root).build end |
.build_doc(md_doc_root, out_doc_root) ⇒ Object
doc(散文ページ)の md ツリー → 旧形式の doc/*.rd。
- クロスツリー include(api 断片の transclude)は旧レイアウト (../api/src/)へ戻す(ブリッジの api ツリーは api/src に置かれるため)
- doc 内ローカル include から参照される断片(spec/regexp19 等)は 拡張子なしで emit する(copy_doc は **/*.rd だけをページとして読む) 戻り値は source_map(出力相対パス => 入力 md 相対パス)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bitclust/markdown_bridge.rb', line 35 def self.build_doc(md_doc_root, out_doc_root) require 'bitclust/markdown_to_rrd' files = Dir.glob('**/*.md', base: md_doc_root).sort referenced = files.flat_map { |f| base = File.dirname(f) File.read(File.join(md_doc_root, f)).scan(/^\#@include\((?!(?:\.\.\/)+api\/)(.*?)\)/) .map { |t| File.(base == '.' ? (t[0] || raise) : File.join(base, t[0] || raise), '/') .delete_prefix('/') } }.to_h { |t| [t, true] } files.to_h do |f| rrd = MarkdownToRRD.convert(File.read(File.join(md_doc_root, f))) rrd = rrd.gsub(%r{^(\#@include\((?:\.\./)+api/)(?!src/)}, '\1src/') name = f.sub(/\.md\z/, '') rel = referenced[name] ? name : "#{name}.rd" full = File.join(out_doc_root, rel) FileUtils.mkdir_p(File.dirname(full)) File.write(full, rrd) [rel, f] end end |
Instance Method Details
#build ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bitclust/markdown_bridge.rb', line 74 def build files = Dir.glob('**/*.md', base: @md_root).sort @dirs = files.flat_map { |f| parts = File.dirname(f).split('/') (1..parts.size).map { |n| parts.first(n).join('/') } }.uniq - ['.'] emitted = files.to_h { |f| [f, emit_name(f)] } @source_map = emitted.invert files.each do |f| rrd = MarkdownToRRD.convert(File.read(File.join(@md_root, f))) rrd = rewrite_includes(rrd, f, emitted) if (libname = @lib_by_path[f]) rrd << member_includes(libname, emitted) elsif (entity = @tree.entities[f]) rrd = wrap_gate(rrd, entity) end write(emitted[f], rrd) end write('LIBRARIES', libraries_manifest) self end |