-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
GH-36 Add encapsulation to def and req blocks in XML
- Loading branch information
Showing
9 changed files
with
237 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
RUBY_ENGINE == 'opal' ? | ||
(require 'xmldefblock/extension') : | ||
(require_relative 'xmldefblock/extension') | ||
|
||
Asciidoctor::Extensions.register do | ||
if (@document.basebackend? 'docbook') | ||
block XmlDefBlock | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'asciidoctor/extensions' | ||
include Asciidoctor | ||
|
||
|
||
class XmlDefBlock < Extensions::BlockProcessor | ||
use_dsl | ||
named :def | ||
on_contexts :open, :paragraph, :example, :listing, :sidebar, :pass | ||
|
||
def process parent, reader, attrs | ||
# Add pass characters here to prevent html character replacements for < > tags | ||
pass = "+++" | ||
attrs['name'] = 'definition' | ||
attrs['caption'] = 'Definition: ' | ||
|
||
# downcase the title and replace spaces with underscores. | ||
# Also replacing special HTML entities: | ||
# " = " | ||
# & = & | ||
formatted_title = attrs['title'].downcase.tr(" ", "_").gsub(/&/, '&').gsub(/"/, '"') | ||
# Sanitize the unformatted title string | ||
san_title = attrs['title'].gsub(/&/, '&').gsub(/"/, '"') | ||
|
||
link = "<link linkend=\"#{formatted_title}\">#{san_title}</link></simpara>\n<simpara>" | ||
anchor = "<anchor xml:id=\"#{formatted_title}\" xreflabel=\"[#{formatted_title}]\"/>" | ||
def_prefix = "<emphasis role=\"strong\">Definition:</emphasis>" | ||
|
||
# concatenate all generated lines and prepend before the included | ||
# definition block content | ||
concat_lines = reader.lines.unshift(pass, anchor, def_prefix, link, pass) | ||
|
||
create_block parent, :admonition, concat_lines, attrs, content_model: :compound | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
RUBY_ENGINE == 'opal' ? | ||
(require 'xmlreqblock/extension') : | ||
(require_relative 'xmlreqblock/extension') | ||
|
||
Asciidoctor::Extensions.register do | ||
if (@document.basebackend? 'docbook') | ||
block XmlReqBlock | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'asciidoctor/extensions' | ||
include Asciidoctor | ||
|
||
|
||
class XmlReqBlock < Extensions::BlockProcessor | ||
use_dsl | ||
named :req | ||
on_contexts :open, :paragraph, :example, :listing, :sidebar, :pass | ||
name_positional_attributes 'number', 'version' | ||
|
||
def process parent, reader, attrs | ||
# Add pass characters here to prevent html character replacements for < > tags | ||
pass = "+++" | ||
attrs['name'] = 'requirement' | ||
attrs['caption'] = 'Requirement: ' | ||
id = attrs['id'] | ||
|
||
begin | ||
# downcase the title and replace spaces with underscores. | ||
# Also replacing special HTML entities: | ||
# " = " | ||
# & = & | ||
downcased_title = attrs['title'].downcase.tr(" ", "_").gsub("\"", """) | ||
san_title = attrs['title'].gsub(/&/, '&') | ||
|
||
rescue Exception => msg | ||
puts msg | ||
# If no title exists on the Req block, throw an exception | ||
puts "[ERROR] Requirement block title missing" | ||
end | ||
|
||
|
||
|
||
anchor = "<anchor xml:id=\"Req-#{id}\" xreflabel=\"[Req-#{id}]\"/>" | ||
req_prefix = "<emphasis role=\"strong\">Requirement: #{id}:</emphasis>" | ||
link = "<link linkend=\"Req-#{id}\">#{san_title}</link> (ver. #{attrs['version']})</simpara> | ||
<simpara>" | ||
|
||
# concatenate all generated lines and prepend before the original content | ||
concat_lines = reader.lines.unshift(pass, anchor, req_prefix, link, pass) | ||
|
||
create_block parent, :admonition, concat_lines, attrs, content_model: :compound | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require_relative 'todo_block' | ||
require 'test/unit' | ||
require_relative 'callout_inline_macro' | ||
require_relative 'xmldefblock' | ||
require_relative 'xmlreqblock' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'asciidoctor' | ||
require_relative "../../main/resources/ext/xmldefblock.rb" | ||
require "test/unit" | ||
|
||
# Register the Def block processor | ||
Extensions.register do | ||
block XmlDefBlock | ||
end | ||
|
||
class TestDefBlock < Test::Unit::TestCase | ||
|
||
def test_simple_xml | ||
input = ".The Title Of MY Definition\n[def]\n--\nMy Amazing Definition\n--" | ||
doc = Asciidoctor::Document.new ("#{input}"), :backend => 'docbook' | ||
|
||
assert_equal("<definition>\n<title>The Title Of MY Definition</title>\n<simpara>"\ | ||
"\n<anchor xml:id=\"the_title_of_my_definition\" xreflabel=\"[the_title_of_my_definition]\""\ | ||
"/>\n<emphasis role=\"strong\">Definition:</emphasis>\n<link linkend=\""\ | ||
"the_title_of_my_definition\">The Title Of MY Definition</link></simpara>\n<simpara>\n\n"\ | ||
"My Amazing Definition</simpara>\n</definition>", | ||
doc.render) | ||
end | ||
|
||
def test_character_replacement_in_title_xml | ||
input = ".The so called \"title\"\n[def]\n--\nThe Definitive Test\n--" | ||
doc = Asciidoctor::Document.new ("#{input}"), :backend => 'docbook' | ||
|
||
assert_equal("<definition>\n<title>The so called \"title\"</title>\n<simpara>\n<anchor xml:id=\""\ | ||
"the_so_called_"title"\" xreflabel=\"[the_so_called_"title"]\"/>\n<emphasis"\ | ||
" role=\"strong\">Definition:</emphasis>\n<link linkend=\"the_so_called_"title"\">The "\ | ||
"so called "title"</link></simpara>\n<simpara>\n\nThe Definitive Test</simpara>\n</definition>", | ||
doc.render) | ||
end | ||
|
||
|
||
def test_simple_html | ||
input = ".The Title Of MY Definition\n[def]\n--\nMy Amazing Definition\n--" | ||
|
||
assert_equal("<div class=\"admonitionblock definition\">\n<table>\n<tr>\n<td "\ | ||
"class=\"icon\">\n<div class=\"title\">Definition: </div>\n</td>\n<td class"\ | ||
"=\"content\">\n<div class=\"title\">The Title Of MY Definition</div>\n<div"\ | ||
" class=\"paragraph\">\n<p>\n<anchor xml:id=\"the_title_of_my_definition\" "\ | ||
"xreflabel=\"[the_title_of_my_definition]\"/>\n<emphasis role=\"strong\">"\ | ||
"Definition:</emphasis>\n<link linkend=\"the_title_of_my_definition\">The "\ | ||
"Title Of MY Definition</link></simpara>\n<simpara>\n\nMy Amazing Definition"\ | ||
"</p>\n</div>\n</td>\n</tr>\n</table>\n</div>", | ||
Asciidoctor::Document.new(input).render) | ||
end | ||
|
||
|
||
|
||
=begin | ||
add a test to catch errors | ||
def test_without_title | ||
input = "\n\n[def]\n--\nMy Amazing Definition\n--" | ||
assert_raise(NoMethodError) do | ||
end | ||
end | ||
=end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'asciidoctor' | ||
require_relative "../../main/resources/ext/xmlreqblock.rb" | ||
require "test/unit" | ||
|
||
# Register the Req block processor | ||
Extensions.register do | ||
block XmlReqBlock | ||
end | ||
|
||
class TestReqBlock < Test::Unit::TestCase | ||
def test_basic_xml | ||
input = ".Some title\n[req,id=R-1,version=1]\n--\nA Super Requirement\n--" | ||
doc = Asciidoctor::Document.new ("#{input}"), :backend => 'docbook' | ||
|
||
assert_equal("<requirement xml:id=\"R-1\">\n<title>Some title</title>\n<simpara>"\ | ||
"\n<anchor xml:id=\"Req-R-1\" xreflabel=\"[Req-R-1]\"/>\n<emphasis role=\"strong"\ | ||
"\">Requirement: R-1:</emphasis>\n<link linkend=\"Req-R-1\">Some title</link> (ver. 1)"\ | ||
"</simpara>\n <simpara>\n\nA Super Requirement</simpara>\n</requirement>", | ||
doc.render) | ||
end | ||
|
||
def test_character_replacement_in_title_xml | ||
input = ".The so called \"title\"\n[req,id=R-1,version=1]\n--\nThe most superlative Requirement\n--" | ||
doc = Asciidoctor::Document.new ("#{input}"), :backend => 'docbook' | ||
|
||
assert_equal("<requirement xml:id=\"R-1\">\n<title>The so called \"title\"</title>"\ | ||
"\n<simpara>\n<anchor xml:id=\"Req-R-1\" xreflabel=\"[Req-R-1]\"/>\n<emphasis "\ | ||
"role=\"strong\">Requirement: R-1:</emphasis>\n<link linkend=\"Req-R-1\">The so"\ | ||
" called \"title\"</link> (ver. 1)</simpara>\n <simpara>\n\nThe most superlative"\ | ||
" Requirement</simpara>\n</requirement>", | ||
doc.render) | ||
end | ||
|
||
def test_multiple_replacements_in_title_xml | ||
input = ".That's like, your \"opinion\" & stuff\n[req,id=R-1,version=1]\n--\nThe dude abides\n--" | ||
doc = Asciidoctor::Document.new ("#{input}"), :backend => 'docbook' | ||
|
||
assert_equal("<requirement xml:id=\"R-1\">\n<title>That’s like, your \"opinion\""\ | ||
" & stuff</title>\n<simpara>\n<anchor xml:id=\"Req-R-1\" xreflabel=\"[Req-R-1]\""\ | ||
"/>\n<emphasis role=\"strong\">Requirement: R-1:</emphasis>\n<link linkend=\"Req-R-1\""\ | ||
">That's like, your \"opinion\" & stuff</link> (ver. 1)</simpara>\n <simpara>\n"\ | ||
"\nThe dude abides</simpara>\n</requirement>", | ||
doc.render) | ||
end | ||
|
||
def test_basic_html | ||
input = ".Some title\n[req,id=R-1,version=1]\n--\nA Super Requirement\n--" | ||
doc = Asciidoctor::Document.new ("#{input}") | ||
|
||
assert_equal("<div id=\"R-1\" class=\"admonitionblock requirement\">\n<table>\n"\ | ||
"<tr>\n<td class=\"icon\">\n<div class=\"title\">Requirement: </div>\n</td>\n"\ | ||
"<td class=\"content\">\n<div class=\"title\">Some title</div>\n<div class=\""\ | ||
"paragraph\">\n<p>\n<anchor xml:id=\"Req-R-1\" xreflabel=\"[Req-R-1]\"/>\n<emphasis"\ | ||
" role=\"strong\">Requirement: R-1:</emphasis>\n<link linkend=\"Req-R-1\">Some "\ | ||
"title</link> (ver. 1)</simpara>\n <simpara>\n\nA Super Requirement</p>\n</div>"\ | ||
"\n</td>\n</tr>\n</table>\n</div>", | ||
doc.render) | ||
end | ||
|
||
=begin | ||
add a test to catch errors | ||
def test_no_title | ||
input = "\n\n[req,id=R-1,version=1]\n--\nA Super Requirement\n--" | ||
assert_raise(NoMethodError) do | ||
Asciidoctor::Document.new(input).render | ||
end | ||
end | ||
=end | ||
|
||
end |