user guide: generate section numbers
This commit is contained in:
parent
812b1cbe9c
commit
bbf2319da6
@ -18,9 +18,11 @@ end
|
|||||||
class Generator
|
class Generator
|
||||||
class Section
|
class Section
|
||||||
attr_reader :name
|
attr_reader :name
|
||||||
|
attr_reader :new_page
|
||||||
attr_reader :contents
|
attr_reader :contents
|
||||||
def initialize(name)
|
def initialize(name, new_page)
|
||||||
@name = name
|
@name = name
|
||||||
|
@new_page = new_page
|
||||||
@contents = ""
|
@contents = ""
|
||||||
end
|
end
|
||||||
def append(contents)
|
def append(contents)
|
||||||
@ -29,12 +31,21 @@ class Generator
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize(input, output_file, multi_file)
|
def initialize(input, output_file, multi_file)
|
||||||
@sections = [Section.new("index")]
|
@sections = [Section.new("index", false)]
|
||||||
|
@current_section_number = [0]
|
||||||
@lines = input.lines
|
@lines = input.lines
|
||||||
while @lines.size > 0
|
while @lines.size > 0
|
||||||
line = @lines.slice!(0)
|
line = @lines.slice!(0)
|
||||||
if line =~ /^```(.*)$/
|
if line =~ /^```(.*)$/
|
||||||
@sections.last.append(gather_code_section($1))
|
@sections.last.append(gather_code_section($1))
|
||||||
|
elsif line =~ /^(#+)(>)?\s*(.*)$/
|
||||||
|
level_text, new_page_text, title_text = $1, $2, $3
|
||||||
|
level = $1.size
|
||||||
|
new_page = !new_page_text.nil?
|
||||||
|
section_number = get_next_section_number(level)
|
||||||
|
section_title = "#{section_number} #{title_text}"
|
||||||
|
@sections << Section.new(section_title, new_page)
|
||||||
|
@sections.last.append("#{level_text} #{section_title}")
|
||||||
else
|
else
|
||||||
@sections.last.append(line)
|
@sections.last.append(line)
|
||||||
end
|
end
|
||||||
@ -42,7 +53,9 @@ class Generator
|
|||||||
|
|
||||||
renderer = Redcarpet::Render::HTML.new
|
renderer = Redcarpet::Render::HTML.new
|
||||||
markdown = Redcarpet::Markdown.new(renderer)
|
markdown = Redcarpet::Markdown.new(renderer)
|
||||||
content = markdown.render(@sections.last.contents)
|
content = @sections.map do |section|
|
||||||
|
markdown.render(section.contents)
|
||||||
|
end.join("\n")
|
||||||
|
|
||||||
template = File.read("rb/assets/user_guide.html.erb")
|
template = File.read("rb/assets/user_guide.html.erb")
|
||||||
erb = ERB.new(template, nil, "<>")
|
erb = ERB.new(template, nil, "<>")
|
||||||
@ -68,6 +81,18 @@ class Generator
|
|||||||
%[<div class="code">\n<pre>#{code}</pre>\n</div>\n]
|
%[<div class="code">\n<pre>#{code}</pre>\n</div>\n]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_next_section_number(level)
|
||||||
|
if @current_section_number.size == level - 1
|
||||||
|
@current_section_number << 1
|
||||||
|
elsif @current_section_number.size >= level
|
||||||
|
@current_section_number[level - 1] += 1
|
||||||
|
@current_section_number.slice!(level, @current_section_number.size)
|
||||||
|
else
|
||||||
|
raise "Section level change from #{@current_section_number.size} to #{level}"
|
||||||
|
end
|
||||||
|
@current_section_number.join(".")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
input = load_file("doc/user_guide.md")
|
input = load_file("doc/user_guide.md")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user