Skip to content

Commit

Permalink
Resolved merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
dshevtsov committed Sep 4, 2018
2 parents 973af83 + ff46fb3 commit 8ff9740
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 121 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ 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.
**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
72 changes: 0 additions & 72 deletions html-to-markdown.xcodeproj/project.pbxproj

This file was deleted.

This file was deleted.

This file was deleted.

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

def to_kramdown(string, options = {})
document = Kramdown::Document.new(string, default_options.merge(options))
document.to_kramdown
end
end
end
end
57 changes: 42 additions & 15 deletions lib/converters/kramdown.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
require 'kramdown'
# Override the 'converter/kramdown' to disable link definitions
# Disabling some functionality commenting it out
module Kramdown
module Converter
# Converts an element tree to the kramdown format.
class Kramdown

ESCAPED_CHAR_RE = /(\$\$|])|^[ ]{0,3}(:)/
#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
# 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
Expand All @@ -25,24 +24,52 @@ def convert_a(el, opts)
end
end

# Disabling addition of an extra new line
def convert(el, opts = {:indent => 0})
# Disabling addition of an extra new line and excaping
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))
if !%i[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)))
elsif %i[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"
# 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

# Disable escaping for special characters in text
def convert_text(el, opts)
if opts[:raw_text]
el.value
else
el.value.gsub(/\A\n/) do
opts[:prev] && opts[:prev].type == :br ? '' : "\n"
end.gsub(/\s+/, ' ')#.gsub(ESCAPED_CHAR_RE) { "\\#{$1 || $2}" }
end
end

# Disable escaping for special characters in alt text of images
def convert_img(el, opts)
alt_text = el.attr['alt'].to_s#.gsub(ESCAPED_CHAR_RE) { $1 ? "\\#{$1}" : $2 }
src = el.attr['src'].to_s
if src.empty?
"![#{alt_text}]()"
else
title = parse_title(el.attr['title'])
link = if src.count("()") > 0
"<#{src}>"
else
src
end
"![#{alt_text}](#{link}#{title})"
end
end
end
end
end
10 changes: 9 additions & 1 deletion lib/crawler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def images_to_kramdown(content)
end

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

def notes_to_kramdown(content)
content.gsub(notes, &replace)
end

def replace
Expand Down Expand Up @@ -52,5 +56,9 @@ def links
def tables
filter.tables
end

def notes
filter.notes
end
end
end
11 changes: 6 additions & 5 deletions lib/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ def headings
end

def links
%r{(<a href=.+</a>)}
%r{<a href=.+</a>}
end

def tables
%r{(<table[^>]*>(?:.|\n)*?<\/table>)}
end
%r{<table[^>]*>(?:.|\n)*?<\/table>}
end

def images
/(^<img.+>)/
end

# TODO
def notes; end
def notes
%r{<div class="bs-callout bs-callout.+>(?:.|\n)*?<\/div>}
end

# TODO
def lists; end
Expand Down
12 changes: 10 additions & 2 deletions lib/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
module HtmlToKramdown
# CLI option parser
class Options
VERSION = '4'.freeze
VERSION = '5'.freeze
# CLI options initialization
class ScriptOptions
attr_accessor :links, :tables, :images, :headings
attr_accessor :links, :tables, :images, :headings, :notes, :help

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

Expand All @@ -39,6 +40,7 @@ def self.option_parser
headings_option parser
images_option parser
tables_option parser
notes_option parser

parser.separator ''
parser.separator 'Common options:'
Expand Down Expand Up @@ -79,5 +81,11 @@ def self.tables_option(parser)
@options.tables = t
end
end

def self.notes_option(parser)
parser.on('-n', '--notes', 'Convert content of HTML notes in the .md files in the given path recursively.') do |n|
@options.notes = n
end
end
end
end
10 changes: 8 additions & 2 deletions lib/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ class Runner
attr_reader :path, :args

def initialize(args)
@args = args
@args = args # return --help if no options were provided
end

EXTENSIONS = ['.md'].freeze

def run
# Parse CLI options
@options = Options.parse(args)
# After the parsing only the ARGV[0] (firtst argument in the CLI) remains in the arrray.
# After the parsing, the ARGV[0] (firtst argument in the CLI) remains in the array only.
# Convert the path to String to use with Find.find

path = args.join

Find.find(path) do |item|
puts "Starting with #{item} ..."
if FileTest.file?(item)
Expand Down Expand Up @@ -56,6 +58,10 @@ def go(file)
@content = reader.all(file)
converted_content = crawler.headings_to_kramdown(@content)
write(file, converted_content)
elsif @options.notes
@content = reader.all(file)
converted_content = crawler.notes_to_kramdown(@content)
write(file, converted_content)
end
end

Expand Down
Loading

0 comments on commit 8ff9740

Please sign in to comment.