slaw 0.1.2

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.
@@ -0,0 +1,116 @@
1
+ <?xml version="1.0"?>
2
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
3
+ xmlns:a="http://www.akomantoso.org/2.0"
4
+ exclude-result-prefixes="a">
5
+
6
+ <xsl:template match="a:act">
7
+ <xsl:element name="span" namespace="">
8
+ <xsl:attribute name="class">an-act</xsl:attribute>
9
+ <xsl:apply-templates select="a:preamble" />
10
+ <xsl:apply-templates select="a:body/a:part | a:body/a:chapter | a:body/a:section" />
11
+ </xsl:element>
12
+ </xsl:template>
13
+
14
+ <!-- for parts and chapters, include an easily stylable heading -->
15
+ <xsl:template match="a:part">
16
+ <div class="an-part" id="{@id}">
17
+ <h1>
18
+ <xsl:text>Part </xsl:text>
19
+ <xsl:value-of select="./a:num" />
20
+ <xsl:text> - </xsl:text>
21
+ <xsl:value-of select="./a:heading" />
22
+ </h1>
23
+
24
+ <xsl:apply-templates select="./*[not(self::a:num) and not(self::a:heading)]" />
25
+ </div>
26
+ </xsl:template>
27
+
28
+ <xsl:template match="a:chapter">
29
+ <div class="an-chapter" id="{@id}">
30
+ <h1>
31
+ <xsl:text>Chapter </xsl:text>
32
+ <xsl:value-of select="./a:num" />
33
+ <br/>
34
+ <xsl:value-of select="./a:heading" />
35
+ </h1>
36
+
37
+ <xsl:apply-templates select="./*[not(self::a:num) and not(self::a:heading)]" />
38
+ </div>
39
+ </xsl:template>
40
+
41
+ <!-- the schedules "chapter" isn't actually a chapter -->
42
+ <xsl:template match="a:chapter[starts-with(@id, 'schedule')]">
43
+ <div class="an-chapter" id="{@id}">
44
+ <h1>
45
+ <xsl:text>Schedule </xsl:text>
46
+ <xsl:value-of select="./a:num" />
47
+ <br/>
48
+ <xsl:value-of select="./a:heading" />
49
+ </h1>
50
+
51
+ <xsl:apply-templates select="./*[not(self::a:num) and not(self::a:heading)]" />
52
+ </div>
53
+ </xsl:template>
54
+
55
+ <xsl:template match="a:section">
56
+ <div class="an-{local-name()}" id="{@id}">
57
+ <h3>
58
+ <xsl:value-of select="./a:num" />
59
+ <xsl:text> </xsl:text>
60
+ <xsl:value-of select="./a:heading" />
61
+ </h3>
62
+
63
+ <xsl:apply-templates select="./*[not(self::a:num) and not(self::a:heading)]" />
64
+ </div>
65
+ </xsl:template>
66
+
67
+ <xsl:template match="a:subsection">
68
+ <span class="an-{local-name()}" id="{@id}">
69
+ <xsl:apply-templates select="./*[not(self::a:heading)]" />
70
+ </span>
71
+ </xsl:template>
72
+
73
+ <!-- for term nodes, ensure we keep the refersTo element -->
74
+ <xsl:template match="a:term">
75
+ <a class="an-{local-name()}">
76
+ <xsl:attribute name="data-refers-to">
77
+ <xsl:value-of select="@refersTo" />
78
+ </xsl:attribute>
79
+
80
+ <xsl:attribute name="href"><xsl:value-of select="$base_url" />definitions/#def-<xsl:value-of select="translate(@refersTo, '#', '')" /></xsl:attribute>
81
+
82
+ <xsl:apply-templates />
83
+ </a>
84
+ </xsl:template>
85
+
86
+ <!-- for all nodes, generate a SPAN element with a class matching
87
+ the AN name of the node and copy over the ID if it exists -->
88
+ <xsl:template match="*">
89
+ <span class="an-{local-name()}">
90
+ <xsl:if test="@id">
91
+ <xsl:attribute name="id">
92
+ <xsl:value-of select="@id" />
93
+ </xsl:attribute>
94
+ </xsl:if>
95
+ <xsl:apply-templates />
96
+ </span>
97
+ </xsl:template>
98
+
99
+ <!-- For HTML table elements, copy them over then apply normal AN
100
+ processing to their contents -->
101
+ <xsl:template match="a:table | a:tr | a:th | a:td">
102
+ <xsl:element name="{local-name()}">
103
+ <xsl:copy-of select="@*" />
104
+ <xsl:apply-templates />
105
+ </xsl:element>
106
+ </xsl:template>
107
+
108
+ <!-- special HTML elements -->
109
+ <xsl:template match="a:a | a:abbr | a:b | a:i | a:span | a:sub | a:sup | a:u">
110
+ <xsl:element name="{local-name()}">
111
+ <xsl:copy-of select="@*" />
112
+ <xsl:apply-templates />
113
+ </xsl:element>
114
+ </xsl:template>
115
+
116
+ </xsl:stylesheet>
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0"?>
2
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
3
+ xmlns:a="http://www.akomantoso.org/2.0"
4
+ exclude-result-prefixes="a">
5
+
6
+ <xsl:import href="elements.xsl" />
7
+
8
+ <xsl:output method="html" />
9
+
10
+ <xsl:template match="/">
11
+ <!-- root_elem is passed in as an xpath parameter -->
12
+ <xsl:apply-templates select="$root_elem" />
13
+ </xsl:template>
14
+
15
+ </xsl:stylesheet>
16
+
@@ -0,0 +1,3 @@
1
+ module Slaw
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,77 @@
1
+ # Add some helper methods to XML node objects
2
+ class Nokogiri::XML::Node
3
+
4
+ # The AkomaNtoso number of this node, or nil if unknown.
5
+ # Major AN elements such as chapters, parts and sections almost
6
+ # always have numbers.
7
+ def num
8
+ node = at_xpath('./a:num', a: Slaw::NS)
9
+ node ? node.text.gsub(/\.$/, '') : nil
10
+ end
11
+
12
+ def heading
13
+ node = at_xpath('./a:heading', a: Slaw::NS)
14
+ node ? node.text : nil
15
+ end
16
+
17
+ def id
18
+ self['id']
19
+ end
20
+
21
+ def chapters
22
+ xpath('./a:chapter', a: Slaw::NS)
23
+ end
24
+
25
+ def sections
26
+ xpath('./a:section', a: Slaw::NS)
27
+ end
28
+
29
+ def parts
30
+ xpath('./a:part', a: Slaw::NS)
31
+ end
32
+
33
+ # Get a nodeset of child elements of this node which should show
34
+ # up in the table of contents
35
+ def toc_children
36
+ if in_schedules?
37
+ # in schedules, we only care about chapters that have numbers in them, because we
38
+ # can't link to them otherwise
39
+ xpath('a:chapter[a:num]', a: Slaw::NS)
40
+ else
41
+ xpath('a:part | a:chapter | a:section', a: Slaw::NS)
42
+ end
43
+ end
44
+
45
+ # Does this element hold children for the table of contents?
46
+ def toc_container?
47
+ !toc_children.empty?
48
+ end
49
+
50
+ # Title for this element in the table of contents
51
+ def toc_title
52
+ case name
53
+ when "mainBody"
54
+ "Schedules"
55
+ when "chapter"
56
+ title = in_schedules? ? "Schedule" : "Chapter"
57
+ title << ' ' + num
58
+ title << ' - ' + heading if heading
59
+ title
60
+ when "part"
61
+ "Part #{num} - #{heading}"
62
+ when "section"
63
+ if not heading or heading.empty?
64
+ "Section #{num}"
65
+ elsif num
66
+ "#{num}. #{heading}"
67
+ else
68
+ heading
69
+ end
70
+ end
71
+ end
72
+
73
+ # Is this element part of a schedule document?
74
+ def in_schedules?
75
+ not ancestors('doc[name="schedules"]').empty?
76
+ end
77
+ end
data/lib/slaw.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'nokogiri'
2
+
3
+ require 'slaw/version'
4
+ require 'slaw/namespace'
5
+ require 'slaw/logging'
6
+
7
+ require 'slaw/act'
8
+ require 'slaw/bylaw'
9
+ require 'slaw/collection'
10
+
11
+ require 'slaw/xml_support'
12
+ require 'slaw/lifecycle_event'
13
+
14
+ require 'slaw/render/html'
15
+ require 'slaw/parse/blocklists'
16
+ require 'slaw/parse/builder'
17
+ require 'slaw/parse/cleanser'
18
+ require 'slaw/parse/error'
19
+ require 'slaw/parse/grammar_helpers'
20
+ require 'slaw/parse/nodes'
21
+ require 'slaw/elasticsearch'
22
+
23
+ module Slaw
24
+ end
data/slaw.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slaw/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slaw"
8
+ spec.version = Slaw::VERSION
9
+ spec.authors = ["Greg Kempe"]
10
+ spec.email = ["greg@kempe.net"]
11
+ spec.summary = %q{A lightweight library for using Akoma Ntoso acts in Ruby.}
12
+ spec.description = %q{Slaw is a lightweight library for rendering and generating Akoma Ntoso acts from plain text and PDF documents.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.14.1"
24
+
25
+ spec.add_runtime_dependency "nokogiri", "~> 1.6.0"
26
+ spec.add_runtime_dependency "elasticsearch", "~> 1.0.5"
27
+ spec.add_runtime_dependency "treetop", "~> 1.5"
28
+ spec.add_runtime_dependency "builder", "~> 3.2.2"
29
+ spec.add_runtime_dependency "log4r", "~> 1.1.10"
30
+ end