mato 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd113838ab44d0b792e313dbbd87ea0f15aed0fe
4
- data.tar.gz: 74113f53d71ab9114cee1d316d0500bafdf24f8e
3
+ metadata.gz: 2a90e7f93e54d37654d73461b98bb894e5ac5ba2
4
+ data.tar.gz: 79023df2717584da84317c669a0368674ed31bc8
5
5
  SHA512:
6
- metadata.gz: 434e444ea0a41f79b997bdb4cf10b555cfa5f4cd3da204e831bc8268ac21cdce2e7471031515b7710baeeff10a6780f2e4f7b78f12f3cb35c53f1d27e401f21a
7
- data.tar.gz: 14a67e122aafc2234b021343fb968b539691c69862adf89e4d471df3d29e8dae264f27c761f2d3d25ed48f460a4cdca2aad980c956dc7326763b9e35b194c33a
6
+ metadata.gz: 9c43c85c5b67a41a7c9a27434863920fd68365b0ab4facf4b053ab5174b5db5d931d249dac09f0f77cdb990273ccc7a923a0c1129e27353429d9341723fdc00b
7
+ data.tar.gz: 4f87d7a392f7d9b0a53453a92c1cef0f496d14f99c3520c34e89ae8bdb436ddda781d8f661dbc325cddc1e05ff38eddcbcdd441299fc5064f262c8673d1f470c
@@ -1,5 +1,12 @@
1
1
  # The revision history of Mato
2
2
 
3
+ ## v1.2.0 - 2017/08/28
4
+
5
+ https://github.com/bitjourney/mato/compare/v1.1.0...v1.2.0
6
+
7
+ * *Experimental*: `Mato::Processor#convert` to convert X-flavored markdown to CommonMark
8
+ * Currently only `flavor: :redcarpet` is supported
9
+
3
10
  ## v1.1.0 - 2017/08/24
4
11
 
5
12
  https://github.com/bitjourney/mato/compare/v1.0.3...v1.1.0
data/Gemfile CHANGED
@@ -9,7 +9,7 @@ gem "rouge", ">= 2.0"
9
9
  gem "sanitize", ">= 3.0"
10
10
 
11
11
  gem "bundler", ">= 1.14"
12
- gem "rake", ">= 10.0"
13
12
  gem "m"
14
13
  gem "minitest"
15
14
  gem "minitest-power_assert"
15
+ gem "rake", ">= 10.0"
@@ -4,6 +4,7 @@
4
4
  require_relative "./mato/version"
5
5
  require_relative "./mato/config"
6
6
  require_relative "./mato/processor"
7
+ require_relative "./mato/converter"
7
8
 
8
9
  # filter classes
9
10
  require_relative "./mato/html_filters/token_link"
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ # Convert X-flavored markdown to CommonMark
6
+ module Mato
7
+ class Converter
8
+
9
+ FLAVORES = Set.new([
10
+ :redcarpet, # legacy GFM uses it
11
+ ]).freeze
12
+
13
+ attr_reader :processor
14
+ attr_reader :content
15
+ attr_reader :flavor
16
+
17
+ # @return [Array<String>]
18
+ attr_reader :content_lines
19
+
20
+ def initialize(processor, content, flavor)
21
+ unless FLAVORES.include?(flavor)
22
+ raise "Unsupported flavor #{flavor.inspect}, it must be one of: #{FLAVORES.map(&:inspect).join(' ')}"
23
+ end
24
+
25
+ @processor = processor
26
+ @content = content
27
+ @content_lines = content.split(/\n/)
28
+ @flavor = flavor
29
+ end
30
+
31
+ def run
32
+ # @type [CommonMarker::Node]
33
+ document = processor.parse_markdown(content)
34
+
35
+ convert_headings!(document)
36
+
37
+ content_lines.join("\n").tap do |c|
38
+ # fixup newlines removed by String#split
39
+ content.scan(/\n+\z/) do |matched|
40
+ c << matched
41
+ end
42
+ end
43
+ end
44
+
45
+ def convert_headings!(document)
46
+ document.walk.select do |node|
47
+ node.type == :text &&
48
+ node.sourcepos[:start_column] == 1 &&
49
+ node.parent.type == :paragraph &&
50
+ node.parent.parent.type == :document
51
+ end.reverse.each do |node|
52
+ replacement = node.string_content.gsub(/\A(#+)(?=\S)/, '\1 ')
53
+
54
+ if node.string_content != replacement
55
+ pos = node.sourcepos
56
+ content_lines[pos[:start_line] - 1][(pos[:start_column] - 1)...pos[:end_column]] = replacement
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -8,19 +8,18 @@
8
8
  module Mato
9
9
  module HtmlFilters
10
10
  class BareInlineElement
11
- STANDALONE_ININE_ELEMENTS = Set.new([
12
- "img",
13
- "input",
14
- "textarea",
15
- ])
11
+ STANDALONE_INLINE_ELEMENTS = Set.new([
12
+ "img",
13
+ "input",
14
+ "textarea",
15
+ ])
16
16
 
17
17
  def call(doc)
18
18
  doc.children.each do |node|
19
- if STANDALONE_ININE_ELEMENTS.include?(node.name)
20
- parent = Nokogiri::HTML.fragment('<p/>')
21
- parent.child.add_child(node.dup)
22
- node.replace(parent)
23
- end
19
+ next unless STANDALONE_INLINE_ELEMENTS.include?(node.name)
20
+ parent = Nokogiri::HTML.fragment('<p/>')
21
+ parent.child.add_child(node.dup)
22
+ node.replace(parent)
24
23
  end
25
24
  end
26
25
  end
@@ -59,5 +59,9 @@ module Mato
59
59
  def parse_html(html)
60
60
  config.html_parser.parse(html)
61
61
  end
62
+
63
+ def convert(content, flavor:)
64
+ Mato::Converter.new(self, content, flavor).run
65
+ end
62
66
  end
63
67
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mato
4
- VERSION = "1.1.0"
4
+ VERSION = "1.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mato
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - FUJI Goro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-24 00:00:00.000000000 Z
11
+ date: 2017-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -61,6 +61,7 @@ files:
61
61
  - lib/mato/anchor_builder.rb
62
62
  - lib/mato/concerns/html_node_checkable.rb
63
63
  - lib/mato/config.rb
64
+ - lib/mato/converter.rb
64
65
  - lib/mato/document.rb
65
66
  - lib/mato/html_filters/bare_inline_element.rb
66
67
  - lib/mato/html_filters/mention_link.rb