greenmat 3.2.2.4 → 3.5.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +1,24 @@
1
- /* markdown.h - generic markdown parser */
2
-
3
1
  /*
4
2
  * Copyright (c) 2009, Natacha Porté
3
+ * Copyright (c) 2015, Vicent Marti
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
5
11
  *
6
- * Permission to use, copy, modify, and distribute this software for any
7
- * purpose with or without fee is hereby granted, provided that the above
8
- * copyright notice and this permission notice appear in all copies.
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
9
14
  *
10
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
17
22
  */
18
23
 
19
24
  #ifndef MARKDOWN_H__
@@ -105,9 +110,6 @@ struct sd_callbacks {
105
110
  void (*doc_footer)(struct buf *ob, void *opaque);
106
111
  };
107
112
 
108
- /* header methods used internally in Greenmat */
109
- char* header_anchor(struct buf *text);
110
-
111
113
  struct sd_markdown;
112
114
 
113
115
  /*********
data/ext/greenmat/stack.c CHANGED
@@ -1,3 +1,25 @@
1
+ /*
2
+ * Copyright (c) 2015, Vicent Marti
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ * THE SOFTWARE.
21
+ */
22
+
1
23
  #include "stack.h"
2
24
  #include <string.h>
3
25
 
data/ext/greenmat/stack.h CHANGED
@@ -1,3 +1,25 @@
1
+ /*
2
+ * Copyright (c) 2015, Vicent Marti
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ * THE SOFTWARE.
21
+ */
22
+
1
23
  #ifndef STACK_H__
2
24
  #define STACK_H__
3
25
 
data/greenmat.gemspec CHANGED
@@ -23,8 +23,8 @@ Gem::Specification.new do |s|
23
23
 
24
24
  s.add_development_dependency "activesupport"
25
25
  s.add_development_dependency "nokogiri", "~> 1.6.0"
26
- s.add_development_dependency "rake", "~> 10.0"
27
- s.add_development_dependency "rake-compiler", "~> 0.8.3"
26
+ s.add_development_dependency "rake", "~> 12.2.1"
27
+ s.add_development_dependency "rake-compiler", "~> 1.0.3"
28
28
  s.add_development_dependency "rspec", "~> 3.2"
29
- s.add_development_dependency "test-unit", "~> 2.5.4"
29
+ s.add_development_dependency "test-unit", "~> 3.2.3"
30
30
  end
data/lib/greenmat.rb CHANGED
@@ -53,7 +53,7 @@ module Greenmat
53
53
  '>' => '&gt;',
54
54
  '"' => '&quot;',
55
55
  "'" => '&#x27;',
56
- "/" => '&#x2F',
56
+ "/" => '&#x2F;',
57
57
  })
58
58
  end
59
59
  end
@@ -0,0 +1,86 @@
1
+ require 'greenmat'
2
+ require 'optparse'
3
+
4
+ module Greenmat
5
+ # This class aims at easing the creation of custom
6
+ # binary for your needs. For example, you can add new
7
+ # options or change the existing ones. The parsing
8
+ # is handled by Ruby's OptionParser. For instance:
9
+ #
10
+ # class Custom::CLI < Greenmat::CLI
11
+ # def self.options_parser
12
+ # super.tap do |opts|
13
+ # opts.on("--rainbow") do
14
+ # @@options[:rainbow] = true
15
+ # end
16
+ # end
17
+ # end
18
+ #
19
+ # def self.render_object
20
+ # @@options[:rainbow] ? RainbowRender : super
21
+ # end
22
+ # end
23
+ class CLI
24
+ def self.options_parser
25
+ @@options = {
26
+ render_extensions: {},
27
+ parse_extensions: {},
28
+ smarty_pants: false
29
+ }
30
+
31
+ OptionParser.new do |opts|
32
+ opts.banner = "Usage: greenmat [--parse <extension>...] " \
33
+ "[--render <extension>...] [--smarty] <file>..."
34
+
35
+ opts.on("--parse EXTENSION", "Enable a parsing extension") do |ext|
36
+ ext = ext.gsub('-', '_').to_sym
37
+ @@options[:parse_extensions][ext] = true
38
+ end
39
+
40
+ opts.on("--render EXTENSION", "Enable a rendering extension") do |ext|
41
+ ext = ext.gsub('-', '_').to_sym
42
+ @@options[:render_extensions][ext] = true
43
+ end
44
+
45
+ opts.on("--smarty", "Enable Smarty Pants") do
46
+ @@options[:smarty_pants] = true
47
+ end
48
+
49
+ opts.on_tail("-v", "--version", "Display the current version") do
50
+ STDOUT.puts "Greenmat #{Greenmat::VERSION}"
51
+ exit
52
+ end
53
+
54
+ opts.on_tail("-h", "--help", "Display this help message") do
55
+ puts opts
56
+ exit
57
+ end
58
+ end
59
+ end
60
+
61
+ def self.process(args)
62
+ self.legacy_parse!(args)
63
+ self.options_parser.parse!(args)
64
+ STDOUT.write parser_object.render(ARGF.read)
65
+ end
66
+
67
+ def self.render_object
68
+ @@options[:smarty_pants] ? Render::SmartyHTML : Render::HTML
69
+ end
70
+
71
+ def self.parser_object
72
+ renderer = render_object.new(@@options[:render_extensions])
73
+ Greenmat::Markdown.new(renderer, @@options[:parse_extensions])
74
+ end
75
+
76
+ def self.legacy_parse!(args) # :nodoc:
77
+ # Workaround for backward compatibility as OptionParser
78
+ # doesn't support the --flag-OPTION syntax.
79
+ args.select {|a| a =~ /--(parse|render)-/ }.each do |arg|
80
+ args.delete(arg)
81
+ arg = arg.partition(/\b-/)
82
+ args.push(arg.first, arg.last)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -1,8 +1,3 @@
1
- require 'greenmat'
2
-
3
- # Deprecated: Please use the default API to parse Markdown
4
- # documents ; this layer will be removed in Greenmat 4.0.
5
- #
6
1
  # Creates an instance of Greenmat with the RedCloth API.
7
2
  class GreenmatCompat
8
3
  attr_accessor :text
@@ -13,7 +13,7 @@ module Greenmat
13
13
  :autolink, :codespan, :double_emphasis,
14
14
  :emphasis, :underline, :raw_html,
15
15
  :triple_emphasis, :strikethrough,
16
- :superscript,
16
+ :superscript, :highlight, :quote,
17
17
 
18
18
  # footnotes
19
19
  :footnotes, :footnote_def, :footnote_ref,
@@ -43,6 +43,18 @@ module Greenmat
43
43
  def header(text, header_level)
44
44
  text + "\n"
45
45
  end
46
+
47
+ def table(header, body)
48
+ "#{header}#{body}"
49
+ end
50
+
51
+ def table_row(content)
52
+ content + "\n"
53
+ end
54
+
55
+ def table_cell(content, alignment)
56
+ content + "\t"
57
+ end
46
58
  end
47
59
  end
48
60
  end
@@ -1,3 +1,3 @@
1
1
  module Greenmat
2
- VERSION = '3.2.2.4'
2
+ VERSION = '3.5.1.0'
3
3
  end
@@ -4,16 +4,48 @@ require 'test_helper'
4
4
  class CustomRenderTest < Greenmat::TestCase
5
5
  class SimpleRender < Greenmat::Render::HTML
6
6
  def emphasis(text)
7
- "<em class=\"cool\">#{text}</em>"
7
+ if @options[:no_intra_emphasis]
8
+ return %(<em class="no_intra_emphasis">#{text}</em>)
9
+ end
10
+
11
+ %(<em class="cool">#{text}</em>)
12
+ end
13
+
14
+ def header(text, level)
15
+ "My little poney" if @options[:with_toc_data]
8
16
  end
9
17
  end
10
18
 
11
19
  def test_simple_overload
12
20
  md = Greenmat::Markdown.new(SimpleRender)
13
- html_equal "<p>This is <em class=\"cool\">just</em> a test</p>\n",
21
+ assert_equal "<p>This is <em class=\"cool\">just</em> a test</p>\n",
14
22
  md.render("This is *just* a test")
15
23
  end
16
24
 
25
+ def test_renderer_options
26
+ parser = Greenmat::Markdown.new(SimpleRender.new(with_toc_data: true))
27
+ output = parser.render("# A title")
28
+
29
+ assert_match "My little poney", output
30
+ end
31
+
32
+ def test_markdown_options
33
+ parser = Greenmat::Markdown.new(SimpleRender, no_intra_emphasis: true)
34
+ output = parser.render("*foo*")
35
+
36
+ assert_match "no_intra_emphasis", output
37
+ end
38
+
39
+ def test_original_options_hash_is_not_mutated
40
+ options = { with_toc_data: true }
41
+ render = SimpleRender.new(options)
42
+ parser = Greenmat::Markdown.new(render, tables: true)
43
+
44
+ computed_options = render.instance_variable_get(:"@options")
45
+
46
+ refute_equal computed_options.object_id, options.object_id
47
+ end
48
+
17
49
  class NilPreprocessRenderer < Greenmat::Render::HTML
18
50
  def preprocess(fulldoc)
19
51
  nil
@@ -25,4 +57,11 @@ class CustomRenderTest < Greenmat::TestCase
25
57
  assert_equal(nil,md.render("Anything"))
26
58
  end
27
59
 
60
+ def test_base_render_without_quote_callback
61
+ # Regression test for https://github.com/vmg/greenmat/issues/569
62
+ render = Class.new(Greenmat::Render::Base)
63
+ parser = Greenmat::Markdown.new render.new, quote: true
64
+
65
+ assert_equal "", parser.render(%(a "quote"))
66
+ end
28
67
  end
@@ -0,0 +1,80 @@
1
+ require 'test_helper'
2
+ require 'tempfile'
3
+
4
+ class GreenmatBinTest < Greenmat::TestCase
5
+ def setup
6
+ @fixture_file = Tempfile.new('bin')
7
+ @fixture_path = @fixture_file.path
8
+
9
+ @fixture_file.write "A ==simple== fixture file -- with " \
10
+ "a [link](https://github.com)."
11
+ @fixture_file.rewind
12
+ end
13
+
14
+ def teardown
15
+ @fixture_file.unlink
16
+ end
17
+
18
+ def test_vanilla_bin
19
+ run_bin(@fixture_path)
20
+
21
+ expected = "<p>A ==simple== fixture file -- with " \
22
+ "a <a href=\"https://github.com\">link</a>.</p>\n"
23
+
24
+ assert_equal expected, @output
25
+ end
26
+
27
+ def test_enabling_a_parse_option
28
+ run_bin("--parse", "highlight", @fixture_path)
29
+
30
+ assert_output "<mark>"
31
+ refute_output "=="
32
+ end
33
+
34
+ def test_enabling_a_render_option
35
+ run_bin("--render", "no-links", @fixture_path)
36
+
37
+ assert_output "[link]"
38
+ refute_output "</a>"
39
+ end
40
+
41
+ def test_enabling_smarty_pants
42
+ run_bin("--smarty", @fixture_path)
43
+
44
+ assert_output "&ndash"
45
+ refute_output "--"
46
+ end
47
+
48
+ def test_version_option
49
+ run_bin("--version")
50
+ assert_output "Greenmat #{Greenmat::VERSION}"
51
+ end
52
+
53
+ def test_legacy_option_parsing
54
+ run_bin("--parse-highlight", "--render-no-links", @fixture_path)
55
+
56
+ assert_output "<mark>"
57
+ refute_output "=="
58
+
59
+ assert_output "[link]"
60
+ refute_output "</a>"
61
+ end
62
+
63
+ private
64
+
65
+ def run_bin(*args)
66
+ bin_path = File.expand_path('../../bin/greenmat', __FILE__)
67
+ ruby = RbConfig.ruby
68
+ IO.popen("#{ruby} #{bin_path} #{args.join(" ")}") do |stream|
69
+ @output = stream.read
70
+ end
71
+ end
72
+
73
+ def assert_output(pattern)
74
+ assert_match pattern, @output
75
+ end
76
+
77
+ def refute_output(pattern)
78
+ refute_match Regexp.new(pattern), @output
79
+ end
80
+ end
@@ -4,35 +4,35 @@ require 'test_helper'
4
4
  class GreenmatCompatTest < Greenmat::TestCase
5
5
  def test_simple_compat_api
6
6
  html = GreenmatCompat.new("This is_just_a test").to_html
7
- html_equal "<p>This is<em>just</em>a test</p>\n", html
7
+ assert_equal "<p>This is<em>just</em>a test</p>\n", html
8
8
  end
9
9
 
10
10
  def test_compat_api_enables_extensions
11
11
  html = GreenmatCompat.new("This is_just_a test", :no_intra_emphasis).to_html
12
- html_equal "<p>This is_just_a test</p>\n", html
12
+ assert_equal "<p>This is_just_a test</p>\n", html
13
13
  end
14
14
 
15
15
  def test_compat_api_knows_fenced_code_extension
16
16
  text = "```ruby\nx = 'foo'\n```"
17
17
  html = GreenmatCompat.new(text, :fenced_code).to_html
18
- html_equal "<pre><code data-metadata=\"ruby\">x = 'foo'\n</code></pre>\n", html
18
+ assert_equal "<pre><code data-metadata=\"ruby\">x = &#39;foo&#39;\n</code></pre>\n", html
19
19
  end
20
20
 
21
21
  def test_compat_api_ignores_gh_blockcode_extension
22
22
  text = "```ruby\nx = 'foo'\n```"
23
23
  html = GreenmatCompat.new(text, :fenced_code, :gh_blockcode).to_html
24
- html_equal "<pre><code data-metadata=\"ruby\">x = 'foo'\n</code></pre>\n", html
24
+ assert_equal "<pre><code data-metadata=\"ruby\">x = &#39;foo&#39;\n</code></pre>\n", html
25
25
  end
26
26
 
27
27
  def test_compat_api_knows_no_intraemphasis_extension
28
28
  html = GreenmatCompat.new("This is_just_a test", :no_intraemphasis).to_html
29
- html_equal "<p>This is_just_a test</p>\n", html
29
+ assert_equal "<p>This is_just_a test</p>\n", html
30
30
  end
31
31
 
32
32
  def test_translate_outdated_extensions
33
33
  # these extensions are no longer used
34
34
  exts = [:gh_blockcode, :no_tables, :smart, :strict]
35
35
  html = GreenmatCompat.new('"TEST"', *exts).to_html
36
- html_equal "<p>&quot;TEST&quot;</p>\n", html
36
+ assert_equal "<p>&quot;TEST&quot;</p>\n", html
37
37
  end
38
38
  end
data/test/html5_test.rb CHANGED
@@ -2,68 +2,81 @@ require 'test_helper'
2
2
 
3
3
  class HTML5Test < Greenmat::TestCase
4
4
  def test_that_html5_works
5
- section = <<EOS
6
- <section>
7
- <p>The quick brown fox jumps over the lazy dog.</p>
8
- </section>
9
- EOS
5
+ section = <<-HTML.chomp.strip_heredoc
6
+ <section>
7
+ <p>The quick brown fox jumps over the lazy dog.</p>
8
+ </section>
9
+ HTML
10
10
 
11
- figure = <<EOS
12
- <figure>
13
- <img src="http://example.org/image.jpg" alt="">
14
- <figcaption>
15
- <p>Hello world!</p>
16
- </figcaption>
17
- </figure>
18
- EOS
11
+ figure = <<-HTML.chomp.strip_heredoc
12
+ <figure>
13
+ <img src="http://example.org/image.jpg" alt="">
14
+ <figcaption>
15
+ <p>Hello world!</p>
16
+ </figcaption>
17
+ </figure>
18
+ HTML
19
19
 
20
20
  assert_renders section, section
21
21
  assert_renders figure, figure
22
22
  end
23
23
 
24
24
  def test_that_html5_works_with_code_blocks
25
- section = <<EOS
25
+ section = <<-HTML
26
26
  \t<section>
27
27
  \t\t<p>The quick brown fox jumps over the lazy dog.</p>
28
28
  \t</section>
29
- EOS
29
+ HTML
30
30
 
31
- section_expected = <<EOE
32
- <pre><code>&lt;section&gt;
33
- &lt;p&gt;The quick brown fox jumps over the lazy dog.&lt;/p&gt;
34
- &lt;/section&gt;
35
- </code></pre>
36
- EOE
31
+ section_expected = <<-HTML.chomp.strip_heredoc
32
+ <pre><code>&lt;section&gt;
33
+ &lt;p&gt;The quick brown fox jumps over the lazy dog.&lt;/p&gt;
34
+ &lt;/section&gt;
35
+ </code></pre>
36
+ HTML
37
37
 
38
- header = <<EOS
38
+ header = <<-HTML
39
39
  <header>
40
40
  <hgroup>
41
41
  <h1>Section heading</h1>
42
42
  <h2>Subhead</h2>
43
43
  </hgroup>
44
44
  </header>
45
- EOS
45
+ HTML
46
46
 
47
- header_expected = <<EOE
48
- <pre><code>&lt;header&gt;
49
- &lt;hgroup&gt;
50
- &lt;h1&gt;Section heading&lt;/h1&gt;
51
- &lt;h2&gt;Subhead&lt;/h2&gt;
52
- &lt;/hgroup&gt;
53
- &lt;/header&gt;
54
- </code></pre>
55
- EOE
47
+ header_expected = <<-HTML.chomp.strip_heredoc
48
+ <pre><code>&lt;header&gt;
49
+ &lt;hgroup&gt;
50
+ &lt;h1&gt;Section heading&lt;/h1&gt;
51
+ &lt;h2&gt;Subhead&lt;/h2&gt;
52
+ &lt;/hgroup&gt;
53
+ &lt;/header&gt;
54
+ </code></pre>
55
+ HTML
56
56
 
57
57
  assert_renders section_expected, section
58
58
  assert_renders header_expected, header
59
59
  end
60
60
 
61
61
  def test_script_tag_recognition
62
- markdown = <<-Md
63
- <script type="text/javascript">
64
- alert('Foo!');
65
- </script>
66
- Md
67
- assert_renders markdown, markdown
62
+ html = <<-HTML.chomp.strip_heredoc
63
+ <script type="text/javascript">
64
+ alert('Foo!');
65
+ </script>
66
+ HTML
67
+
68
+ assert_renders html, html
69
+ end
70
+
71
+ def test_new_html5_tags_not_escaped
72
+ details = <<-HTML.chomp.strip_heredoc
73
+ <details>
74
+ log:
75
+
76
+ </details>
77
+ HTML
78
+
79
+ assert_renders details, details
68
80
  end
81
+
69
82
  end