lib/ebnf/writer.rb in ebnf-2.3.3 vs lib/ebnf/writer.rb in ebnf-2.3.4

- old
+ new

@@ -131,11 +131,12 @@ @coder = HTMLEntities.new eruby = Erubis::Eruby.new(ERB_DESC) formatted_rules = rules.map do |rule| if rule.kind == :terminals || rule.kind == :pass OpenStruct.new(id: ("@#{rule.kind}"), - sym: nil, + class: :declaration, + sym: rule.kind, assign: nil, formatted: ( rule.kind == :terminals ? "<strong># Productions for terminals</strong>" : self.send(format_meth, rule.expr))) @@ -148,30 +149,32 @@ # Can only reasonably split apart alts self.send(format_meth, rule.expr, sep: "--rule-extensions--"). split(/\s*--rule-extensions--\s*/).each_with_index do |formatted, ndx| assign = case format when :ebnf - formatted.sub!(%r{\s*<code>\|</code>\s*}, '') - (ndx > 0 ? (rule.alt? ? '|' : '') : '::=') + formatted.sub!(%r{\s*<code[^>]*>\|</code>\s*}, '') + (ndx > 0 ? (rule.alt? ? '<code class="grammar-alt">|</code>' : '') : '::=') when :abnf - formatted.sub!(%r{\s*<code>/</code>\s*}, '') - (ndx > 0 ? '=/' : '=') + formatted.sub!(%r{\s*<code[^>]>/</code>\s*}, '') + (ndx > 0 ? '<code class="grammar-alt">=/</code>' : '=') else - formatted.sub!(%r{\s*<code>\|</code>\s*}, '') - (ndx > 0 ? (rule.alt? ? '|' : '') : '=') + formatted.sub!(%r{\s*<code[^>]>\|</code>\s*}, '') + (ndx > 0 ? (rule.alt? ? '<code class="grammar-alt">|</code>' : '') : '=') end lines << OpenStruct.new(id: ((ndx == 0 ? "[#{rule.id}]" : "") if rule.id), sym: (rule.sym if ndx == 0 || format == :abnf), + class: :production, assign: assign, formatted: formatted) end if format == :isoebnf lines << OpenStruct.new(assign: ';') end lines else OpenStruct.new(id: ("[#{rule.id}]" if rule.id), + class: :production, sym: rule.sym, assign: (format == :ebnf ? '::=' : '='), formatted: (formatted_expr + (format == :isoebnf ? ' ;' : ''))) end end @@ -349,12 +352,12 @@ raise RangeError, "cannot format #{string.inspect} as an EBNF String: #{c.inspect} is out of range" unless ISOEBNF::TERMINAL_CHARACTER.match?(c) end end - res = "#{quote}#{string}#{quote}" - @options[:html] ? @coder.encode(res) : res + res = @options[:html] ? %(<code class="grammar-literal">#{@coder.encode(string)}</code>) : string + res = "#{quote}#{res}#{quote}" end def escape_ebnf_hex(u) fmt = case u.ord when 0x00..0x20 then "#x%02X" @@ -706,20 +709,19 @@ # Possibly only a single character (no character?) chars.length == 1 ? chars.last.inspect : chars.unshift(:alt) end - ERB_DESC = %q( - <!-- Generated with ebnf version #{EBNF::VERSION}. See https://github.com/dryruby/ebnf. --> - <table class="grammar"> + ERB_DESC = %(<!-- Generated with ebnf version #{EBNF::VERSION}. See https://github.com/dryruby/ebnf. -->\n) + + %q(<table class="grammar"> <tbody id="grammar-productions" class="<%= @format %>"> <% for rule in @rules %> - <tr<%= %{ id="grammar-production-#{rule.sym}"} unless %w(=/ |).include?(rule.assign) || rule.sym.nil?%>> + <tr<%= %{ id="grammar-#{rule[:class]}-#{rule.sym}"} unless %w(=/ |).include?(rule.assign) || rule.sym.nil?%>> <% if rule.id %> <td<%= " colspan=2" unless rule.sym %>><%= rule.id %></td> <% end %> <% if rule.sym %> - <td><code><%== rule.sym %></code></td> + <td><code><%== (rule.sym unless rule.class == :declaration) %></code></td> <% end %> <td><%= rule.assign %></td> <td><%= rule.formatted %></td> </tr> <% end %>