Class: BitClust::MarkdownExporter

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

Overview

statichtml の --markdown-output 用に、HTML の各ページと対になる Markdown を エントリの前処理済みソース(Markdown ツリー由来の md)から組み立てる。 参照記法(単一ブラケットの [m:...] 等と、参照 scheme を宛先にした ラベル付きリンク)は相対 .md リンクへ解決する。解決できない参照 (未対応の種別・不正な指定)はそのまま残し、エラーにしない。 コードスパンとコードフェンスの中は変換しない。

Constant Summary collapse

CLASS_SECTIONS =

クラスページの メソッド種別 → セクション見出し(doctree の md と同じ名称)

[
  ["Class Methods",              :public,    :singleton_method],
  ["Instance Methods",           :public,    :instance_method],
  ["Private Class Methods",      :private,   :singleton_method],
  ["Private Instance Methods",   :private,   :instance_method],
  ["Protected Instance Methods", :protected, :instance_method],
  ["Module Functions",           :public,    :module_function],
  ["Constants",                  :public,    :constant],
  ["Special Variables",          :public,    :special_variable],
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(urlmapper, html_suffix) ⇒ MarkdownExporter

Returns a new instance of MarkdownExporter.



27
28
29
30
# File 'lib/bitclust/markdown_exporter.rb', line 27

def initialize(urlmapper, html_suffix)
  @urlmapper = urlmapper
  @html_suffix = html_suffix
end

Instance Method Details

#class_page(entry) ⇒ Object

クラスページは HTML と同じく本文+メソッドリンク一覧(本文は載せない)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bitclust/markdown_exporter.rb', line 37

def class_page(entry)
  out = +""
  CLASS_SECTIONS.each do |section, visibility, type|
    methods = entry.entries.select { |m|
      !m.undefined? && m.type == type && m.visibility == visibility
    }
    next if methods.empty?
    out << "\n## #{section}\n\n"
    methods.each do |m|
      m.names.each do |name|
        spec = m.klass.name + m.typemark + name
        url = ref_url('m', spec)
        out << (url ? "- [#{escape_label(name)}](#{url})\n" : "- #{escape_label(name)}\n")
      end
    end
  end
  page(class_heading(entry), entry.source, out)
end

#convert_text(str) ⇒ Object

本文中の参照をコードスパン・コードフェンスを避けて相対 .md リンクへ 変換する



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

def convert_text(str)
  out = +''
  fence = nil
  str.each_line do |line|
    if fence
      out << line
      stripped = line.strip
      fence = nil if stripped.match?(/\A`+\z/) && stripped.length >= (fence || 0)
    elsif (m = /\A {0,3}(`{3,})/.match(line))
      fence = (m[1] || raise).length
      out << line
    else
      out << convert_line(line)
    end
  end
  out
end

#doc_page(entry) ⇒ Object



65
66
67
# File 'lib/bitclust/markdown_exporter.rb', line 65

def doc_page(entry)
  page("# #{entry.title}", entry.source)
end

#function_page(entry) ⇒ Object



69
70
71
# File 'lib/bitclust/markdown_exporter.rb', line 69

def function_page(entry)
  page("# #{entry.name}", "### #{entry.header}\n#{entry.source}")
end

#library_page(entry) ⇒ Object



32
33
34
# File 'lib/bitclust/markdown_exporter.rb', line 32

def library_page(entry)
  page("# library #{entry.name}", entry.source)
end

#method_page(method_name, entries) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/bitclust/markdown_exporter.rb', line 56

def method_page(method_name, entries)
  out = +"# #{method_name.sub('.#', '?.')}\n"
  entries.each do |m|
    body = m.source.strip
    out << "\n" << convert_text(body) << "\n" unless body.empty?
  end
  out
end