Class: Wpconv::Filter::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/wpconv/filter/markdown.rb

Constant Summary collapse

BackslashEscapedCharacters =
%w(\\ ` * _ { } [ ] \( \) # + - . !)

Class Method Summary collapse

Class Method Details

.apply(source_content) ⇒ Object



10
11
12
13
# File 'lib/wpconv/filter/markdown.rb', line 10

def self.apply(source_content)
  escaped_source_content = escape_literal(source_content)
  convert_html_tags(escaped_source_content)
end

.convert_html_tags(escaped_source_content) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wpconv/filter/markdown.rb', line 24

def self.convert_html_tags(escaped_source_content)
  escaped_source_content.tap do |content|
    # Heading
    {
      'h1' => '#',
      'h2' => '##',
      'h3' => '###',
      'h4' => '####',
      'h5' => '#####',
      'h6' => '######'
    }.each do |tag, md|
      content.gsub!(/<#{tag}>(.+?)<\/#{tag}>/m) { "#{md} #{$1.gsub(/\n/, '')}" }  # remove LF in the header string
    end
  
    # List 定義リストは未サポート
    %w(ul ol).each do |tag|
      content.gsub!(/<\/?#{tag}>[\n]?/, '') # remove LF after the tag
    end
    content.gsub!(/[ \t]*?<li.*?>(.+?)<\/li>[ \t]*?/m) { "* #{$1.strip}" } # 数値リストも未対応
  
    # hr
    content.gsub!(/<hr\s?\/?>/, "\n---\n")
  
    # pre and code 
    content.gsub!(/<pre.*?><code.*?>(.*?)<\/code><\/pre>/m) { "#{decode_markup_symbol($1).gsub(/^/, '    ')}" }

    # code
    content.gsub!(/<code.*?>(.*?)<\/code>/m) { '`` ' + decode_markup_symbol($1) + ' ``' }
  
    # brockquote
    content.gsub!(/<blockquote>(.*?)<\/blockquote>/m) { "#{$1.gsub(/^/, '> ')}" }

    # em
    content.gsub!(/<em>(.*?)<\/em>/m) { "*#{$1}*" }
  
    # strong, b
    ["strong", "b"].each do |tag|
      content.gsub!(/<#{tag}>(.*?)<\/#{tag}>/m) { "**#{$1}**" }
    end
  
    # Link
    content.gsub!(/<a.*?href=("|')(.+?)("|').*?>(.+?)<\/a>/m) { "[#{$4}](#{$2})" }

    # Image
  
    # Table?
  end

end

.decode_markup_symbol(code) ⇒ Object



74
75
76
# File 'lib/wpconv/filter/markdown.rb', line 74

def self.decode_markup_symbol(code)
  code.gsub('&lt;', '<').gsub('&gt;', '>').gsub('&amp;', '&')
end

.escape_literal(source_content) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/wpconv/filter/markdown.rb', line 15

def self.escape_literal(source_content)
  html = Nokogiri::HTML(source_content)
  source_content.tap do |content|
    BackslashEscapedCharacters.each do |char|
      content.gsub!(char) { "\\#{char}" }
    end
  end
end