From c8f7c16bcc0b25b8cfe3791a013896a0bc47c6a4 Mon Sep 17 00:00:00 2001 From: bsmith Date: Tue, 11 Jul 2017 13:41:34 +0200 Subject: [PATCH 1/9] GH-36 add def block encapsulation for XML only case --- src/main/resources/asciispec | 2 +- src/main/resources/ext/xmldefblock.rb | 9 ++++++++ .../resources/ext/xmldefblock/extension.rb | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/ext/xmldefblock.rb create mode 100644 src/main/resources/ext/xmldefblock/extension.rb diff --git a/src/main/resources/asciispec b/src/main/resources/asciispec index 1875932..dba4a28 100644 --- a/src/main/resources/asciispec +++ b/src/main/resources/asciispec @@ -17,7 +17,7 @@ CLASSPATH="$APP_HOME/lib/asciispec-0.0.1-SNAPSHOT.jar" PARAMETERS="-a icons=font -B $INVOCATION_HOME" # Load Ruby extensions -EXTENSIONS="-r $APP_HOME/res/ext/todo.rb -r $APP_HOME/res/ext/callout.rb" +EXTENSIONS="-r $APP_HOME/res/ext/todo.rb -r $APP_HOME/res/ext/callout.rb -r $APP_HOME/res/ext/xmldefblock.rb" # 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..0538fe4 --- /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 \ No newline at end of file diff --git a/src/main/resources/ext/xmldefblock/extension.rb b/src/main/resources/ext/xmldefblock/extension.rb new file mode 100644 index 0000000..ae0e1af --- /dev/null +++ b/src/main/resources/ext/xmldefblock/extension.rb @@ -0,0 +1,22 @@ +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 the anchor from being processed + # as regular adoc content i.e. html character replacements for < > tags + pass = "+++" + attrs['name'] = 'definition' + attrs['caption'] = 'Definition: ' + title = attrs['caption'] + attrs['title'] + anchor = (create_anchor parent, title, type: :link, target: title).convert + endcontent = reader.lines.unshift(pass, anchor, pass) + create_block parent, :admonition, endcontent, attrs, content_model: :compound + end + +end From 24fba2175c989cb05d51257e17c191efc449e872 Mon Sep 17 00:00:00 2001 From: bsmith Date: Wed, 12 Jul 2017 11:17:50 +0200 Subject: [PATCH 2/9] GH-36 add basic tests for def and req blocks to XML --- .../resources/ext/xmldefblock/extension.rb | 26 ++++++++++++++----- src/test/ruby/suite.rb | 2 ++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/resources/ext/xmldefblock/extension.rb b/src/main/resources/ext/xmldefblock/extension.rb index ae0e1af..c52b667 100644 --- a/src/main/resources/ext/xmldefblock/extension.rb +++ b/src/main/resources/ext/xmldefblock/extension.rb @@ -8,15 +8,27 @@ class XmlDefBlock < Extensions::BlockProcessor on_contexts :open, :paragraph, :example, :listing, :sidebar, :pass def process parent, reader, attrs - # Add pass characters here to prevent the anchor from being processed - # as regular adoc content i.e. html character replacements for < > tags + # Add pass characters here to prevent html character replacements for < > tags pass = "+++" attrs['name'] = 'definition' attrs['caption'] = 'Definition: ' - title = attrs['caption'] + attrs['title'] - anchor = (create_anchor parent, title, type: :link, target: title).convert - endcontent = reader.lines.unshift(pass, anchor, pass) - create_block parent, :admonition, endcontent, attrs, content_model: :compound - end + + # downcase the title and replace spaces with underscores. + # Also replacing double quotes with single quotes to stop xml from breaking + # in the case where the title contains a quote symbol + formatted_title = attrs['title'].downcase.tr(" ", "_").tr("\"", "'") + + # TODO try and use the built-in behaviour of generating anchors: + # link = (create_anchor parent, attrs['title'], type: :link, target: attrs['title']).render + # not working due to missing 'linkend' property. + link = "#{attrs['title']}" + 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/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' From 2b0ac1db4a71ffe90ccc5c0c42e41bdf3029240c Mon Sep 17 00:00:00 2001 From: bsmith Date: Wed, 12 Jul 2017 13:03:42 +0200 Subject: [PATCH 3/9] GH-36 minor patch to visually separate definition content --- src/main/resources/ext/xmldefblock/extension.rb | 2 +- src/test/ruby/xmldefblock.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/resources/ext/xmldefblock/extension.rb b/src/main/resources/ext/xmldefblock/extension.rb index c52b667..8518bc3 100644 --- a/src/main/resources/ext/xmldefblock/extension.rb +++ b/src/main/resources/ext/xmldefblock/extension.rb @@ -21,7 +21,7 @@ def process parent, reader, attrs # TODO try and use the built-in behaviour of generating anchors: # link = (create_anchor parent, attrs['title'], type: :link, target: attrs['title']).render # not working due to missing 'linkend' property. - link = "#{attrs['title']}" + link = "#{attrs['title']}\n" anchor = "" def_prefix = "Definition:" diff --git a/src/test/ruby/xmldefblock.rb b/src/test/ruby/xmldefblock.rb index e874770..593cd42 100644 --- a/src/test/ruby/xmldefblock.rb +++ b/src/test/ruby/xmldefblock.rb @@ -17,8 +17,8 @@ def test_simple " class=\"paragraph\">\n

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

\n\n\n"\ - "\n\n", + "Title Of MY Definition
\n\n\nMy Amazing Definition"\ + "

\n\n\n\n\n", Asciidoctor::Document.new(input).render) end From 2bb1b94433710b50359a11d413d4598ba64a221b Mon Sep 17 00:00:00 2001 From: bsmith Date: Wed, 12 Jul 2017 17:40:18 +0200 Subject: [PATCH 4/9] GH-36 revert jar versioning, add special treatment of character replacements in titles --- pom.xml | 3 +- src/main/resources/asciispec | 2 +- .../resources/ext/xmldefblock/extension.rb | 14 +++--- .../resources/ext/xmlreqblock/extension.rb | 10 +++-- src/test/ruby/xmldefblock.rb | 26 ++++++++++- src/test/ruby/xmlreqblock.rb | 44 +++++++++++++++++-- 6 files changed, 81 insertions(+), 18 deletions(-) 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 cdcbbe1..2d07bf3 100644 --- a/src/main/resources/asciispec +++ b/src/main/resources/asciispec @@ -11,7 +11,7 @@ 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" diff --git a/src/main/resources/ext/xmldefblock/extension.rb b/src/main/resources/ext/xmldefblock/extension.rb index 8518bc3..5de38f9 100644 --- a/src/main/resources/ext/xmldefblock/extension.rb +++ b/src/main/resources/ext/xmldefblock/extension.rb @@ -14,14 +14,14 @@ def process parent, reader, attrs attrs['caption'] = 'Definition: ' # downcase the title and replace spaces with underscores. - # Also replacing double quotes with single quotes to stop xml from breaking - # in the case where the title contains a quote symbol - formatted_title = attrs['title'].downcase.tr(" ", "_").tr("\"", "'") + # Also replacing special HTML entities: + # " = " + # & = & + formatted_title = attrs['title'].downcase.tr(" ", "_").gsub(/&/, '&').gsub(/"/, '"') + # Sanitize the unformatted title string + san_title = attrs['title'].gsub(/&/, '&').gsub(/"/, '"') - # TODO try and use the built-in behaviour of generating anchors: - # link = (create_anchor parent, attrs['title'], type: :link, target: attrs['title']).render - # not working due to missing 'linkend' property. - link = "#{attrs['title']}
\n" + link = "#{san_title}\n" anchor = "" def_prefix = "Definition:" diff --git a/src/main/resources/ext/xmlreqblock/extension.rb b/src/main/resources/ext/xmlreqblock/extension.rb index e3bfe6b..59cf15e 100644 --- a/src/main/resources/ext/xmlreqblock/extension.rb +++ b/src/main/resources/ext/xmlreqblock/extension.rb @@ -17,9 +17,11 @@ def process parent, reader, attrs begin # downcase the title and replace spaces with underscores. - # Also replacing double quotes with single quotes to stop xml from breaking - # in the case where the title contains a quote symbol - formatted_title = attrs['title'].downcase.tr(" ", "_").tr("\"", "'") + # Also replacing special HTML entities: + # " = " + # & = & + downcased_title = attrs['title'].downcase.tr(" ", "_").gsub("\"", """) + san_title = attrs['title'].gsub(/&/, '&') rescue Exception => msg puts msg @@ -31,7 +33,7 @@ def process parent, reader, attrs anchor = "" req_prefix = "Requirement: #{id}:" - link = "#{attrs['title']} (ver. #{attrs['version']}) + link = "#{san_title} (ver. #{attrs['version']}) " # concatenate all generated lines and prepend before the original content diff --git a/src/test/ruby/xmldefblock.rb b/src/test/ruby/xmldefblock.rb index 593cd42..1aa8987 100644 --- a/src/test/ruby/xmldefblock.rb +++ b/src/test/ruby/xmldefblock.rb @@ -8,7 +8,29 @@ end class TestDefBlock < Test::Unit::TestCase - def test_simple + + 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
'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
", - Asciidoctor::Document.new(input).render) + "title (ver. 1)\n \n\nA Super Requirement

\n"\ + "\n
\n
", + doc.render) end =begin From c768ff9f169d71eead27b2559727ffde0c73ff2a Mon Sep 17 00:00:00 2001 From: bsmith Date: Wed, 12 Jul 2017 17:42:03 +0200 Subject: [PATCH 5/9] GH-36 observe better line wrap for tests --- src/test/ruby/xmldefblock.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/ruby/xmldefblock.rb b/src/test/ruby/xmldefblock.rb index 1aa8987..d45bb88 100644 --- a/src/test/ruby/xmldefblock.rb +++ b/src/test/ruby/xmldefblock.rb @@ -25,7 +25,10 @@ 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", + assert_equal("\nThe so called \"title\"\n\n\nDefinition:\nThe "\ + "so called "title"\n\n\nThe Definitive Test\n", doc.render) end From ee50ed8f32af15972bd5c7b61436ed72b086c1bf Mon Sep 17 00:00:00 2001 From: Brian Thomas Smith Date: Wed, 12 Jul 2017 18:13:45 +0200 Subject: [PATCH 6/9] Update xmldefblock.rb --- src/main/resources/ext/xmldefblock.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/ext/xmldefblock.rb b/src/main/resources/ext/xmldefblock.rb index 0538fe4..375191e 100644 --- a/src/main/resources/ext/xmldefblock.rb +++ b/src/main/resources/ext/xmldefblock.rb @@ -6,4 +6,4 @@ if (@document.basebackend? 'docbook') block XmlDefBlock end -end \ No newline at end of file +end From d74eec0ae974c0cf661cfa25af71f03ee5bb2a6c Mon Sep 17 00:00:00 2001 From: Brian Thomas Smith Date: Wed, 12 Jul 2017 18:14:01 +0200 Subject: [PATCH 7/9] Update xmlreqblock.rb --- src/main/resources/ext/xmlreqblock.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/ext/xmlreqblock.rb b/src/main/resources/ext/xmlreqblock.rb index b38bce3..b71b7cf 100644 --- a/src/main/resources/ext/xmlreqblock.rb +++ b/src/main/resources/ext/xmlreqblock.rb @@ -6,4 +6,4 @@ if (@document.basebackend? 'docbook') block XmlReqBlock end -end \ No newline at end of file +end From b818cab133c2eaff1d50bd6e3c26ef24dd42f641 Mon Sep 17 00:00:00 2001 From: bsmith Date: Wed, 12 Jul 2017 18:15:11 +0200 Subject: [PATCH 8/9] GH-36 adding newlines to eof --- src/test/ruby/xmldefblock.rb | 3 +-- src/test/ruby/xmlreqblock.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/ruby/xmldefblock.rb b/src/test/ruby/xmldefblock.rb index d45bb88..98ddc37 100644 --- a/src/test/ruby/xmldefblock.rb +++ b/src/test/ruby/xmldefblock.rb @@ -59,5 +59,4 @@ def test_without_title end end =end - -end \ No newline at end of file +end diff --git a/src/test/ruby/xmlreqblock.rb b/src/test/ruby/xmlreqblock.rb index a5d9657..9475b2f 100644 --- a/src/test/ruby/xmlreqblock.rb +++ b/src/test/ruby/xmlreqblock.rb @@ -69,4 +69,4 @@ def test_no_title end =end -end \ No newline at end of file +end From 0c5dec06f03cc07ba62c946dae2ac67fda4ac388 Mon Sep 17 00:00:00 2001 From: bsmith Date: Thu, 13 Jul 2017 11:18:14 +0200 Subject: [PATCH 9/9] GH-36 typo fix in test --- src/test/ruby/xmlreqblock.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ruby/xmlreqblock.rb b/src/test/ruby/xmlreqblock.rb index 9475b2f..00d9868 100644 --- a/src/test/ruby/xmlreqblock.rb +++ b/src/test/ruby/xmlreqblock.rb @@ -2,7 +2,7 @@ require_relative "../../main/resources/ext/xmlreqblock.rb" require "test/unit" -# Register the TODO block processor +# Register the Req block processor Extensions.register do block XmlReqBlock end