Skip to content

Commit

Permalink
Fixed the trailing new line
Browse files Browse the repository at this point in the history
Diabled escaping for special characters
  • Loading branch information
dshevtsov committed Aug 31, 2018
1 parent 83c7b2d commit 973af83
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 73 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ If `PATH` is a directory, the tool reads all the `.md` files recursively.
### Available options

- `--images`
- `--headings` - adds a blank line after a heading.
- `--links` - converts to inline links.
- `--tables` - adds two blank lines after a table.
- `--headings`
- `--links`
- `--tables`

### Example

Expand All @@ -30,7 +30,7 @@ $ gem install kramdown
## Precautions

Note, that the Kramdown parser doesn't recognize Kramdown elements inside HTML blocks by default.
To make it work, provide additional parameter to tell Kramdown to parse kramdown inside HTML: `markdown="1"`, or `markdown="span"`, or `markdown="block"`
To make it work, provide additional parameter to enable Kramdown parsing within HTML: `markdown="1"`, or `markdown="span"`, or `markdown="block"`

Breaking example:
```html
Expand Down
1 change: 0 additions & 1 deletion lib/converter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require_relative 'converters/kramdown.rb'
require 'pandoc-ruby'
module HtmlToKramdown
# Converts input HTML to kramdown
class Converter
Expand Down
33 changes: 32 additions & 1 deletion lib/converters/kramdown.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
require 'kramdown'

# Override the 'converter/kramdown' to disable link definitions
module Kramdown
module Converter
# Converts an element tree to the kramdown format.
class Kramdown

ESCAPED_CHAR_RE = /(\$\$|])|^[ ]{0,3}(:)/

# Remove the links definitions conversion
def convert_a(el, opts)
if el.attr['href'].empty?
"[#{inner(el, opts)}]()"
#elsif el.attr['href'] =~ /^(?:http|ftp)/ || el.attr['href'].count("()") > 0
# index = if link_el = @linkrefs.find {|c| c.attr['href'] == el.attr['href']}
# @linkrefs.index(link_el) + 1
# else
# @linkrefs << el
# @linkrefs.size
# end
# "[#{inner(el, opts)}][#{index}]"
else
title = parse_title(el.attr['title'])
"[#{inner(el, opts)}](#{el.attr['href']}#{title})"
end
end

# Disabling addition of an extra new line
def convert(el, opts = {:indent => 0})
res = send("convert_#{el.type}", el, opts)
if ![:html_element, :li, :dt, :dd, :td].include?(el.type) && (ial = ial_for_element(el))
res << ial
res << "\n\n" if Element.category(el) == :block
elsif [:ul, :dl, :ol, :codeblock].include?(el.type) && opts[:next] &&
([el.type, :codeblock].include?(opts[:next].type) ||
(opts[:next].type == :blank && opts[:nnext] && [el.type, :codeblock].include?(opts[:nnext].type)))
res << "^\n\n"
#elsif Element.category(el) == :block &&
# ![:li, :dd, :dt, :td, :th, :tr, :thead, :tbody, :tfoot, :blank].include?(el.type) &&
# (el.type != :html_element || @stack.last.type != :html_element) &&
# (el.type != :p || !el.options[:transparent])
# res << "\n"
end
res
end
end
end
end
16 changes: 4 additions & 12 deletions lib/crawler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,27 @@ module HtmlToKramdown
# convert them to Kramdown, and return the updated content
class Crawler
def links_to_kramdown(content)
content.gsub(links, &replace).strip
content.gsub(links, &replace)
end

def headings_to_kramdown(content)
content.gsub(headings, &replace)
end

def images_to_kramdown(content)
content.gsub(images, &replace).strip
content.gsub(images, &replace)
end

def tables_to_kramdown(content)
content.gsub(tables, &replace)
end

def substitute
convert_to_kramdown(matcher)
content.gsub(tables, &replace)
end

def replace
->(s) { convert_to_kramdown(s) }
end

def convert_to_kramdown(string, options = {})
converter.to_kramdown(string, options)
end

def matcher
Regexp.last_match(1).to_s
converter.to_kramdown(string, options).chomp
end

def converter
Expand Down
2 changes: 1 addition & 1 deletion lib/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module HtmlToKramdown
# CLI option parser
class Options
VERSION = '3'.freeze
VERSION = '4'.freeze
# CLI options initialization
class ScriptOptions
attr_accessor :links, :tables, :images, :headings
Expand Down
38 changes: 26 additions & 12 deletions test/test_converter.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
require 'minitest/autorun'
require_relative '../lib/converter.rb'
#require 'minitest/debugger'
include HtmlToKramdown

describe Converter do
before do
@converter = Converter.new
end

ESCAPED_CHAR_RE = /(\$\$|[\\*_`\[\]\{"'|])|^[ ]{0,3}(:)/

describe 'when converting the heading with default options and no attributes' do
it 'must return the valid kramdown heading' do
@converter.to_kramdown('<h2>This is a heading</h2>').must_equal "## This is a heading\n\n"
it 'must return the valid kramdown heading with trailing new line' do
@converter.to_kramdown('<h2>This is a heading</h2>').must_equal "## This is a heading\n"
end
end

describe 'when converting the heading with default options and id attribute' do
it 'must return the valid kramdown heading with id in kramdown' do
@converter.to_kramdown("<h2 id='heading'>This is a heading</h2>").must_equal "## This is a heading {#heading}\n\n"
it 'must return the valid kramdown heading with id in kramdown with trailing new line' do
@converter.to_kramdown("<h2 id='heading'>This is a heading</h2>").must_equal "## This is a heading {#heading}\n"
end
end

describe 'when converting the heading with default options and id attribute' do
it 'must return the valid kramdown heading with id in kramdown' do
@converter.to_kramdown("<h2 id='heading'>This is a heading</h2>").must_equal "## This is a heading {#heading}\n\n"
it 'must return the valid kramdown heading with id in kramdown with trailing new line' do
@converter.to_kramdown("<h2 id='heading'>This is a heading</h2>").must_equal "## This is a heading {#heading}\n"
end
end

describe 'when converting the link with Liquid variables' do
it 'must return the valid kramdown link' do
@converter.to_kramdown('<a href="{{ page.baseurl }}/cloud/project/project-conf-files_services-elastic.html#cloud-es-config-mg">Get this value</a>').must_equal "[Get this value]({{ page.baseurl }}/cloud/project/project-conf-files_services-elastic.html#cloud-es-config-mg)\n\n"
it 'must return the valid inline kramdown link' do
@converter.to_kramdown('<a href="{{ page.baseurl }}/cloud/project/project-conf-files_services-elastic.html#cloud-es-config-mg">Get this value</a>').must_equal "[Get this value]({{ page.baseurl }}/cloud/project/project-conf-files_services-elastic.html#cloud-es-config-mg)\n"
end
end

describe 'when converting the external link' do
it 'must return the valid kramdown link' do
@converter.to_kramdown('<a href="http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html">Get this value</a>').must_equal "[Get this value](http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html)\n\n"
it 'must return the valid inline kramdown link with trailing new line' do
@converter.to_kramdown('<a href="http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html">Get this value</a>').must_equal "[Get this value](http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html)\n"
end
end

describe 'when converting the image with default options and no text and attributes' do
it 'must return the image in Kramdown with trailing new line' do
@converter.to_kramdown('<img src="{{ site.baseurl }}/common/images/h5d-sectioning-flowchart.png">').must_equal "![]({{ site.baseurl }}/common/images/h5d-sectioning-flowchart.png)\n"
end
end

describe 'when converting the text with {}' do
it 'must return {} without escapting' do
@converter.to_kramdown('{}').must_equal "{}"
end
end

describe 'when giving the simple HTML table' do
it 'must return the table in Kramdown' do
it 'must return the table in Kramdown with trailing new line' do
@converter.to_kramdown(<<-HTML
<table>
<thead>
Expand Down Expand Up @@ -85,7 +100,6 @@
| | | | | | |
| | | | | | |
| | | | | | |
KRMD
)
end
Expand Down
79 changes: 37 additions & 42 deletions test/test_crawler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,89 +9,84 @@

describe 'when crawling the headings' do
it 'must return the valid kramdown heading' do
@crawler.headings_to_kramdown("<h2>This is a heading</h2>").must_equal "## This is a heading\n\n"
@crawler.headings_to_kramdown('<h2>This is a heading</h2>').must_equal "## This is a heading"
end
end

describe 'when crawling the image' do
it 'must return the valid kramdown image' do
@crawler.images_to_kramdown('<img src="{{ site.baseurl }}/common/images/h5d-sectioning-flowchart.png">').must_equal '![]({{ site.baseurl }}/common/images/h5d-sectioning-flowchart.png)'
@crawler.images_to_kramdown('<img src="{{ site.baseurl }}/common/images/h5d-sectioning-flowchart.png">').must_equal "![]({{ site.baseurl }}/common/images/h5d-sectioning-flowchart.png)"
end
end

describe 'when giving the external HTML link' do
it 'must return the inline link in Kramdown' do
@crawler.links_to_kramdown('<a href="http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html">Get this value</a>').must_equal "[Get this value](http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html)"
@crawler.links_to_kramdown('<a href="http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html">Get this value</a>').must_equal '[Get this value](http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html)'
end
end

describe 'when giving the internal HTML link' do
it 'must return the inline link in Kramdown' do
@crawler.links_to_kramdown('<a href="{{ page.baseurl }}/cloud/project/project-integrate-blackfire.html">Blackfire.io</a>').must_equal "[Blackfire.io]({{ page.baseurl }}/cloud/project/project-integrate-blackfire.html)"
@crawler.links_to_kramdown('<a href="{{ page.baseurl }}/cloud/project/project-integrate-blackfire.html">Blackfire {{io}}</a>').must_equal '[Blackfire {{io}}]({{ page.baseurl }}/cloud/project/project-integrate-blackfire.html)'
end
end

describe 'when giving the internal HTML link to section' do
it 'must return the inline link in Kramdown' do
@crawler.links_to_kramdown('<a href="#section">Blackfire.io</a>').must_equal "[Blackfire.io](#section)"
@crawler.links_to_kramdown('<a href="#section">Blackfire.io</a>').must_equal '[Blackfire.io](#section)'
end
end

describe 'when giving the internal HTML link to section' do
it 'must return the inline link in Kramdown' do
@crawler.tables_to_kramdown(<<-HTML
<table>
<thead>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</tbody>
</table>
HTML
).must_equal(<<-KRMD
HTML
).must_equal(<<-KRMD
| | | | | | |
|----------
| | | | | | |
| | | | | | |
| | | | | | |
KRMD
)
KRMD
)
end
end



end

0 comments on commit 973af83

Please sign in to comment.