pmd/docs/_plugins/tocmaker_block.rb

61 lines
1.4 KiB
Ruby
Raw Normal View History

2018-08-18 16:44:45 +02:00
# Generates a table of contents based on markdown headers in the body
#
2018-08-18 20:30:19 +02:00
# The block has 2 optional args:
# * A variable name. If provided, the toc will only be generated if the var is true
# * An integer, describing the maximum depth at which headers are added to the toc
2018-08-18 16:44:45 +02:00
class TocMakerBlock < Liquid::Block
def initialize(tag_name, arg, tokens)
super
2018-08-18 20:30:19 +02:00
condition, depth = arg.split
@max_depth = depth.to_s.empty? ? 100 : depth.to_i
2018-08-18 22:03:13 +02:00
@condition_var = condition.strip unless condition.to_s.empty?
2018-08-18 20:30:19 +02:00
2018-08-18 16:44:45 +02:00
@body = tokens
end
def to_internal_link(header)
url = header.downcase.gsub(/\s+/, "-")
"[#{header}](##{url})"
end
def render(context)
contents = @body.render(context)
2018-08-18 20:30:19 +02:00
if @condition_var && !context[@condition_var]
# If the condition is false, the toc is not generated
return contents
end
2018-08-18 16:44:45 +02:00
headers = contents.lines.map {|l|
if /^(#+)\s+(\S.*)$/ =~ l
[$1.length, $2]
end
}.compact
2018-08-18 20:30:19 +02:00
min_indent = headers.map {|t| t[0]}.min
2018-08-18 16:44:45 +02:00
headers = headers.map {|t|
actual_depth = t[0] - min_indent
if actual_depth < @max_depth then
indent = " " * actual_depth
"#{indent}* #{to_internal_link(t[1])}"
end
}.compact
headers.unshift("### Table Of Contents\n")
headers.join("\n") + contents
end
end
2018-08-18 17:39:29 +02:00
Liquid::Template.register_tag('tocmaker', TocMakerBlock)