File: a.rb

package info (click to toggle)
ruby-reverse-markdown 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 440 kB
  • sloc: ruby: 1,452; makefile: 4
file content (27 lines) | stat: -rw-r--r-- 579 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module ReverseMarkdown
  module Converters
    class A < Base
      def convert(node, state = {})
        name  = treat_children(node, state)
        href  = node['href']
        title = extract_title(node)

        if href.to_s.empty? || name.empty?
          name
        else
          link = "[#{name}](#{href}#{title})"
          link.prepend(' ') if prepend_space?(node)
          link
        end
      end

      private

      def prepend_space?(node)
        node.at_xpath("preceding::text()[1]").to_s.end_with?('!')
      end
    end

    register :a, A.new
  end
end