Skip to content

Commit

Permalink
Added separate optoins for HTML notes and notes with enabled markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
dshevtsov committed Sep 18, 2018
1 parent 8ff9740 commit ec1b43a
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 16 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ If `PATH` is a directory, the tool reads all the `.md` files recursively.
- `--headings`
- `--links`
- `--tables`
- `--notes` - converts notes like `<div class="bs-callout bs-callout-xxx">...` to Kramdown and adds `markdown=1` argument.
- `--notes` - converts notes like `<div class="bs-callout bs-callout-xxx" ... >...</div>` to Kramdown and adds the `markdown="1"` argument if it is not there. Same as `--notes_html` + `--notes_wih_md`.
- `--notes_html` - converts HTML content in the notes of format `<div class="bs-callout bs-callout-xxx" ... >...</div>` and adds the `markdown="1"` argument.
- `--notes_wih_md` - converts mixed content in the notes of format `<div class="bs-callout bs-callout-xxx" markdown="1">...</div>`

**Cution:** If the note is already in the valid Kramdown format and doesn't contain HTML, the tool still converts it and can break the valid formatting.

### Example
Expand Down
2 changes: 1 addition & 1 deletion lib/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module HtmlToKramdown
# Converts input HTML to kramdown
class Converter
def default_options
{ html_to_native: false, line_width: 1000, input: 'html' }
{ html_to_native: true, line_width: 1000, input: 'html' }
end

def to_kramdown(string, options = {})
Expand Down
12 changes: 6 additions & 6 deletions lib/converters/kramdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def convert(el, opts = { indent: 0 })
([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"
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
Expand All @@ -50,7 +50,7 @@ def convert_text(el, opts)
else
el.value.gsub(/\A\n/) do
opts[:prev] && opts[:prev].type == :br ? '' : "\n"
end.gsub(/\s+/, ' ')#.gsub(ESCAPED_CHAR_RE) { "\\#{$1 || $2}" }
end#.gsub(/\s+/, ' ').gsub(ESCAPED_CHAR_RE) { "\\#{$1 || $2}" }
end
end

Expand Down
18 changes: 17 additions & 1 deletion lib/crawler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ def notes_to_kramdown(content)
content.gsub(notes, &replace)
end

def notes_html_to_kramdown(content)
content.gsub(notes_html, &replace)
end

def notes_with_md_to_kramdown(content)
content.gsub(notes_with_md, &replace)
end

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

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

def converter
Expand Down Expand Up @@ -60,5 +68,13 @@ def tables
def notes
filter.notes
end

def notes_html
filter.notes_html
end

def notes_with_md
filter.notes_with_md
end
end
end
8 changes: 8 additions & 0 deletions lib/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def notes
%r{<div class="bs-callout bs-callout.+>(?:.|\n)*?<\/div>}
end

def notes_html
%r{<div class="bs-callout (bs-callout-(info|warning|tip))"[[:blank:]]?(id="\2")?[[:blank:]]?>(?:.|\n)*?<\/div>}
end

def notes_with_md
%r{<div class="bs-callout (bs-callout-(info|warning|tip))"[[:blank:]]?(id="\2")?[[:blank:]]?markdown="1">(?:.|\n)*?<\/div>}
end

# TODO
def lists; end
end
Expand Down
18 changes: 17 additions & 1 deletion lib/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ class Options
VERSION = '5'.freeze
# CLI options initialization
class ScriptOptions
attr_accessor :links, :tables, :images, :headings, :notes, :help
attr_accessor :links, :tables, :images, :headings, :notes, :help, :notes_html, :notes_html_md

def initialize
self.links = false
self.tables = false
self.images = false
self.headings = false
self.notes = false
self.notes_html = false
self.notes_html_md = false
end
end

Expand All @@ -41,6 +43,8 @@ def self.option_parser
images_option parser
tables_option parser
notes_option parser
notes_html_option parser
notes_with_md_option parser

parser.separator ''
parser.separator 'Common options:'
Expand Down Expand Up @@ -87,5 +91,17 @@ def self.notes_option(parser)
@options.notes = n
end
end

def self.notes_html_option(parser)
parser.on('-h', '--notes_html', 'Convert HTML notes WITHOUT "mardown=1" in the .md files in the given path recursively.') do |nh|
@options.notes_html = nh
end
end

def self.notes_with_md_option(parser)
parser.on('-m', '--notes-with-md', 'Convert HTML notes WITH "mardown=1" in the .md files in the given path recursively.') do |nm|
@options.notes_with_md = nm
end
end
end
end
8 changes: 8 additions & 0 deletions lib/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ def go(file)
@content = reader.all(file)
converted_content = crawler.notes_to_kramdown(@content)
write(file, converted_content)
elsif @options.notes_html
@content = reader.all(file)
converted_content = crawler.notes_html_to_kramdown(@content)
write(file, converted_content)
elsif @options.notes_with_md
@content = reader.all(file)
converted_content = crawler.notes_with_md_to_kramdown(@content)
write(file, converted_content)
end
end

Expand Down
30 changes: 24 additions & 6 deletions test/test_crawler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@
</tr>
</tbody>
</table>
HTML
).must_equal(<<-KRMD
| | | | | | |
|----------
| | | | | | |
| | | | | | |
| | | | | | |
KRMD
)
end
Expand All @@ -105,7 +107,9 @@
).must_equal <<-KRAMDOWN
<div class="bs-callout bs-callout-warning" markdown="1">
Don’t configure the module in your local before building and deploying. You’ll configure the module in those environments.
We recommend using the `bin/magento magento-cloud:scd-dump` command for Configuration Management ([2.1.X]({{ site.baseurl }}/guides/v2.1/cloud/live/sens-data-over.html#cloud-config-specific-recomm), [2.2.X]({{ site.baseurl }}/guides/v2.2/cloud/live/sens-data-over.html#cloud-config-specific-recomm)). If you use the `app:config:dump` command, all configuration options for Fastly will be locked from editing in Staging and Production.
</div>
KRAMDOWN
end
Expand All @@ -125,8 +129,11 @@
HTML
).must_equal <<-KRAMDOWN
<div class="bs-callout bs-callout-warning" markdown="1">
Don’t configure the module in your local before building and deploying. You’ll configure the module in those environments. We recommend using the `bin/magento magento-cloud:scd-dump` command for Configuration Management ([2.1.X]({{ site.baseurl }}/guides/v2.1/cloud/live/sens-data-over.html#cloud-config-specific-recomm), [2.2.X]({{ site.baseurl }}/guides/v2.2/cloud/live/sens-data-over.html#cloud-config-specific-recomm)).
Don’t configure the module in your local before building and deploying. You’ll configure the module in those environments.
We recommend using the `bin/magento magento-cloud:scd-dump` command for Configuration Management ([2.1.X]({{ site.baseurl }}/guides/v2.1/cloud/live/sens-data-over.html#cloud-config-specific-recomm), [2.2.X]({{ site.baseurl }}/guides/v2.2/cloud/live/sens-data-over.html#cloud-config-specific-recomm)).
If you use the `app:config:dump` command, all configuration options for Fastly will be locked from editing in Staging and Production.
</div>
KRAMDOWN
end
Expand All @@ -147,6 +154,22 @@
end
end

describe 'when converting a note wrapped in div with markdown="1" but with no HTML ' do
it 'must remain it as is' do
@crawler.notes_html_to_kramdown(<<-HTML
<div class="bs-callout bs-callout-info">
<a href="{{ page.baseurl }}/cloud/bk-cloud.html">{{site.data.var.ece}}</a> supports production mode only.
</div>
HTML
).must_equal <<-KRAMDOWN
<div class="bs-callout bs-callout-info" markdown="1">
[{{site.data.var.ece}}]({{ page.baseurl }}/cloud/bk-cloud.html) supports production mode only.
</div>
KRAMDOWN
end
end


##TOtest
#<div class="bs-callout bs-callout-info" markdown="1">

Expand All @@ -162,9 +185,4 @@
#The default configuration is set in [`<magento2>/dev/tests/functional/etc/config.xml.dist`]({{ site.mage2000url }}dev/tests/functional/etc/config.xml.dist). It should be copied as `config.xml` for further changes.
#</div>

##TOtest
#<div class="bs-callout bs-callout-info">
#<a href="{{ page.baseurl }}/cloud/bk-cloud.html">{{site.data.var.ece}}</a> supports production mode only.
#</div>

end

0 comments on commit ec1b43a

Please sign in to comment.