Module: Ibex::Codegen::RailroadDocumentation
- Included in:
- Railroad
- Defined in:
- lib/ibex/codegen/railroad_documentation.rb,
sig/ibex/codegen/railroad_documentation.rbs
Overview
Documentation layout helpers mixed into the railroad renderer singleton.
Constant Summary collapse
- DOCUMENTATION_COLUMNS =
72- DOCUMENTATION_LINE_HEIGHT =
18- DOCUMENTATION_GAP =
8
Instance Method Summary collapse
- #append_rule_documentation(lines, symbol) ⇒ void
- #documentation_lines(documentation) ⇒ Array[String]
- #escape(value) ⇒ String
- #section_header_height(symbol) ⇒ Integer
- #track(from, to) ⇒ String
Instance Method Details
#append_rule_documentation(lines, symbol) ⇒ void
This method returns an undefined value.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ibex/codegen/railroad_documentation.rb', line 14 def append_rule_documentation(lines, symbol) # @type self: singleton(Railroad) documentation = symbol.documentation return unless documentation lines << " <desc>#{escape(documentation)}</desc>" documentation_lines(documentation).each_with_index do |line, index| y = Railroad::SECTION_HEADER + DOCUMENTATION_GAP + (index * DOCUMENTATION_LINE_HEIGHT) lines << %( <text class="rule-documentation" x="#{Railroad::PAGE_PADDING}" y="#{y}">#{escape(line)}</text>) end end |
#documentation_lines(documentation) ⇒ Array[String]
36 37 38 39 40 41 42 43 |
# File 'lib/ibex/codegen/railroad_documentation.rb', line 36 def documentation_lines(documentation) return [] unless documentation documentation.split("\n", -1).flat_map do |line| characters = line.each_char.to_a characters.empty? ? [""] : characters.each_slice(DOCUMENTATION_COLUMNS).map(&:join) end end |
#escape(value) ⇒ String
46 47 48 49 50 51 |
# File 'lib/ibex/codegen/railroad_documentation.rb', line 46 def escape(value) xml = value.to_s.encode(Encoding::UTF_8, invalid: :replace, undef: :replace) .gsub(/[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/u, "\uFFFD") xml.gsub("&", "&").gsub("<", "<").gsub(">", ">") .gsub('"', """).gsub("'", "'") end |
#section_header_height(symbol) ⇒ Integer
27 28 29 30 31 32 33 |
# File 'lib/ibex/codegen/railroad_documentation.rb', line 27 def section_header_height(symbol) # @type self: singleton(Railroad) lines = documentation_lines(symbol.documentation) return Railroad::SECTION_HEADER if lines.empty? Railroad::SECTION_HEADER + DOCUMENTATION_GAP + (lines.length * DOCUMENTATION_LINE_HEIGHT) end |
#track(from, to) ⇒ String
54 55 56 |
# File 'lib/ibex/codegen/railroad_documentation.rb', line 54 def track(from, to) %(<line class="track" x1="#{from}" y1="0" x2="#{to}" y2="0"/>) end |