reverse_adoc 0.2.6 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rake.yml +42 -0
- data/Gemfile +2 -2
- data/README.adoc +6 -22
- data/Rakefile +1 -1
- data/bin/reverse_adoc +17 -17
- data/bin/w2a +17 -17
- data/lib/reverse_adoc.rb +27 -0
- data/lib/{reverse_asciidoctor → reverse_adoc}/cleaner.rb +4 -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_asciidoctor → reverse_adoc}/converters/h.rb +1 -1
- 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 +11 -11
- 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 +5 -4
- data/spec/assets/{test.docx → external_images.docx} +0 -0
- data/spec/assets/{test.html → external_images.html} +0 -0
- data/spec/bin/reverse_adoc_spec.rb +32 -0
- data/spec/bin/w2a_spec.rb +35 -0
- data/spec/components/anchors_spec.rb +2 -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 +4 -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 +119 -101
- data/.github/workflows/macos.yml +0 -27
- data/.github/workflows/ubuntu.yml +0 -27
- data/.github/workflows/windows.yml +0 -30
- data/lib/reverse_asciidoctor.rb +0 -70
- data/lib/reverse_asciidoctor/version.rb +0 -3
- data/spec/lib/reverse_asciidoctor_spec.rb +0 -37
@@ -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
|
@@ -2,31 +2,31 @@ require "fileutils"
|
|
2
2
|
require "pathname"
|
3
3
|
require "tempfile"
|
4
4
|
require "base64"
|
5
|
-
require "
|
5
|
+
require "marcel"
|
6
6
|
|
7
|
-
module
|
7
|
+
module ReverseAdoc
|
8
8
|
module Converters
|
9
9
|
class Img < Base
|
10
10
|
|
11
11
|
def image_number
|
12
12
|
sprintf(
|
13
|
-
|
14
|
-
|
13
|
+
ReverseAdoc.config.image_counter_pattern,
|
14
|
+
ReverseAdoc.config.image_counter
|
15
15
|
)
|
16
16
|
end
|
17
17
|
|
18
18
|
def image_number_increment
|
19
|
-
|
19
|
+
ReverseAdoc.config.image_counter += 1
|
20
20
|
end
|
21
21
|
|
22
22
|
def datauri2file(src)
|
23
23
|
%r{^data:image/(?<imgtype>[^;]+);base64,(?<imgdata>.+)$} =~ src
|
24
24
|
|
25
|
-
dest_dir = Pathname.new(
|
25
|
+
dest_dir = Pathname.new(ReverseAdoc.config.destination).dirname
|
26
26
|
images_dir = dest_dir + 'images'
|
27
27
|
FileUtils.mkdir_p(images_dir)
|
28
28
|
|
29
|
-
ext, image_src_path = determine_image_src_path(imgdata)
|
29
|
+
ext, image_src_path = determine_image_src_path(src, imgdata)
|
30
30
|
image_dest_path = images_dir + "#{image_number}.#{ext}"
|
31
31
|
|
32
32
|
# puts "image_dest_path: #{image_dest_path.to_s}"
|
@@ -38,11 +38,11 @@ module ReverseAsciidoctor
|
|
38
38
|
image_dest_path.relative_path_from(dest_dir)
|
39
39
|
end
|
40
40
|
|
41
|
-
def determine_image_src_path(imgdata)
|
41
|
+
def determine_image_src_path(src, imgdata)
|
42
42
|
return copy_temp_file(imgdata) if imgdata
|
43
43
|
|
44
44
|
ext = File.extname(src).strip.downcase[1..-1]
|
45
|
-
[ext, Pathname.new(
|
45
|
+
[ext, Pathname.new(ReverseAdoc.config.sourcedir) + src]
|
46
46
|
end
|
47
47
|
|
48
48
|
def copy_temp_file(imgdata)
|
@@ -50,7 +50,7 @@ module ReverseAsciidoctor
|
|
50
50
|
f.binmode
|
51
51
|
f.write(Base64.strict_decode64(imgdata))
|
52
52
|
f.rewind
|
53
|
-
ext =
|
53
|
+
ext = Marcel::MimeType.for(f).sub(%r{^[^/]+/}, "")
|
54
54
|
[ext, f.path]
|
55
55
|
end
|
56
56
|
end
|
@@ -64,7 +64,7 @@ module ReverseAsciidoctor
|
|
64
64
|
anchor = id ? "[[#{id}]]\n" : ""
|
65
65
|
title = extract_title(node)
|
66
66
|
|
67
|
-
if
|
67
|
+
if ReverseAdoc.config.external_images
|
68
68
|
# puts "external image conversion #{id}, #{src}"
|
69
69
|
src = datauri2file(src)
|
70
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
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'reverse_adoc/converters/a'
|
4
|
+
require 'reverse_adoc/converters/aside'
|
5
|
+
require 'reverse_adoc/converters/audio'
|
6
|
+
require 'reverse_adoc/converters/blockquote'
|
7
|
+
require 'reverse_adoc/converters/br'
|
8
|
+
require 'reverse_adoc/converters/bypass'
|
9
|
+
require 'reverse_adoc/converters/code'
|
10
|
+
require 'reverse_adoc/converters/div'
|
11
|
+
require 'reverse_adoc/converters/drop'
|
12
|
+
require 'reverse_adoc/converters/em'
|
13
|
+
require 'reverse_adoc/converters/figure'
|
14
|
+
require 'reverse_adoc/converters/h'
|
15
|
+
require 'reverse_adoc/converters/head'
|
16
|
+
require 'reverse_adoc/converters/hr'
|
17
|
+
require 'reverse_adoc/converters/ignore'
|
18
|
+
require 'reverse_adoc/converters/img'
|
19
|
+
require 'reverse_adoc/converters/mark'
|
20
|
+
require 'reverse_adoc/converters/li'
|
21
|
+
require 'reverse_adoc/converters/ol'
|
22
|
+
require 'reverse_adoc/converters/p'
|
23
|
+
require 'reverse_adoc/converters/pass_through'
|
24
|
+
require 'reverse_adoc/converters/pre'
|
25
|
+
require 'reverse_adoc/converters/q'
|
26
|
+
require 'reverse_adoc/converters/strong'
|
27
|
+
require 'reverse_adoc/converters/sup'
|
28
|
+
require 'reverse_adoc/converters/sub'
|
29
|
+
require 'reverse_adoc/converters/table'
|
30
|
+
require 'reverse_adoc/converters/td'
|
31
|
+
require 'reverse_adoc/converters/th'
|
32
|
+
require 'reverse_adoc/converters/text'
|
33
|
+
require 'reverse_adoc/converters/tr'
|
34
|
+
require 'reverse_adoc/converters/video'
|
35
|
+
require 'reverse_adoc/converters/math'
|
36
|
+
|
37
|
+
module ReverseAdoc
|
38
|
+
class HtmlConverter
|
39
|
+
def self.convert(input, options = {})
|
40
|
+
root = if input.is_a?(String)
|
41
|
+
then Nokogiri::HTML(input).root
|
42
|
+
elsif input.is_a?(Nokogiri::XML::Document)
|
43
|
+
then input.root
|
44
|
+
elsif input.is_a?(Nokogiri::XML::Node)
|
45
|
+
then input
|
46
|
+
end
|
47
|
+
|
48
|
+
root || (return '')
|
49
|
+
|
50
|
+
ReverseAdoc.config.with(options) do
|
51
|
+
result = ReverseAdoc::Converters.lookup(root.name).convert(root)
|
52
|
+
ReverseAdoc.cleaner.tidy(result)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/reverse_adoc.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "
|
3
|
+
require "reverse_adoc/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "reverse_adoc"
|
7
|
-
s.version =
|
7
|
+
s.version = ReverseAdoc::VERSION
|
8
8
|
s.authors = ["Ribose Inc."]
|
9
9
|
s.email = ["open.source@ribose.com"]
|
10
10
|
|
@@ -20,9 +20,9 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
21
21
|
|
22
22
|
# specify any dependencies here; for example:
|
23
|
-
s.add_dependency 'nokogiri', "
|
23
|
+
s.add_dependency 'nokogiri', "~> 1.10.4"
|
24
24
|
s.add_dependency 'mathml2asciimath'
|
25
|
-
s.add_dependency '
|
25
|
+
s.add_dependency 'marcel', "~> 1.0.0"
|
26
26
|
s.add_development_dependency 'rspec'
|
27
27
|
s.add_development_dependency 'simplecov'
|
28
28
|
s.add_development_dependency 'rake'
|
@@ -33,4 +33,5 @@ Gem::Specification.new do |s|
|
|
33
33
|
|
34
34
|
# All the following are for bin/w2m
|
35
35
|
s.add_dependency 'word-to-markdown'
|
36
|
+
s.add_dependency 'premailer', '~> 1.11.0'
|
36
37
|
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'bin/revrse_adoc' do
|
6
|
+
subject(:convert) do
|
7
|
+
ShellUtils.execute!("./bin/reverse_adoc -e -o test1 #{input_file_path}")
|
8
|
+
end
|
9
|
+
unless Gem.win_platform?
|
10
|
+
context 'when external images present' do
|
11
|
+
let(:input_file_path) { 'spec/assets/external_images.html' }
|
12
|
+
let(:images_folder) { 'images' }
|
13
|
+
|
14
|
+
after do
|
15
|
+
FileUtils.rm_rf(images_folder) if File.directory?(images_folder)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'Does not raise error' do
|
19
|
+
expect { convert }.to_not raise_error
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'exatracts images from source html' do
|
23
|
+
expect { convert }
|
24
|
+
.to(change do
|
25
|
+
Dir["#{images_folder}/*gif"]
|
26
|
+
.map { |entry| File.basename(entry) }
|
27
|
+
.sort
|
28
|
+
end.from([]).to(['001.gif']))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'bin/w2a' do
|
6
|
+
subject(:convert) do
|
7
|
+
ShellUtils.execute!("./bin/w2a -e -o test1 #{input_file_path}")
|
8
|
+
end
|
9
|
+
# TODO: fix github actions integration with libreoffice, currently it hangs
|
10
|
+
# when trying to use soffice binary
|
11
|
+
unless Gem.win_platform? ||
|
12
|
+
(Gem::Platform.local.os == 'darwin' && !ENV['GITHUB_ACTION'].nil?)
|
13
|
+
context 'when external images present' do
|
14
|
+
let(:input_file_path) { 'spec/assets/external_images.docx' }
|
15
|
+
let(:images_folder) { 'images' }
|
16
|
+
|
17
|
+
after do
|
18
|
+
FileUtils.rm_rf(images_folder) if File.directory?(images_folder)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'Does not raise error' do
|
22
|
+
expect { convert }.to_not raise_error
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'extracts images from source html' do
|
26
|
+
expect { convert }
|
27
|
+
.to(change do
|
28
|
+
Dir["#{images_folder}/*gif"]
|
29
|
+
.map { |entry| File.basename(entry) }
|
30
|
+
.sort
|
31
|
+
end.from([]).to(['001.gif']))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|