From ec1b43a3467240258e4a85069a679ee72c01fb88 Mon Sep 17 00:00:00 2001 From: Dima Shevtsov Date: Tue, 18 Sep 2018 18:08:22 -0500 Subject: [PATCH] Added separate optoins for HTML notes and notes with enabled markdown --- README.md | 5 ++++- lib/converter.rb | 2 +- lib/converters/kramdown.rb | 12 ++++++------ lib/crawler.rb | 18 +++++++++++++++++- lib/filter.rb | 8 ++++++++ lib/options.rb | 18 +++++++++++++++++- lib/runner.rb | 8 ++++++++ test/test_crawler.rb | 30 ++++++++++++++++++++++++------ 8 files changed, 85 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index aebf82a..116457a 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,10 @@ If `PATH` is a directory, the tool reads all the `.md` files recursively. - `--headings` - `--links` - `--tables` -- `--notes` - converts notes like `
...` to Kramdown and adds `markdown=1` argument. +- `--notes` - converts notes like `
...
` 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 `
...
` and adds the `markdown="1"` argument. +- `--notes_wih_md` - converts mixed content in the notes of format `
...
` + **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 diff --git a/lib/converter.rb b/lib/converter.rb index 9b92a9d..ee1a4b5 100644 --- a/lib/converter.rb +++ b/lib/converter.rb @@ -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 = {}) diff --git a/lib/converters/kramdown.rb b/lib/converters/kramdown.rb index 6b56722..a87096e 100644 --- a/lib/converters/kramdown.rb +++ b/lib/converters/kramdown.rb @@ -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 @@ -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 diff --git a/lib/crawler.rb b/lib/crawler.rb index f0a5391..1463ea7 100644 --- a/lib/crawler.rb +++ b/lib/crawler.rb @@ -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 @@ -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 diff --git a/lib/filter.rb b/lib/filter.rb index 9fcac95..2e7c842 100644 --- a/lib/filter.rb +++ b/lib/filter.rb @@ -21,6 +21,14 @@ def notes %r{
(?:.|\n)*?<\/div>} + end + + def notes_with_md + %r{
(?:.|\n)*?<\/div>} + end + # TODO def lists; end end diff --git a/lib/options.rb b/lib/options.rb index 1b2fde8..e1c4f3e 100644 --- a/lib/options.rb +++ b/lib/options.rb @@ -7,7 +7,7 @@ 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 @@ -15,6 +15,8 @@ def initialize self.images = false self.headings = false self.notes = false + self.notes_html = false + self.notes_html_md = false end end @@ -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:' @@ -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 diff --git a/lib/runner.rb b/lib/runner.rb index bf8c0b0..8ddb127 100644 --- a/lib/runner.rb +++ b/lib/runner.rb @@ -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 diff --git a/test/test_crawler.rb b/test/test_crawler.rb index 2d42bdc..f6c9970 100644 --- a/test/test_crawler.rb +++ b/test/test_crawler.rb @@ -78,6 +78,7 @@ + HTML ).must_equal(<<-KRMD | | | | | | | @@ -85,6 +86,7 @@ | | | | | | | | | | | | | | | | | | | | | + KRMD ) end @@ -105,7 +107,9 @@ ).must_equal <<-KRAMDOWN
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. +
KRAMDOWN end @@ -125,8 +129,11 @@ HTML ).must_equal <<-KRAMDOWN
-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. +
KRAMDOWN end @@ -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 +
+{{site.data.var.ece}} supports production mode only. +
+HTML +).must_equal <<-KRAMDOWN +
+[{{site.data.var.ece}}]({{ page.baseurl }}/cloud/bk-cloud.html) supports production mode only. +
+ KRAMDOWN + end + end + + ##TOtest #
@@ -162,9 +185,4 @@ #The default configuration is set in [`/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. #
-##TOtest -#
-#{{site.data.var.ece}} supports production mode only. -#
- end