asciidoctor-xml-ast 0.1.0

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 (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +54 -0
  3. data/lib/asciidoctor-xml-ast.rb +70 -0
  4. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f5787ab74943ac77f8d74feeacc44271123a213cc97ecf0b97c43eed74ac6b9d
4
+ data.tar.gz: 4a80b86fe5ede21a76b77aaef3c43636b6e51b842af0f635252c2349d4719713
5
+ SHA512:
6
+ metadata.gz: c9a384773cbaa0b32d1a1b00fa77c811814b941a29e9c866618a695f8fa7e83cb390b2d6d1d28c2b3e75148e8867d7629888738964e3536f7f2423b5c569dcad
7
+ data.tar.gz: f7751122d0b16ce7e7e7890303b316188d433d310fd1a3ab874d1dd6472c6d528287a6b029d36a2d24732d1f0e1e9c22701e2f6a6a6a431b104c8e3323242b82
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # asciidoctor-ast-xml
2
+
3
+ This repository implements an [Asciidoctor converter] to generate a XML file
4
+ with the syntax nodes of a parsed document.
5
+
6
+ ## Usage
7
+
8
+ The converter is available as a Ruby gem, and can be installed with the
9
+ following command:
10
+
11
+ ```console
12
+ $ gem install asciidoctor-ast-xml
13
+ ```
14
+
15
+ Then, add the options `--require asciidoctor-ast-xml` and `--backend xml-ast`.
16
+ For example:
17
+
18
+ ```console
19
+ $ asciidoctor -r asciidoctor-ast-xml -b xml-ast document.adoc
20
+ ```
21
+
22
+ In the previous command, the output will be in the `document.ast.xml` file.
23
+
24
+ ## Example
25
+
26
+ The following document:
27
+
28
+ ```asciidoc
29
+ = Lorem Ipsum
30
+
31
+ == Dolor Sit Amet
32
+
33
+ Cillum dolore eu *fugiat* nulla pariatur
34
+ ```
35
+
36
+ Generates a XML like this:
37
+
38
+ ```xml
39
+ <document title="Lorem Ipsum" ... >
40
+ <section id="_dolor_sit_amet" title="Dolor Sit Amet">
41
+ <paragraph>Cillum dolore eu <inline-quoted type="strong">fugiat</inline-quoted> nulla pariatur</paragraph>
42
+ </section>
43
+ </document>
44
+ ```
45
+
46
+ The `<document>` element contains a lot of attributes, which have been removed
47
+ in this example so it is easier to read.
48
+
49
+ The actual output is not indented. The previous example is formatted with [xmllint].
50
+
51
+
52
+
53
+ [Asciidoctor converter]: https://docs.asciidoctor.org/asciidoctor/latest/convert/
54
+ [xmllint]: https://gnome.pages.gitlab.gnome.org/libxml2/xmllint.html
@@ -0,0 +1,70 @@
1
+ require "cgi"
2
+
3
+ class AstConverter
4
+ include Asciidoctor::Converter
5
+ register_for "xml-ast"
6
+
7
+ def initialize(*args)
8
+ super
9
+ outfilesuffix ".ast.xml"
10
+ end
11
+
12
+ def convert(node, transform = node.node_name)
13
+ element_name = transform.tr("_", "-")
14
+
15
+ # Element attributes.
16
+ attributes = []
17
+
18
+ %i(id target title type).each do |name|
19
+ if node.respond_to?(name) and value = node.send(name) and not value.nil?
20
+ value = CGI.escapeHTML(value.to_s)
21
+ attributes << %[ #{name}="#{value}"]
22
+ end
23
+ end
24
+
25
+ node.attributes.each_pair do |name, value|
26
+ if not value.nil?
27
+ value = CGI.escapeHTML(value.to_s)
28
+ attributes << %[ attr-#{name}="#{value}"]
29
+ end
30
+ end
31
+
32
+ # Element body.
33
+ result = []
34
+
35
+ result << "<#{element_name}#{attributes.join}>"
36
+
37
+ if node.respond_to?(:text)
38
+ result << node.text
39
+ end
40
+
41
+ if node.block?
42
+ append_content(result, node.content)
43
+ end
44
+
45
+ result << "</#{element_name}>"
46
+ result.join
47
+ end
48
+
49
+ def append_content(result, content)
50
+ case content
51
+ when Array
52
+ content.each do |i|
53
+ append_content(result, i)
54
+ end
55
+
56
+ when String
57
+ result << content
58
+
59
+ when Asciidoctor::AbstractNode
60
+ result << content.convert
61
+
62
+ when nil
63
+ # Nothing to add.
64
+
65
+ else
66
+ raise "Invalid content type: #{content.class}"
67
+
68
+ end
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asciidoctor-xml-ast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ayosec
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: asciidoctor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: |2
28
+ Asciidoctor converter to generate a XML file with the syntax
29
+ nodes from a parsed document.
30
+ email:
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - README.md
36
+ - lib/asciidoctor-xml-ast.rb
37
+ homepage: https://github.com/ayosec/asciidoctor-xml-ast
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.3.7
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Converter for Asciidoctor syntax to XML
60
+ test_files: []