Sha256: d016c482efca066d24536f8b80b378f88bca7235c3f1f0ae941f4d52253aed40

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

module ReverseAdoc
  module Converters
    class Code < Base
      def to_coradoc(node, state = {})
        content = treat_children_coradoc(node, state)

        if Coradoc::Generator.gen_adoc(content).strip.empty?
          return ""
        end

        if node_has_ancestor?(node, ["code", "tt", "kbd", "samp", "var"])
          return content
        end

        node.text =~ /^(\s+)/
        leading_whitespace = $1
        has_leading_whitespace = !leading_whitespace.nil?

        if has_leading_whitespace
          first_text = node.at_xpath("./text()[1]")
          first_text.replace(first_text.text.lstrip)
          leading_whitespace = " "
        end

        node.text =~ /(\s+)$/
        trailing_whitespace = $1
        has_trailing_whitespace = !trailing_whitespace.nil?

        if has_trailing_whitespace
          last_text = node.at_xpath("./text()[last()]")
          last_text.replace(last_text.text.rstrip)
          trailing_whitespace = " "
        end

        u_before = unconstrained_before?(node)
        u_after = unconstrained_after?(node)
        u = !((!u_before || has_leading_whitespace) && (!u_after || has_trailing_whitespace))
        e = Coradoc::Element::Inline::Monospace.new(
          Coradoc::Element::TextElement.new(content), u
        )

        [leading_whitespace, e, trailing_whitespace]
      end

      def convert(node, state = {})
        Coradoc::Generator.gen_adoc(to_coradoc(node, state))
      end
    end

    register :code, Code.new
    register :tt, Code.new
    register :kbd, Code.new
    register :samp, Code.new
    register :var, Code.new
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reverse_adoc-1.0.1 lib/reverse_adoc/converters/code.rb
reverse_adoc-1.0.0 lib/reverse_adoc/converters/code.rb