reverse_adoc 0.2.5 → 0.2.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/macos.yml +19 -5
- data/.github/workflows/ubuntu.yml +18 -5
- data/.github/workflows/windows.yml +20 -6
- data/Gemfile +2 -2
- data/Gemfile.lock +85 -0
- data/README.adoc +12 -25
- data/Rakefile +1 -1
- data/bin/reverse_adoc +20 -17
- data/bin/w2a +18 -31
- data/lib/reverse_adoc.rb +27 -0
- data/lib/{reverse_asciidoctor → reverse_adoc}/cleaner.rb +27 -4
- data/lib/{reverse_asciidoctor → reverse_adoc}/config.rb +3 -2
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters.rb +6 -6
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/a.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/aside.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/audio.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/base.rb +2 -2
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/blockquote.rb +2 -2
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/br.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/bypass.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/code.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/div.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/drop.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/em.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/figure.rb +1 -1
- data/lib/reverse_adoc/converters/h.rb +34 -0
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/head.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/hr.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/ignore.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/img.rb +27 -29
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/li.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/mark.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/math.rb +3 -3
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/ol.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/p.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/pass_through.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/pre.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/q.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/strong.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/sub.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/sup.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/table.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/td.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/text.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/th.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/tr.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/converters/video.rb +1 -1
- data/lib/{reverse_asciidoctor → reverse_adoc}/errors.rb +1 -1
- data/lib/reverse_adoc/html_converter.rb +56 -0
- data/lib/reverse_adoc/version.rb +3 -0
- data/reverse_adoc.gemspec +4 -2
- data/spec/assets/anchors.html +2 -0
- data/spec/assets/external_images.docx +0 -0
- data/spec/assets/external_images.html +35 -0
- data/spec/bin/reverse_adoc_spec.rb +32 -0
- data/spec/bin/w2a_spec.rb +35 -0
- data/spec/components/anchors_spec.rb +3 -2
- data/spec/components/basic_spec.rb +2 -2
- data/spec/components/code_spec.rb +4 -4
- data/spec/components/escapables_spec.rb +2 -2
- data/spec/components/from_the_wild_spec.rb +2 -2
- data/spec/components/html_fragment_spec.rb +2 -2
- data/spec/components/lists_spec.rb +2 -2
- data/spec/components/paragraphs_spec.rb +2 -2
- data/spec/components/quotation_spec.rb +2 -2
- data/spec/components/tables_spec.rb +2 -2
- data/spec/components/unknown_tags_spec.rb +9 -9
- data/spec/lib/reverse_adoc.rb +90 -0
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/cleaner_spec.rb +28 -4
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/config_spec.rb +5 -5
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/aside_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/audio_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/blockquote_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/br_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/code_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/div_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/figure_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/img_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/li_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/mark_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/p_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/pre_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/q_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/strong_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/text_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters/video_spec.rb +2 -2
- data/spec/lib/{reverse_asciidoctor → reverse_adoc}/converters_spec.rb +5 -5
- data/spec/spec_helper.rb +7 -2
- data/spec/support/shell_helpers.rb +15 -0
- metadata +126 -87
- data/lib/reverse_asciidoctor.rb +0 -70
- data/lib/reverse_asciidoctor/converters/h.rb +0 -19
- data/lib/reverse_asciidoctor/version.rb +0 -3
- data/spec/lib/reverse_asciidoctor_spec.rb +0 -37
data/lib/reverse_adoc.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'digest'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'reverse_adoc/version'
|
6
|
+
require 'reverse_adoc/errors'
|
7
|
+
require 'reverse_adoc/cleaner'
|
8
|
+
require 'reverse_adoc/config'
|
9
|
+
require 'reverse_adoc/converters'
|
10
|
+
require 'reverse_adoc/converters/base'
|
11
|
+
require "reverse_adoc/html_converter"
|
12
|
+
|
13
|
+
module ReverseAdoc
|
14
|
+
def self.convert(input, options = {})
|
15
|
+
ReverseAdoc::HtmlConverter.convert(input, options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.config
|
19
|
+
@config ||= Config.new
|
20
|
+
yield @config if block_given?
|
21
|
+
@config
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.cleaner
|
25
|
+
@cleaner ||= Cleaner.new
|
26
|
+
end
|
27
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module ReverseAdoc
|
2
2
|
class Cleaner
|
3
3
|
|
4
4
|
def tidy(string)
|
@@ -36,19 +36,19 @@ module ReverseAsciidoctor
|
|
36
36
|
# Same for underscores and brackets.
|
37
37
|
def clean_tag_borders(string)
|
38
38
|
result = string.gsub(/\s?\*{2,}.*?\*{2,}\s?/) do |match|
|
39
|
-
preserve_border_whitespaces(match, default_border:
|
39
|
+
preserve_border_whitespaces(match, default_border: ReverseAdoc.config.tag_border) do
|
40
40
|
match.strip.sub('** ', '**').sub(' **', '**')
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
44
|
result = result.gsub(/\s?\_{2,}.*?\_{2,}\s?/) do |match|
|
45
|
-
preserve_border_whitespaces(match, default_border:
|
45
|
+
preserve_border_whitespaces(match, default_border: ReverseAdoc.config.tag_border) do
|
46
46
|
match.strip.sub('__ ', '__').sub(' __', '__')
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
result = result.gsub(/\s?~{2,}.*?~{2,}\s?/) do |match|
|
51
|
-
preserve_border_whitespaces(match, default_border:
|
51
|
+
preserve_border_whitespaces(match, default_border: ReverseAdoc.config.tag_border) do
|
52
52
|
match.strip.sub('~~ ', '~~').sub(' ~~', '~~')
|
53
53
|
end
|
54
54
|
end
|
@@ -64,6 +64,29 @@ module ReverseAsciidoctor
|
|
64
64
|
string.gsub(/(\*\*|~~|__)\s([\.!\?'"])/, "\\1".strip + "\\2")
|
65
65
|
end
|
66
66
|
|
67
|
+
# preprocesses HTML, rather than postprocessing it
|
68
|
+
def preprocess_word_html(string)
|
69
|
+
clean_headings(scrub_whitespace(string.dup))
|
70
|
+
end
|
71
|
+
|
72
|
+
def scrub_whitespace(string)
|
73
|
+
string.gsub!(/ |\ |\u00a0/i, ' ') # HTML encoded spaces
|
74
|
+
string.sub!(/^\A[[:space:]]+/m, '') # document leading whitespace
|
75
|
+
string.sub!(/[[:space:]]+\z$/m, '') # document trailing whitespace
|
76
|
+
string.gsub!(/([ ]+)$/, ' ') # line trailing whitespace
|
77
|
+
string.gsub!(/\n\n\n\n/, "\n\n") # Quadruple line breaks
|
78
|
+
#string.delete!('?| ') # Unicode non-breaking spaces, injected as tabs
|
79
|
+
string
|
80
|
+
end
|
81
|
+
|
82
|
+
# following added by me
|
83
|
+
def clean_headings(string)
|
84
|
+
string.gsub!(%r{<h([1-9])[^>]*></h\1>}, " ") # I don't know why Libre Office is inserting them, but they need to go
|
85
|
+
string.gsub!(%r{<h([1-9])[^>]* style="vertical-align: super;[^>]*>(.+?)</h\1>},
|
86
|
+
"<sup>\\2</sup>") # I absolutely don't know why Libre Office is rendering superscripts as h1
|
87
|
+
string
|
88
|
+
end
|
89
|
+
|
67
90
|
private
|
68
91
|
|
69
92
|
def preserve_border_whitespaces(string, options = {}, &block)
|
@@ -1,12 +1,13 @@
|
|
1
1
|
require 'tmpdir'
|
2
2
|
|
3
|
-
module
|
3
|
+
module ReverseAdoc
|
4
4
|
class Config
|
5
5
|
attr_accessor :unknown_tags, :tag_border, :mathml2asciimath, :external_images,
|
6
|
-
:destination, :sourcedir, :image_counter, :image_counter_pattern
|
6
|
+
:destination, :sourcedir, :image_counter, :image_counter_pattern, :input_format
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
@unknown_tags = :pass_through
|
10
|
+
@input_format = :html
|
10
11
|
@mathml2asciimath = false
|
11
12
|
@external_images = false
|
12
13
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module ReverseAdoc
|
2
2
|
module Converters
|
3
3
|
def self.register(tag_name, converter)
|
4
4
|
@@converters ||= {}
|
@@ -16,17 +16,17 @@ module ReverseAsciidoctor
|
|
16
16
|
private
|
17
17
|
|
18
18
|
def self.default_converter(tag_name)
|
19
|
-
case
|
19
|
+
case ReverseAdoc.config.unknown_tags.to_sym
|
20
20
|
when :pass_through
|
21
|
-
|
21
|
+
ReverseAdoc::Converters::PassThrough.new
|
22
22
|
when :drop
|
23
|
-
|
23
|
+
ReverseAdoc::Converters::Drop.new
|
24
24
|
when :bypass
|
25
|
-
|
25
|
+
ReverseAdoc::Converters::Bypass.new
|
26
26
|
when :raise
|
27
27
|
raise UnknownTagError, "unknown tag: #{tag_name}"
|
28
28
|
else
|
29
|
-
raise InvalidConfigurationError, "unknown value #{
|
29
|
+
raise InvalidConfigurationError, "unknown value #{ReverseAdoc.config.unknown_tags.inspect} for ReverseAdoc.config.unknown_tags"
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module ReverseAdoc
|
2
2
|
module Converters
|
3
3
|
class Base
|
4
4
|
def treat_children(node, state)
|
@@ -8,7 +8,7 @@ module ReverseAsciidoctor
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def treat(node, state)
|
11
|
-
|
11
|
+
ReverseAdoc::Converters.lookup(node.name).convert(node, state)
|
12
12
|
end
|
13
13
|
|
14
14
|
def escape_keychars(string)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module ReverseAdoc
|
2
2
|
module Converters
|
3
3
|
class Blockquote < Base
|
4
4
|
def convert(node, state = {})
|
@@ -7,7 +7,7 @@ module ReverseAsciidoctor
|
|
7
7
|
cite = node['cite']
|
8
8
|
attrs = cite ? "[quote, #{cite}]\n" : ""
|
9
9
|
content = treat_children(node, state).strip
|
10
|
-
content =
|
10
|
+
content = ReverseAdoc.cleaner.remove_newlines(content)
|
11
11
|
#"\n\n> " << content.lines.to_a.join('> ') << "\n\n"
|
12
12
|
"\n\n#{attrs}____\n" << content.lines.to_a.join('') << "\n____\n\n"
|
13
13
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ReverseAdoc
|
2
|
+
module Converters
|
3
|
+
class H < Base
|
4
|
+
def convert(node, state = {})
|
5
|
+
id = node['id']
|
6
|
+
anchor = id ? "[[#{id}]]" : ""
|
7
|
+
internal_anchor = treat_children_anchors(node, state) || ""
|
8
|
+
anchor.empty? and anchor = internal_anchor
|
9
|
+
anchor.empty? or anchor += "\n"
|
10
|
+
prefix = '=' * (node.name[/\d/].to_i + 1)
|
11
|
+
["\n", anchor, prefix, ' ', treat_children_no_anchors(node, state), "\n"].join
|
12
|
+
end
|
13
|
+
|
14
|
+
def treat_children_no_anchors(node, state)
|
15
|
+
node.children.reject { |a| a.name == "a" }.inject('') do |memo, child|
|
16
|
+
memo << treat(child, state)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def treat_children_anchors(node, state)
|
21
|
+
node.children.select { |a| a.name == "a" }.inject('') do |memo, child|
|
22
|
+
memo << treat(child, state)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
register :h1, H.new
|
28
|
+
register :h2, H.new
|
29
|
+
register :h3, H.new
|
30
|
+
register :h4, H.new
|
31
|
+
register :h5, H.new
|
32
|
+
register :h6, H.new
|
33
|
+
end
|
34
|
+
end
|
@@ -1,51 +1,32 @@
|
|
1
1
|
require "fileutils"
|
2
2
|
require "pathname"
|
3
|
+
require "tempfile"
|
4
|
+
require "base64"
|
5
|
+
require "mimemagic"
|
3
6
|
|
4
|
-
module
|
7
|
+
module ReverseAdoc
|
5
8
|
module Converters
|
6
9
|
class Img < Base
|
7
10
|
|
8
11
|
def image_number
|
9
12
|
sprintf(
|
10
|
-
|
11
|
-
|
13
|
+
ReverseAdoc.config.image_counter_pattern,
|
14
|
+
ReverseAdoc.config.image_counter
|
12
15
|
)
|
13
16
|
end
|
14
17
|
|
15
18
|
def image_number_increment
|
16
|
-
|
19
|
+
ReverseAdoc.config.image_counter += 1
|
17
20
|
end
|
18
21
|
|
19
22
|
def datauri2file(src)
|
20
23
|
%r{^data:image/(?<imgtype>[^;]+);base64,(?<imgdata>.+)$} =~ src
|
21
24
|
|
22
|
-
dest_dir = Pathname.new(
|
25
|
+
dest_dir = Pathname.new(ReverseAdoc.config.destination).dirname
|
23
26
|
images_dir = dest_dir + 'images'
|
24
27
|
FileUtils.mkdir_p(images_dir)
|
25
28
|
|
26
|
-
ext =
|
27
|
-
|
28
|
-
if imgdata
|
29
|
-
file = Tempfile.open(["radoc", ".jpg"]) do |f|
|
30
|
-
begin
|
31
|
-
f.binmode
|
32
|
-
f.write(Base64.strict_decode64(imgdata))
|
33
|
-
f.rewind
|
34
|
-
ext = MimeMagic.by_magic(f)
|
35
|
-
ensure
|
36
|
-
f.close!
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
image_src_path = file.path
|
41
|
-
# puts "tempfile: #{file}"
|
42
|
-
|
43
|
-
else
|
44
|
-
ext = File.extname(src).strip.downcase[1..-1]
|
45
|
-
image_src_path = Pathname.new(ReverseAsciidoctor.config.sourcedir) + src
|
46
|
-
|
47
|
-
end
|
48
|
-
|
29
|
+
ext, image_src_path = determine_image_src_path(src, imgdata)
|
49
30
|
image_dest_path = images_dir + "#{image_number}.#{ext}"
|
50
31
|
|
51
32
|
# puts "image_dest_path: #{image_dest_path.to_s}"
|
@@ -57,6 +38,23 @@ module ReverseAsciidoctor
|
|
57
38
|
image_dest_path.relative_path_from(dest_dir)
|
58
39
|
end
|
59
40
|
|
41
|
+
def determine_image_src_path(src, imgdata)
|
42
|
+
return copy_temp_file(imgdata) if imgdata
|
43
|
+
|
44
|
+
ext = File.extname(src).strip.downcase[1..-1]
|
45
|
+
[ext, Pathname.new(ReverseAdoc.config.sourcedir) + src]
|
46
|
+
end
|
47
|
+
|
48
|
+
def copy_temp_file(imgdata)
|
49
|
+
Tempfile.open(['radoc', '.jpg']) do |f|
|
50
|
+
f.binmode
|
51
|
+
f.write(Base64.strict_decode64(imgdata))
|
52
|
+
f.rewind
|
53
|
+
ext = MimeMagic.by_magic(f).subtype
|
54
|
+
[ext, f.path]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
60
58
|
def convert(node, state = {})
|
61
59
|
alt = node['alt']
|
62
60
|
src = node['src']
|
@@ -66,7 +64,7 @@ module ReverseAsciidoctor
|
|
66
64
|
anchor = id ? "[[#{id}]]\n" : ""
|
67
65
|
title = extract_title(node)
|
68
66
|
|
69
|
-
if
|
67
|
+
if ReverseAdoc.config.external_images
|
70
68
|
# puts "external image conversion #{id}, #{src}"
|
71
69
|
src = datauri2file(src)
|
72
70
|
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
# Unless run with
|
1
|
+
# Unless run with ReverseAdoc.config.mathml2asciimath,
|
2
2
|
# this is cheating: we're injecting MathML into Asciidoctor, but
|
3
3
|
# Asciidoctor only understands AsciiMath or LaTeX
|
4
4
|
|
5
5
|
require "mathml2asciimath"
|
6
6
|
|
7
|
-
module
|
7
|
+
module ReverseAdoc
|
8
8
|
module Converters
|
9
9
|
class Math < Base
|
10
10
|
def convert(node, state = {})
|
11
11
|
stem = node.to_s.gsub(/\n/, " ")
|
12
|
-
stem = MathML2AsciiMath.m2a(stem) if
|
12
|
+
stem = MathML2AsciiMath.m2a(stem) if ReverseAdoc.config.mathml2asciimath
|
13
13
|
stem = stem.gsub(/\[/, "\\[").gsub(/\]/, "\\]").gsub(/\(\(([^\)]+)\)\)/, "(\\1)") unless stem.nil?
|
14
14
|
" stem:[" << stem << "] "
|
15
15
|
end
|