Skip to content

Commit

Permalink
Merge pull request #87 from NumberFour/GH-36
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
bsmith-n4 authored Jul 13, 2017
2 parents 6293769 + 0c5dec0 commit 2e78389
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>eu.numberfour</groupId>
<artifactId>asciispec</artifactId>
<version>0.0.10</version>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/asciispec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/ext/xmldefblock.rb
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
34 changes: 34 additions & 0 deletions src/main/resources/ext/xmldefblock/extension.rb
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:
# &quot; = "
# &amp; = &
formatted_title = attrs['title'].downcase.tr(" ", "_").gsub(/&/, '&amp;').gsub(/"/, '&quot;')
# Sanitize the unformatted title string
san_title = attrs['title'].gsub(/&/, '&amp;').gsub(/"/, '&quot;')

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
9 changes: 9 additions & 0 deletions src/main/resources/ext/xmlreqblock.rb
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
45 changes: 45 additions & 0 deletions src/main/resources/ext/xmlreqblock/extension.rb
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:
# &quot; = "
# &amp; = &
downcased_title = attrs['title'].downcase.tr(" ", "_").gsub("\"", "&quot;")
san_title = attrs['title'].gsub(/&/, '&amp;')

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
2 changes: 2 additions & 0 deletions src/test/ruby/suite.rb
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'
62 changes: 62 additions & 0 deletions src/test/ruby/xmldefblock.rb
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_&quot;title&quot;\" xreflabel=\"[the_so_called_&quot;title&quot;]\"/>\n<emphasis"\
" role=\"strong\">Definition:</emphasis>\n<link linkend=\"the_so_called_&quot;title&quot;\">The "\
"so called &quot;title&quot;</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
72 changes: 72 additions & 0 deletions src/test/ruby/xmlreqblock.rb
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&#8217;s like, your \"opinion\""\
" &amp; 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\" &amp; 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

0 comments on commit 2e78389

Please sign in to comment.