Sha256: adb6f9328ee457de743db16b4727f1472ff310f05fe5cbdf6741fa7efb552e3c

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module ReverseAdoc
  module Converters
    class Pre < Base
      def to_coradoc(node, state = {})
        id = node["id"]
        lang = language(node)
        content = treat_children(node, state)

        unless lang
          return Coradoc::Element::Block::Literal.new(
            nil,
            lines: content,
            id: id,
          )
        end

        Coradoc::Element::Block::SourceCode.new(
          nil,
          lines: content,
          lang: lang,
          id: id,
        )
      end

      def convert(node, state = {})
        Coradoc::Generator.gen_adoc(to_coradoc(node, state))
      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-1.0.1 lib/reverse_adoc/converters/pre.rb
reverse_adoc-1.0.0 lib/reverse_adoc/converters/pre.rb