Sha256: e71419c4995addcd1ad153b9a6153ac1b6f31d229a295253c626a09d467c9cb3

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module ReverseAdoc
  module Converters
    class Pre < Base
      def convert(node, state = {})
        id = node['id']
        anchor = id ? "[[#{id}]]\n" : ""
        lang = language(node)
        content = treat_children(node, state)
        if lang
          "\n\n#{anchor}[source,#{lang}]\n----\n" << content.lines.to_a.join("") << "\n----\n\n"
        else
          "\n\n#{anchor}....\n" << content.lines.to_a.join("") << "\n....\n\n"
        end
      end

      private

      def treat(node, state)
        return "\n" if node.name == "br"

        prefix = postfix = "\n\n" if node.name == "p"

        "#{prefix}#{node.text}#{postfix}"
      end

      def language(node)
        lang = language_from_highlight_class(node)
        lang || language_from_confluence_class(node)
      end

      def language_from_highlight_class(node)
        node.parent['class'].to_s[/highlight-([a-zA-Z0-9]+)/, 1]
      end

      def language_from_confluence_class(node)
        node['class'].to_s[/brush:\s?(:?.*);/, 1]
      end
    end

    register :pre, Pre.new
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reverse_adoc-0.3.7 lib/reverse_adoc/converters/pre.rb
reverse_adoc-0.3.6 lib/reverse_adoc/converters/pre.rb