diff --git a/pom.xml b/pom.xml index 364b69f..d8d147f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,3 +1,4 @@ + @@ -5,7 +6,7 @@ eu.numberfour asciispec - 0.0.10 + 0.0.1-SNAPSHOT jar diff --git a/src/main/resources/asciispec b/src/main/resources/asciispec index 0da370a..2d07bf3 100644 --- a/src/main/resources/asciispec +++ b/src/main/resources/asciispec @@ -11,13 +11,13 @@ APP_HOME="$(pwd -P)" cd "$INVOCATION_HOME" >/dev/null # The classpath is built from absolute paths since this script may be executed from anywhere. -CLASSPATH="$APP_HOME/lib/asciispec-0.0.10.jar" +CLASSPATH="$APP_HOME/lib/asciispec-0.0.1-SNAPSHOT.jar" # Force use of Font Awesome icons & pass filename PARAMETERS="-a icons=font -B $INVOCATION_HOME" # Ruby Extensions -EXTENSIONS="-r $APP_HOME/res/ext/todo.rb -r $APP_HOME/res/ext/callout.rb -r asciidoctor-diagram" +EXTENSIONS="-r $APP_HOME/res/ext/todo.rb -r $APP_HOME/res/ext/callout.rb -r $APP_HOME/res/ext/xmldefblock.rb -r $APP_HOME/res/ext/xmlreqblock.rb -r asciidoctor-diagram" # Starts the AsciiSpec Server CMD_SERVER="java -Dfile.encoding=UTF-8 -cp $CLASSPATH eu.numberfour.asciispec.cli.AsciiSpecServer" diff --git a/src/main/resources/ext/xmldefblock.rb b/src/main/resources/ext/xmldefblock.rb new file mode 100644 index 0000000..375191e --- /dev/null +++ b/src/main/resources/ext/xmldefblock.rb @@ -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 diff --git a/src/main/resources/ext/xmldefblock/extension.rb b/src/main/resources/ext/xmldefblock/extension.rb new file mode 100644 index 0000000..5de38f9 --- /dev/null +++ b/src/main/resources/ext/xmldefblock/extension.rb @@ -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 = "#{san_title}\n" + anchor = "" + def_prefix = "Definition:" + + # 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 diff --git a/src/main/resources/ext/xmlreqblock.rb b/src/main/resources/ext/xmlreqblock.rb new file mode 100644 index 0000000..b71b7cf --- /dev/null +++ b/src/main/resources/ext/xmlreqblock.rb @@ -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 diff --git a/src/main/resources/ext/xmlreqblock/extension.rb b/src/main/resources/ext/xmlreqblock/extension.rb new file mode 100644 index 0000000..59cf15e --- /dev/null +++ b/src/main/resources/ext/xmlreqblock/extension.rb @@ -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 = "" + req_prefix = "Requirement: #{id}:" + link = "#{san_title} (ver. #{attrs['version']}) + " + + # 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 diff --git a/src/test/ruby/suite.rb b/src/test/ruby/suite.rb index 9994592..cbe6a64 100644 --- a/src/test/ruby/suite.rb +++ b/src/test/ruby/suite.rb @@ -1,3 +1,5 @@ require_relative 'todo_block' require 'test/unit' require_relative 'callout_inline_macro' +require_relative 'xmldefblock' +require_relative 'xmlreqblock' diff --git a/src/test/ruby/xmldefblock.rb b/src/test/ruby/xmldefblock.rb new file mode 100644 index 0000000..98ddc37 --- /dev/null +++ b/src/test/ruby/xmldefblock.rb @@ -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("\nThe Title Of MY Definition\n"\ + "\n\nDefinition:\nThe Title Of MY Definition\n\n\n"\ + "My Amazing Definition\n", + 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("\nThe so called \"title\"\n\n\nDefinition:\nThe "\ + "so called "title"\n\n\nThe Definitive Test\n", + doc.render) + end + + + def test_simple_html + input = ".The Title Of MY Definition\n[def]\n--\nMy Amazing Definition\n--" + + assert_equal("
\n\n\n\n\n\n
\n
Definition:
\n
\n
The Title Of MY Definition
\n\n

\n\n"\ + "Definition:\nThe "\ + "Title Of MY Definition\n\n\nMy Amazing Definition"\ + "

\n\n
\n
", + 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 diff --git a/src/test/ruby/xmlreqblock.rb b/src/test/ruby/xmlreqblock.rb new file mode 100644 index 0000000..00d9868 --- /dev/null +++ b/src/test/ruby/xmlreqblock.rb @@ -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("\nSome title\n"\ + "\n\nRequirement: R-1:\nSome title (ver. 1)"\ + "\n \n\nA Super Requirement\n", + 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("\nThe so called \"title\""\ + "\n\n\nRequirement: R-1:\nThe so"\ + " called \"title\" (ver. 1)\n \n\nThe most superlative"\ + " Requirement\n", + 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("\nThat’s like, your \"opinion\""\ + " & stuff\n\n\nRequirement: R-1:\nThat's like, your \"opinion\" & stuff (ver. 1)\n \n"\ + "\nThe dude abides\n", + 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("
\n\n"\ + "\n\n"\ + "\n\n
\n
Requirement:
\n
\n
Some title
\n
\n

\n\nRequirement: R-1:\nSome "\ + "title (ver. 1)\n \n\nA Super Requirement

\n
"\ + "\n
\n
", + 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