coradoc 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +35 -0
  3. data/{.docker/readme.md → README.Docker.adoc} +21 -25
  4. data/README.adoc +121 -0
  5. data/coradoc.gemspec +4 -0
  6. data/docker-compose.yml +14 -0
  7. data/exe/coradoc +5 -0
  8. data/exe/reverse_adoc +24 -34
  9. data/exe/w2a +16 -28
  10. data/lib/coradoc/cli.rb +105 -0
  11. data/lib/coradoc/converter.rb +145 -0
  12. data/lib/coradoc/document.rb +13 -0
  13. data/lib/coradoc/element/admonition.rb +5 -0
  14. data/lib/coradoc/element/attribute_list.rb +1 -1
  15. data/lib/coradoc/element/author.rb +4 -4
  16. data/lib/coradoc/element/bibliography.rb +24 -0
  17. data/lib/coradoc/element/bibliography_entry.rb +24 -0
  18. data/lib/coradoc/element/block/core.rb +6 -4
  19. data/lib/coradoc/element/block/example.rb +1 -0
  20. data/lib/coradoc/element/block/pass.rb +21 -0
  21. data/lib/coradoc/element/block/quote.rb +1 -1
  22. data/lib/coradoc/element/block/reviewer_comment.rb +19 -0
  23. data/lib/coradoc/element/block/side.rb +4 -2
  24. data/lib/coradoc/element/block/sourcecode.rb +1 -0
  25. data/lib/coradoc/element/block.rb +2 -0
  26. data/lib/coradoc/element/comment_block.rb +22 -0
  27. data/lib/coradoc/element/comment_line.rb +18 -0
  28. data/lib/coradoc/element/document_attributes.rb +8 -1
  29. data/lib/coradoc/element/header.rb +1 -1
  30. data/lib/coradoc/element/image/block_image.rb +8 -0
  31. data/lib/coradoc/element/image/core.rb +6 -5
  32. data/lib/coradoc/element/include.rb +18 -0
  33. data/lib/coradoc/element/inline/citation.rb +24 -0
  34. data/lib/coradoc/element/inline/cross_reference.rb +29 -8
  35. data/lib/coradoc/element/inline.rb +1 -0
  36. data/lib/coradoc/element/list/core.rb +10 -2
  37. data/lib/coradoc/element/list_item.rb +7 -5
  38. data/lib/coradoc/element/paragraph.rb +8 -5
  39. data/lib/coradoc/element/revision.rb +1 -1
  40. data/lib/coradoc/element/section.rb +6 -4
  41. data/lib/coradoc/element/table.rb +2 -2
  42. data/lib/coradoc/element/tag.rb +19 -0
  43. data/lib/coradoc/element/term.rb +21 -0
  44. data/lib/coradoc/element/text_element.rb +9 -1
  45. data/lib/coradoc/element/title.rb +1 -1
  46. data/lib/coradoc/generator.rb +2 -0
  47. data/lib/coradoc/input/adoc.rb +28 -0
  48. data/lib/coradoc/input/docx.rb +35 -0
  49. data/lib/coradoc/{reverse_adoc → input/html}/README.adoc +9 -9
  50. data/lib/coradoc/{reverse_adoc → input/html}/cleaner.rb +18 -4
  51. data/lib/coradoc/input/html/config.rb +75 -0
  52. data/lib/coradoc/{reverse_adoc → input/html}/converters/a.rb +1 -1
  53. data/lib/coradoc/{reverse_adoc → input/html}/converters/aside.rb +1 -1
  54. data/lib/coradoc/{reverse_adoc → input/html}/converters/audio.rb +1 -1
  55. data/lib/coradoc/{reverse_adoc → input/html}/converters/base.rb +1 -1
  56. data/lib/coradoc/{reverse_adoc → input/html}/converters/blockquote.rb +2 -2
  57. data/lib/coradoc/{reverse_adoc → input/html}/converters/br.rb +1 -1
  58. data/lib/coradoc/{reverse_adoc → input/html}/converters/bypass.rb +1 -1
  59. data/lib/coradoc/{reverse_adoc → input/html}/converters/code.rb +1 -1
  60. data/lib/coradoc/{reverse_adoc → input/html}/converters/div.rb +1 -1
  61. data/lib/coradoc/{reverse_adoc → input/html}/converters/dl.rb +1 -1
  62. data/lib/coradoc/{reverse_adoc → input/html}/converters/drop.rb +1 -1
  63. data/lib/coradoc/{reverse_adoc → input/html}/converters/em.rb +1 -1
  64. data/lib/coradoc/{reverse_adoc → input/html}/converters/figure.rb +1 -1
  65. data/lib/coradoc/{reverse_adoc → input/html}/converters/h.rb +1 -1
  66. data/lib/coradoc/{reverse_adoc → input/html}/converters/head.rb +1 -1
  67. data/lib/coradoc/{reverse_adoc → input/html}/converters/hr.rb +1 -1
  68. data/lib/coradoc/{reverse_adoc → input/html}/converters/ignore.rb +1 -1
  69. data/lib/coradoc/{reverse_adoc → input/html}/converters/img.rb +7 -7
  70. data/lib/coradoc/{reverse_adoc → input/html}/converters/li.rb +1 -1
  71. data/lib/coradoc/{reverse_adoc → input/html}/converters/mark.rb +1 -1
  72. data/lib/coradoc/{reverse_adoc → input/html}/converters/markup.rb +1 -1
  73. data/lib/coradoc/{reverse_adoc → input/html}/converters/math.rb +3 -3
  74. data/lib/coradoc/{reverse_adoc → input/html}/converters/ol.rb +1 -1
  75. data/lib/coradoc/{reverse_adoc → input/html}/converters/p.rb +1 -1
  76. data/lib/coradoc/{reverse_adoc → input/html}/converters/pass_through.rb +1 -1
  77. data/lib/coradoc/{reverse_adoc → input/html}/converters/pre.rb +1 -1
  78. data/lib/coradoc/{reverse_adoc → input/html}/converters/q.rb +1 -1
  79. data/lib/coradoc/{reverse_adoc → input/html}/converters/strong.rb +1 -1
  80. data/lib/coradoc/{reverse_adoc → input/html}/converters/sub.rb +1 -1
  81. data/lib/coradoc/{reverse_adoc → input/html}/converters/sup.rb +1 -1
  82. data/lib/coradoc/{reverse_adoc → input/html}/converters/table.rb +4 -4
  83. data/lib/coradoc/{reverse_adoc → input/html}/converters/td.rb +1 -1
  84. data/lib/coradoc/{reverse_adoc → input/html}/converters/text.rb +2 -2
  85. data/lib/coradoc/{reverse_adoc → input/html}/converters/th.rb +1 -1
  86. data/lib/coradoc/{reverse_adoc → input/html}/converters/tr.rb +1 -1
  87. data/lib/coradoc/{reverse_adoc → input/html}/converters/video.rb +1 -1
  88. data/lib/coradoc/input/html/converters.rb +57 -0
  89. data/lib/coradoc/input/html/errors.rb +12 -0
  90. data/lib/coradoc/{reverse_adoc → input/html}/html_converter.rb +37 -22
  91. data/lib/coradoc/{reverse_adoc → input/html}/plugin.rb +6 -6
  92. data/lib/coradoc/{reverse_adoc → input/html}/plugins/plateau.rb +3 -3
  93. data/lib/coradoc/{reverse_adoc → input/html}/postprocessor.rb +3 -3
  94. data/lib/coradoc/input/html.rb +59 -0
  95. data/lib/coradoc/input.rb +12 -0
  96. data/lib/coradoc/output/adoc.rb +17 -0
  97. data/lib/coradoc/output/coradoc_tree_debug.rb +19 -0
  98. data/lib/coradoc/output.rb +11 -0
  99. data/lib/coradoc/parser/asciidoc/admonition.rb +24 -0
  100. data/lib/coradoc/parser/asciidoc/attribute_list.rb +67 -0
  101. data/lib/coradoc/parser/asciidoc/base.rb +101 -13
  102. data/lib/coradoc/parser/asciidoc/bibliography.rb +30 -0
  103. data/lib/coradoc/parser/asciidoc/block.rb +82 -0
  104. data/lib/coradoc/parser/asciidoc/citation.rb +48 -0
  105. data/lib/coradoc/parser/asciidoc/content.rb +15 -120
  106. data/lib/coradoc/parser/asciidoc/document_attributes.rb +12 -5
  107. data/lib/coradoc/parser/asciidoc/header.rb +1 -4
  108. data/lib/coradoc/parser/asciidoc/inline.rb +72 -0
  109. data/lib/coradoc/parser/asciidoc/list.rb +81 -0
  110. data/lib/coradoc/parser/asciidoc/paragraph.rb +33 -0
  111. data/lib/coradoc/parser/asciidoc/section.rb +36 -31
  112. data/lib/coradoc/parser/asciidoc/table.rb +32 -0
  113. data/lib/coradoc/parser/asciidoc/term.rb +23 -0
  114. data/lib/coradoc/parser/base.rb +39 -4
  115. data/lib/coradoc/transformer.rb +353 -82
  116. data/lib/coradoc/util.rb +1 -1
  117. data/lib/coradoc/version.rb +1 -1
  118. data/lib/coradoc.rb +8 -5
  119. data/lib/reverse_adoc.rb +6 -6
  120. data/utils/parser_analyzer.rb +66 -0
  121. data/utils/round_trip.rb +37 -0
  122. metadata +112 -54
  123. data/.docker/Makefile +0 -35
  124. data/.docker/docker-compose.yml +0 -14
  125. data/Makefile +0 -1
  126. data/README.md +0 -73
  127. data/docker-compose.yml +0 -1
  128. data/lib/coradoc/reverse_adoc/config.rb +0 -73
  129. data/lib/coradoc/reverse_adoc/converters.rb +0 -55
  130. data/lib/coradoc/reverse_adoc/errors.rb +0 -10
  131. data/lib/coradoc/reverse_adoc.rb +0 -30
  132. /data/{.docker/Dockerfile → Dockerfile} +0 -0
  133. /data/lib/coradoc/{reverse_adoc → input/html}/LICENSE.txt +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e44c5e565e224487496ecdd7be8c5c88e5f05fd2a42a4f2cbf31746a908aa25
4
- data.tar.gz: a1e8fb651b29b516071e91c5c6f7bf72a68c34fd4ae9512a928d3ea3185bc0c6
3
+ metadata.gz: '0187fda1ca8860383e973840ca92de0d24b7de0f4b548eb92364b83dc76773c2'
4
+ data.tar.gz: bcccecebdf83ef11138182fc66ad27f5e24bb01d0b2824fa8b470d3c32ba413a
5
5
  SHA512:
6
- metadata.gz: 4df0a3edcebe6990006c7ca951b59f717f05d9b25eb6481394fe276dbaa318f6016e7a915d268abc19d61cec0605ab7d44466662d499cfa08a0ffa2f98bd8dcc
7
- data.tar.gz: 9a44116b58e7e99a14ae856b1b06e23c26cbe2913f970a6849615282c3f8e33b05c538c39c72a827fa65aef30f56c9eb494d2f48574ea01f86bca858d9692e16
6
+ metadata.gz: 37f09a7b2ba8fc861c4cf88f8c5638e6c654cc19a65b1b6c68314c6d9ff1b45f046559d573a849ee972b4dfa0422384271c1951ce5baa02491dc1c03000c2e77
7
+ data.tar.gz: c6844f3ccad625662b838cba656fa6e1f773675548d4fe664278f93569f7cf0ae86edcd70e26de2e60cb487090d3873720bc0c6e0f6892f8559e77a78129af4d
data/Makefile ADDED
@@ -0,0 +1,35 @@
1
+ export SPEC ?= spec
2
+ SPEC_FILE = $(subst ../,, $(SPEC))
3
+ export RUBY_IMAGE ?= ruby:3.1.2-slim
4
+
5
+ .PHONY: up
6
+ up:
7
+ docker-compose up
8
+
9
+ .PHONY: down
10
+ down:
11
+ docker-compose down
12
+
13
+ .PHONY: test
14
+ test: rspec
15
+
16
+ .PHONY: ssh
17
+ ssh:
18
+ docker-compose run lib bash
19
+
20
+ .PHONY: install
21
+ install:
22
+ docker-compose run lib bin/setup
23
+
24
+ .PHONY: console
25
+ console:
26
+ docker-compose run lib bin/console
27
+
28
+ .PHONY: rspec
29
+ rspec:
30
+ docker-compose run lib bin/rspec ${SPEC_FILE}
31
+
32
+ .PHONY: setup
33
+ setup:
34
+ docker-compose build --build-arg RUBY_IMAGE=${RUBY_IMAGE}
35
+ docker-compose run lib bin/setup
@@ -1,61 +1,57 @@
1
- ## Docker
1
+ = Docker
2
2
 
3
- This directory is only meant to be used for development, and contains some
3
+ This Docker image is only meant to be used for development, and contains some
4
4
  necessary setup to spin up docker containers with multiple ruby environment.
5
5
 
6
- ### Setup
6
+ == Setup
7
7
 
8
- Before doing anything, you might want to create a symlink to the docker file and
9
- Makefile. This would allow you to avoid some of the unnecessary work related to
10
- the file paths To do that run the following from the root of the project.
11
-
12
- ```
13
- ln -sf .docker/Makefile .
14
- ln -sf .docker/docker-compose.yml .
15
- ```
16
-
17
- By default it usages the most recent ruby version for docker environment, but if
8
+ By default it uses the most recent ruby version for docker environment, but if
18
9
  you want to run it in any specific version then you can set it up by exporting
19
10
  `RUBY_IMAGE` environment variable in your shell:
20
11
 
21
- ```sh
12
+ [source,sh]
13
+ ----
22
14
  export RUBY_IMAGE=ruby:3.0-buster
23
- ```
15
+ ----
24
16
 
25
17
  Once everything is set then you would need to build the development images for
26
18
  the first time and you can do that using:
27
19
 
28
- ```sh
20
+ [source,sh]
21
+ ----
29
22
  make setup
30
- ```
23
+ ----
31
24
 
32
25
  The setup process will install all dependencies and it will also setup a volume
33
26
  to speed up the repeated gem installation.
34
27
 
35
- ### Playground
28
+ == Playground
36
29
 
37
30
  The `Makefile` contains two target for tests, and you can run the tests using
38
31
  any of the following commands:
39
32
 
40
- ```sh
33
+ [source,sh]
34
+ ----
41
35
  make test
42
36
 
43
37
  # or
44
38
  make rspec
45
- ```
39
+ ----
46
40
 
47
41
  If you need more control, and you want to do some development on the go then you
48
42
  can get into the container using:
49
43
 
50
- ```sh
44
+ [source,sh]
45
+ ----
51
46
  make ssh
52
- ```
47
+ ----
53
48
 
54
- ### Cleanup
49
+ == Cleanup
55
50
 
56
51
  Once you are done with your experiment then you can cleanup the docker
57
52
  environment using the following command.
58
53
 
59
- ```sh
54
+ [source,sh]
55
+ ----
60
56
  make down
61
- ```
57
+ ----
data/README.adoc ADDED
@@ -0,0 +1,121 @@
1
+ :sandi-metz: footnote:[http://robots.thoughtbot.com/post/50655960596/sandi-metz-rules-for-developers]
2
+
3
+ = Coradoc
4
+
5
+ Coradoc is a modern Parser for Asciidoc document. It defines a grammar for
6
+ AsciiDoc, and then build the Parser for that grammar.
7
+
8
+ Once the document is parsed, it provides a pure ruby object `Coradoc::Document`,
9
+ which can used to customize the document in easiest way.
10
+
11
+ == Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ [source,ruby]
16
+ ----
17
+ gem "coradoc"
18
+ ----
19
+
20
+ And then execute:
21
+
22
+ [source,sh]
23
+ ----
24
+ bundle install
25
+ ----
26
+
27
+ Or install it yourself as:
28
+
29
+ [source,sh]
30
+ ----
31
+ gem install coradoc
32
+ ----
33
+
34
+ == Development
35
+
36
+ We are following Sandi Metz's Rules for this gem, you can read the
37
+ description of the rules here.{sandi-metz} All new code should follow these
38
+ rules. If you make changes in a pre-existing file that violates these rules you
39
+ should fix the violations as part of your contribution.
40
+
41
+ == Setup
42
+
43
+ Clone the repository.
44
+
45
+ [source,sh]
46
+ ----
47
+ git clone https://github.com/metanorma/coradoc.git
48
+ ----
49
+
50
+ Setup your environment in docker
51
+
52
+ [source,sh]
53
+ ----
54
+ make setup
55
+ ----
56
+
57
+ Run the test suite
58
+
59
+ [source,sh]
60
+ ----
61
+ make test
62
+ ----
63
+
64
+
65
+ == Usage from command line
66
+
67
+ === Converting a document
68
+
69
+ [source,sh]
70
+ ----
71
+ $ coradoc help convert
72
+ $ coradoc convert file.html -o file.adoc
73
+ ----
74
+
75
+ == Usage from Ruby
76
+
77
+ === Parsing a document
78
+
79
+ To parse any AsciiDoc, we can use the following:
80
+
81
+ [source,ruby]
82
+ ----
83
+ Coradoc::Parser.parse(sample_asciidoc)
84
+ ----
85
+
86
+ This interface will return the abstract syntax tree.
87
+
88
+ === Converting a document
89
+
90
+ To convert any document of a supported format (right now: `.html`, `.adoc`, `.docx`) to any supported
91
+ format (right now: `.adoc`), you can execute:
92
+
93
+ [source,ruby]
94
+ ----
95
+ Coradoc::Converter.("input.html", "output.adoc")
96
+ ----
97
+
98
+ The converters are chosen based on file extension, but you can select a converter manually like so:
99
+
100
+ [source,ruby]
101
+ ----
102
+ Coradoc::Converter.("input", "output", input_processor: :html, output_processor: :adoc)
103
+ ----
104
+
105
+ Some converters may support additional options, which can likewise be passed as keyword arguments:
106
+
107
+ [source,ruby]
108
+ ----
109
+ Coradoc::Converter.(
110
+ "input.html", "output.adoc",
111
+ input_options: { external_images: true, split_sections: 2 }
112
+ )
113
+ ----
114
+
115
+ It is also possible to pass IO objects instead of filenames. By default, if an argument is not
116
+ provided, it defaults to STDIN/STDOUT. Note that not all combinations of formats and converter
117
+ options are supported in this mode.
118
+
119
+ === Legacy README for converting from HTML to AsciiDoc (formerly reverse_adoc)
120
+
121
+ See: https://github.com/metanorma/coradoc/blob/main/lib/coradoc/input/html/README.adoc[Coradoc::Input::HTML README]
data/coradoc.gemspec CHANGED
@@ -35,6 +35,8 @@ Gem::Specification.new do |spec|
35
35
  spec.add_dependency "parslet"
36
36
  spec.add_dependency "premailer", "~> 1.11.0"
37
37
  spec.add_dependency "word-to-markdown"
38
+ spec.add_dependency "base64"
39
+ spec.add_dependency "thor"
38
40
  spec.add_development_dependency "codeclimate-test-reporter"
39
41
  spec.add_development_dependency "pry"
40
42
  spec.add_development_dependency "rake"
@@ -43,5 +45,7 @@ Gem::Specification.new do |spec|
43
45
  spec.add_development_dependency "rubocop"
44
46
  spec.add_development_dependency "rubocop-performance"
45
47
  spec.add_development_dependency "simplecov"
48
+ # spec.add_development_dependency "parallel_tests"
49
+ # spec.add_development_dependency "stackprof"
46
50
  # spec.add_runtime_dependency "thor"
47
51
  end
@@ -0,0 +1,14 @@
1
+ version: "3"
2
+
3
+ services:
4
+ lib:
5
+ build:
6
+ context: .
7
+ dockerfile: ./Dockerfile
8
+
9
+ volumes:
10
+ - .:/workspace:z
11
+ - bundle:/bundle:z
12
+
13
+ volumes:
14
+ bundle:
data/exe/coradoc ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "coradoc/cli"
4
+
5
+ Coradoc::CLI.start
data/exe/reverse_adoc CHANGED
@@ -4,28 +4,37 @@
4
4
  require "rubygems"
5
5
  require "bundler/setup"
6
6
 
7
- require "coradoc/reverse_adoc"
7
+ require "coradoc/input/html"
8
+ require "coradoc/converter"
8
9
  require "optparse"
9
10
  require "fileutils"
10
11
 
12
+ config = {
13
+ input_options: input_options = {},
14
+ input_processor: :html,
15
+ output_options: output_options = {},
16
+ output_processor: :adoc,
17
+ }
18
+ destination = nil
19
+
11
20
  OptionParser.new do |opts|
12
21
  opts.banner = "Usage: reverse_adoc [options] <file>"
13
22
  opts.on("-m", "--mathml2asciimath", "Convert MathML to AsciiMath") do |_v|
14
- Coradoc::ReverseAdoc.config.mathml2asciimath = true
23
+ input_options[:mathml2asciimath] = true
15
24
  end
16
25
 
17
26
  opts.on("-oFILENAME", "--output=FILENAME", "Output file to write to") do |v|
18
- Coradoc::ReverseAdoc.config.destination = File.expand_path(v)
19
- # puts "output goes to #{Coradoc::ReverseAdoc.config.destination}"
27
+ destination = File.expand_path(v)
28
+ # puts "output goes to #{Coradoc::Input::HTML.config.destination}"
20
29
  end
21
30
 
22
31
  opts.on("-e", "--external-images", "Export images if data URI") do |_v|
23
- Coradoc::ReverseAdoc.config.external_images = true
32
+ input_options[:external_images] = true
24
33
  end
25
34
 
26
35
  opts.on("-u", "--unknown_tags [pass_through, drop, bypass, raise]",
27
36
  "Unknown tag handling (default: pass_through)") do |v|
28
- Coradoc::ReverseAdoc.config.unknown_tags = v
37
+ input_options[:unknown_tags] = v
29
38
  end
30
39
 
31
40
  opts.on("-r", "--require RUBYMODULE", "Require additional Ruby file") do |v|
@@ -33,15 +42,15 @@ OptionParser.new do |opts|
33
42
  end
34
43
 
35
44
  opts.on("--track-time", "Track time spent on each step") do
36
- Coradoc::ReverseAdoc.config.track_time = true
45
+ input_options[:track_time] = true
37
46
  end
38
47
 
39
48
  opts.on("--split-sections LEVEL", "Split sections up to LEVEL") do |i|
40
- Coradoc::ReverseAdoc.config.split_sections = i.to_i
49
+ input_options[:split_sections] = i.to_i
41
50
  end
42
51
 
43
52
  opts.on("-v", "--version", "Version information") do |_v|
44
- puts "reverse_adoc: v#{Coradoc::ReverseAdoc::VERSION}"
53
+ puts "Coradoc: v#{Coradoc::VERSION}"
45
54
  exit
46
55
  end
47
56
 
@@ -52,40 +61,21 @@ OptionParser.new do |opts|
52
61
  end.parse!
53
62
 
54
63
  if filename = ARGV.pop
55
- input_content = IO.read(filename)
56
- Coradoc::ReverseAdoc.config.sourcedir = File.dirname(File.expand_path(filename))
64
+ input_content = filename
57
65
  else
58
- if Coradoc::ReverseAdoc.config.external_images
66
+ if input_options[:external_images]
59
67
  raise "The -e | --external-images feature cannot be used with STDIN input. Exiting."
60
68
  end
61
69
 
62
- input_content = ARGF.read
70
+ input_content = ARGF
63
71
  end
64
72
 
65
- if Coradoc::ReverseAdoc.config.external_images && Coradoc::ReverseAdoc.config.destination.nil?
73
+ if input_options[:external_images] && destination.nil?
66
74
  raise "The -e | --external-images feature must be used with -o | --output. Exiting."
67
75
  end
68
76
 
69
- if Coradoc::ReverseAdoc.config.split_sections && Coradoc::ReverseAdoc.config.destination.nil?
77
+ if input_options[:split_sections] && destination.nil?
70
78
  raise "The --split_sections feature must be used with -o | --output. Exiting."
71
79
  end
72
80
 
73
- # Read from STDIN
74
- adoc_content = Coradoc::ReverseAdoc.convert(input_content)
75
-
76
- # Print to STDOUT
77
- unless Coradoc::ReverseAdoc.config.destination
78
- puts adoc_content
79
- exit
80
- end
81
-
82
- # Write output to Coradoc::ReverseAdoc.config.destination
83
- adoc_content = {nil => adoc_content} unless adoc_content.is_a? Hash
84
-
85
- adoc_content.each do |file, content|
86
- destination = Coradoc::ReverseAdoc.config.destination
87
- destdir = File.dirname(destination)
88
- filename = file ? "#{destdir}/#{file}" : destination
89
- FileUtils.mkdir_p(File.dirname(filename))
90
- File.write(filename, content)
91
- end
81
+ Coradoc::Converter.(input_content, destination, **config)
data/exe/w2a CHANGED
@@ -6,27 +6,35 @@ require "bundler/setup"
6
6
 
7
7
  require "word-to-markdown"
8
8
  require "optparse"
9
- require "coradoc/reverse_adoc"
9
+ require "coradoc"
10
10
 
11
11
  ARGV.push("-h") if ARGV.empty?
12
12
 
13
+ config = {
14
+ input_options: input_options = {},
15
+ input_processor: :docx,
16
+ output_options: output_options = {},
17
+ output_processor: :adoc,
18
+ }
19
+ destination = nil
20
+
13
21
  OptionParser.new do |opts|
14
22
  opts.banner = "Usage: w2a [options] <file>"
15
23
  opts.on("-m", "--mathml2asciimath", "Convert MathML to AsciiMath") do |_v|
16
- Coradoc::ReverseAdoc.config.mathml2asciimath = true
24
+ input_options[:mathml2asciimath] = true
17
25
  end
18
26
 
19
27
  opts.on("-oFILENAME", "--output=FILENAME", "Output file to write to") do |v|
20
- Coradoc::ReverseAdoc.config.destination = File.expand_path(v)
21
- # puts "output goes to #{Coradoc::ReverseAdoc.config.destination}"
28
+ destination = File.expand_path(v)
29
+ # puts "output goes to #{Coradoc::Input::HTML.config.destination}"
22
30
  end
23
31
 
24
32
  opts.on("-e", "--external-images", "Export images if data URI") do |_v|
25
- Coradoc::ReverseAdoc.config.external_images = true
33
+ input_options[:external_images] = true
26
34
  end
27
35
 
28
36
  opts.on("-v", "--version", "Version information") do |_v|
29
- puts "reverse_adoc: v#{Coradoc::ReverseAdoc::VERSION}"
37
+ puts "Coradoc: v#{Coradoc::VERSION}"
30
38
  puts "[dependency] WordToMarkdown: v#{WordToMarkdown::VERSION}"
31
39
  if Gem.win_platform?
32
40
  puts "[dependency] LibreOffice: version not available on Windows"
@@ -45,28 +53,8 @@ end.parse!
45
53
  filename = ARGV.pop
46
54
  raise "Please provide an input file to process. Exiting." unless filename
47
55
 
48
- if Coradoc::ReverseAdoc.config.external_images && Coradoc::ReverseAdoc.config.destination.nil?
56
+ if input_options[:external_images] && destination.nil?
49
57
  raise "The -e | --external-images feature must be used with -o | --output. Exiting."
50
58
  end
51
59
 
52
- Coradoc::ReverseAdoc.config.sourcedir = Dir.mktmpdir
53
-
54
- doc = WordToMarkdown.new(filename, Coradoc::ReverseAdoc.config.sourcedir)
55
- # File.open("test.html", "w:UTF-8") { |f| f.write doc.document.html }
56
- adoc_content = Coradoc::ReverseAdoc.convert(
57
- Coradoc::ReverseAdoc.cleaner.preprocess_word_html(doc.document.html),
58
- WordToMarkdown::REVERSE_MARKDOWN_OPTIONS,
59
- )
60
- # puts scrub_whitespace(doc.document.html)
61
-
62
- # Print to STDOUT
63
- unless Coradoc::ReverseAdoc.config.destination
64
- puts adoc_content
65
- exit
66
- end
67
-
68
- # Write output to Coradoc::ReverseAdoc.config.destination
69
- FileUtils.mkdir_p(File.dirname(Coradoc::ReverseAdoc.config.destination))
70
- File.open(Coradoc::ReverseAdoc.config.destination, "w") do |file|
71
- file.write(adoc_content)
72
- end
60
+ Coradoc::Converter.(filename, destination, **config)
@@ -0,0 +1,105 @@
1
+ require "coradoc"
2
+ require "thor"
3
+
4
+ module Coradoc
5
+ class CLI < Thor
6
+ package_name "coradoc"
7
+
8
+ desc "convert [FILE]", "Convert document to another format"
9
+
10
+ option :output,
11
+ type: :string, aliases: "-o",
12
+ desc: "Output file to write"
13
+
14
+ option :input_format,
15
+ type: :string, aliases: "-I",
16
+ enum: Input.keys.map(&:to_s), default: nil,
17
+ desc: "Define input format (defaults to input file extension)"
18
+
19
+ option :output_format,
20
+ type: :string, aliases: "-O",
21
+ enum: Output.keys.map(&:to_s), default: nil,
22
+ desc: "Define output format (defaults to output file extension)"
23
+
24
+ at_least_one :output, :output_format
25
+
26
+ option :require,
27
+ type: :string, aliases: "-r",
28
+ repeatable: true,
29
+ desc: "Require additional Ruby file (eg. to load a plugin)"
30
+
31
+ option :external_images,
32
+ type: :boolean, aliases: "-e",
33
+ desc: "Extract images from input document"
34
+
35
+ option :unknown_tags,
36
+ type: :string, aliases: "-u",
37
+ enum: %w[pass_through drop bypass raise],
38
+ default: "pass_through",
39
+ desc: "Unknown tag handling"
40
+
41
+ option :mathml2asciimath,
42
+ type: :boolean, aliases: "-m",
43
+ desc: "Convert MathML to AsciiMath"
44
+
45
+ option :track_time,
46
+ type: :boolean,
47
+ desc: "Track time spent on each step"
48
+
49
+ option :split_sections,
50
+ type: :numeric,
51
+ default: 0, banner: "LEVEL",
52
+ desc: "Split sections into separate files up to a provided level"
53
+
54
+ def convert(input = nil)
55
+ options[:require]&.each { |r| Kernel.require r }
56
+
57
+ config = {
58
+ input_options: input_options = {},
59
+ input_processor: nil,
60
+ output_options: output_options = {},
61
+ output_processor: nil,
62
+ }
63
+
64
+ config[:input_processor] = options[:input_format]&.to_sym
65
+ config[:output_processor] = options[:output_format]&.to_sym
66
+
67
+ %i[
68
+ external_images
69
+ unknown_tags
70
+ mathml2asciimath
71
+ track_time
72
+ split_sections
73
+ ].each do |i|
74
+ input_options[i] = options[i]
75
+ end
76
+
77
+ output = options[:output]
78
+
79
+ begin
80
+ Coradoc::Converter.(input, output, **config)
81
+ rescue Converter::NoInputPathError => e
82
+ warn "You must provide INPUT file as a file for this optionset."
83
+ warn "Detail: #{e.message}"
84
+ rescue Converter::NoOutputPathError => e
85
+ warn "You must provide OUTPUT file as a file for this optionset."
86
+ warn "Detail: #{e.message}"
87
+ rescue Converter::NoProcessorError => e
88
+ warn "No processor found for given input/output."
89
+ warn "Hint: set -I/--input-format or -O/--output-format option."
90
+ warn "Detail: #{e.message}"
91
+ end
92
+ end
93
+
94
+ desc "version", "display version information"
95
+ def version
96
+ puts "Coradoc: v#{Coradoc::VERSION}"
97
+ puts "[dependency] WordToMarkdown: v#{WordToMarkdown::VERSION}"
98
+ if Gem.win_platform?
99
+ puts "[dependency] LibreOffice: version not available on Windows"
100
+ else
101
+ puts "[dependency] LibreOffice: v#{WordToMarkdown.soffice.version}"
102
+ end
103
+ end
104
+ end
105
+ end