mini_transformer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.gem
6
+ *.rbc
7
+ .bundle
8
+ .config
9
+ coverage
10
+ InstalledFiles
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .DS_Store
20
+
21
+ # Thumbnails
22
+ ._*
23
+
24
+ # Files that might appear on external disk
25
+ .Spotlight-V100
26
+ .Trashes
27
+
28
+ .idea/workspace.xml
data/.rvmrc ADDED
@@ -0,0 +1,3 @@
1
+ rvm use 1.9.2
2
+ rvm gemset create mini_transformer
3
+ rvm gemset use mini_transformer
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mini_transformer.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^spec/.+_spec\.rb$})
11
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
12
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
13
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
14
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
15
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
16
+ watch('spec/spec_helper.rb') { "spec" }
17
+ watch('config/routes.rb') { "spec/routing" }
18
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
19
+ # Capybara request specs
20
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
21
+ end
22
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 John Wang, http://johntwang.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ # Mini Transformer - [![Build Status](https://secure.travis-ci.org/jwang/mini_transformer.png)](http://travis-ci.org/jwang/mini_transformer)
2
+
3
+ # Description
4
+ Mini Transformer takes a pair of input files, JSON and XML, then converts it to HTML5 or JSON format. It also supports a mapping of the children elements to HTML elements via YAML mapping file.
5
+
6
+ # Installation
7
+ `gem install mini_transformer`
8
+
9
+ ## Usage
10
+ `mini_transform transform [JSON, XML, OUTPUT=nil, FORMAT=html, MAPPING=nil]`
11
+
12
+ Requires the input.json and input.xml, if neither is provided or found, a File Not Found error will occur.
13
+
14
+ #### Options
15
+ * An output file name such as output.html. Defaults to UID tag or output if not available.
16
+ * 2 different output formats supported, html or json. Defaults to html.
17
+ * A YAML mapping configuration file for the entries tags to convert to HTML tags. Only available for html output format. Defaults to dt and dd tags.
18
+
19
+ #### Example Usage
20
+ `mini_transform transform input.json, input.xml`
21
+ `mini_transform transform input.json, input.xml, out.json, json`
22
+ `mini_transform transform input.json, input.xml, nil, json`
23
+ `mini_transform transform input.json, input.xml, out.html, html, mapping.yml`
24
+ `mini_transform transform input.json, input.xml, nil, html, mapping.yml`
25
+
26
+ #### Configuration
27
+ Example Mapping YAML - mapping.yml
28
+ `entries: dl
29
+ key-name: dt
30
+ key-description: dd`
31
+
32
+ ## Dependencies
33
+ * [Nokogiri](nokogiri.org)
34
+ * ActiveSupport
35
+ * [JSON](http://flori.github.com/json)
36
+ * [Thor](https://github.com/wycats/thor)
37
+
38
+ ### Development Dependencies
39
+ * [RSpec 2](https://www.relishapp.com/rspec)
40
+ * [guard](https://github.com/guard/guard)
41
+ * [guard-rspec](https://github.com/guard/guard-rspec)
42
+ * [simplecov](https://github.com/colszowka/simplecov)
43
+ * rb-fsevent (Mac OSX only)
44
+ * [growl_notify (Mac OSX only)](https://github.com/scottdavis/growl_notify)
45
+
46
+ ### Additional Information
47
+ * [Test Coverage](http://johntwang.com/mini_transformer/coverage)
48
+ * [Travis CI Build Status](http://travis-ci.org/jwang/mini_transformer)
49
+
50
+
51
+ #### Known Issues
52
+ * Ruby 1.8.7 alphabetizes the meta tags. Meaning :id and :content are swapped when written to file.
53
+
54
+
55
+ ## MIT License
56
+ Copyright (c) 2011 John Wang, http://johntwang.com
57
+
58
+ Permission is hereby granted, free of charge, to any person obtaining
59
+ a copy of this software and associated documentation files (the
60
+ "Software"), to deal in the Software without restriction, including
61
+ without limitation the rights to use, copy, modify, merge, publish,
62
+ distribute, sublicense, and/or sell copies of the Software, and to
63
+ permit persons to whom the Software is furnished to do so, subject to
64
+ the following conditions:
65
+
66
+ The above copyright notice and this permission notice shall be
67
+ included in all copies or substantial portions of the Software.
68
+
69
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
70
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
71
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
72
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
73
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
74
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
75
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Default: run specs.'
5
+ task :default => :spec
6
+
7
+ desc "Run specs"
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.pattern = "./spec/**/*_spec.rb"
10
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ require 'mini_transformer'
3
+
4
+ begin
5
+ require "mini_transformer/cli"
6
+ MiniTransformer::CLI.start
7
+ end
@@ -0,0 +1,124 @@
1
+ require 'rbconfig'
2
+ require 'fileutils'
3
+ require 'open-uri'
4
+ require 'json'
5
+ require 'yaml'
6
+ require 'nokogiri'
7
+ require 'active_support/inflector' # added for .underscore and .dasherize
8
+ require "thor"
9
+ require "mini_transformer/version"
10
+ require "mini_transformer/cli"
11
+ require "mini_transformer/book"
12
+ require "mini_transformer/key_list"
13
+ require "mini_transformer/entry"
14
+ require "mini_transformer/html"
15
+
16
+ module MiniTransformer
17
+ class Parser
18
+ def setup(json_input_filename, xml_input_filename, output=nil, format="html", mapping=nil)
19
+ if File.exists?(xml_input_filename) && File.exists?(json_input_filename)
20
+ xml_input_file = File.open(xml_input_filename)
21
+ json_input = File.open(json_input_filename)
22
+ json_contents = ""
23
+ json_input.each do |line|
24
+ json_contents = json_contents + line
25
+ end
26
+ json_data = JSON.parse json_contents
27
+
28
+ if File.extname(xml_input_filename) == ".xml"
29
+ @document = Nokogiri::XML(xml_input_file, nil, 'UTF-8')
30
+ end
31
+ else
32
+ raise "File Not Found."
33
+ end
34
+
35
+ unless output.nil?
36
+ # set up the ouput HTML file to write to
37
+ @output = output
38
+ end
39
+
40
+ unless mapping.nil?
41
+ @mapping = open(mapping) {|f| YAML.load(f) }
42
+ end
43
+
44
+ @book = Book.new
45
+ @book.key_list = KeyList.new
46
+
47
+ @book.title = json_data["title"] if json_data["title"]
48
+ @book.type = json_data["type"] if json_data["type"]
49
+ @book.uid = json_data["uid"] if json_data["uid"]
50
+
51
+ xml_input_file.close
52
+ json_input.close
53
+ end
54
+
55
+ def validate
56
+ if !@document.errors.empty?
57
+ raise "Malformed XML"
58
+ end
59
+ @document.errors
60
+ end
61
+
62
+ def parse
63
+ @book.id = @document.root.attribute_nodes.first.content if @document.root.attribute_nodes.size > 0
64
+ @book.introduction = @document.xpath("//Book//Introduction//Para")
65
+
66
+ keylist = @document.xpath("//Book//KeyList")
67
+ entries = @document.xpath("//Book//KeyList//Entries")
68
+
69
+ @book.key_list.name = keylist.at_xpath("//Name").text
70
+ @book.key_list.key_label = keylist.at_xpath("//KeyLabel").text
71
+ @book.key_list.description_label = keylist.at_xpath("//DescriptionLabel").text
72
+ @book.key_list.entries = Array.new
73
+
74
+ entries.children.each do |entry|
75
+ if entry.class == Nokogiri::XML::Element
76
+ book_entry = Entry.new
77
+ entry.children.each do |child|
78
+ if child.class == Nokogiri::XML::Element
79
+ if child.name == "KeyName"
80
+ book_entry.key_name = child.content
81
+ elsif child.name == "Description"
82
+ book_entry.description = child.content
83
+ end
84
+ end
85
+ end
86
+ @book.key_list.entries << book_entry
87
+ end
88
+ end
89
+ end
90
+
91
+ def to_json
92
+ json = JSON.pretty_generate @book
93
+ # Save the JSON file
94
+ if @output
95
+ File.open(@output, "w") { |f| f << json }
96
+ else
97
+ if @book.uid.nil?
98
+ File.open("output.json", "w") { |f| f << json }
99
+ else
100
+ File.open("#{@book.uid}.json", "w") { |f| f << json }
101
+ end
102
+ end
103
+ end
104
+
105
+ def to_html
106
+ html = MiniTransformer::generate_html(@book, @mapping)
107
+
108
+ # Save the HTML file
109
+ if @output
110
+ File.open(@output, "w") { |f| f << html }
111
+ else
112
+ if @book.uid.nil?
113
+ File.open("output.html", "w") { |f| f << html }
114
+ else
115
+ File.open("#{@book.uid}.html", "w") { |f| f << html }
116
+ end
117
+ end
118
+
119
+ # return the HTML file as a string
120
+ html
121
+ end
122
+
123
+ end
124
+ end
@@ -0,0 +1,17 @@
1
+ module MiniTransformer
2
+ class Book
3
+ attr_accessor :id, :title, :uid, :type, :introduction, :key_list
4
+
5
+ def to_json(*a)
6
+ {
7
+ 'uid' => self.uid,
8
+ 'type' => self.type,
9
+ 'title' => self.title,
10
+ 'id' => self.id,
11
+ 'introduction' => self.introduction,
12
+ "key-list" => self.key_list.to_json
13
+ }.to_json(*a)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ module MiniTransformer
2
+ class CLI < Thor
3
+ include Thor::Actions
4
+
5
+ desc "transform [JSON, XML, OUTPUT, FORMAT, MAPPING]", "Transform the files into format"
6
+ def transform(json, xml, output=nil, format="html", mapping=nil)
7
+ if output.nil?
8
+ output = "#{Dir.pwd}/test.html"
9
+ end
10
+
11
+ @parser = MiniTransformer::Parser.new
12
+ @parser.setup(json, xml, output, format, mapping)
13
+ @parser.validate
14
+ @parser.parse
15
+
16
+ if format == "json"
17
+ @parser.to_json
18
+ else
19
+ @parser.to_html
20
+ end
21
+
22
+ end
23
+ map %w(-t --transform) => :transform
24
+
25
+ desc "version", "Prints the bundler's version information"
26
+ def version
27
+ puts "Mini Transform version #{MiniTransformer::VERSION}"
28
+ "Mini Transform version #{MiniTransformer::VERSION}"
29
+ #Bundler.ui.info "Bundler version #{Bundler::VERSION}"
30
+ end
31
+ map %w(-v --version) => :version
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ module MiniTransformer
2
+ class Entry
3
+ attr_accessor :key_name, :description
4
+
5
+ def to_json(*a)
6
+ {
7
+ 'key-name' => self.key_name,
8
+ 'description' => self.description
9
+ }.to_json(*a)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
2
+ <xsl:output method="html" version="5.0" encoding="UTF-8" indent="yes"/>
3
+ <xsl:param name="indent-increment" select="' '"/>
4
+ <xsl:template name="newline">
5
+ <xsl:text disable-output-escaping="yes">
6
+ </xsl:text>
7
+ </xsl:template>
8
+
9
+ <xsl:template match="comment() | processing-instruction()">
10
+ <xsl:param name="indent" select="''"/>
11
+ <xsl:call-template name="newline"/>
12
+ <xsl:value-of select="$indent"/>
13
+ <xsl:copy />
14
+ </xsl:template>
15
+
16
+ <xsl:template match="text()">
17
+ <xsl:param name="indent" select="''"/>
18
+ <xsl:call-template name="newline"/>
19
+ <xsl:value-of select="$indent"/>
20
+ <xsl:value-of select="normalize-space(.)"/>
21
+ </xsl:template>
22
+
23
+ <xsl:template match="text()[normalize-space(.)='']"/>
24
+
25
+ <xsl:template match="*">
26
+ <xsl:param name="indent" select="''"/>
27
+ <xsl:call-template name="newline"/>
28
+ <xsl:value-of select="$indent"/>
29
+ <xsl:choose>
30
+ <xsl:when test="count(child::*) > 0">
31
+ <xsl:copy>
32
+ <xsl:copy-of select="@*"/>
33
+ <xsl:apply-templates select="*|text()">
34
+ <xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/>
35
+ </xsl:apply-templates>
36
+ <xsl:call-template name="newline"/>
37
+ <xsl:value-of select="$indent"/>
38
+ </xsl:copy>
39
+ </xsl:when>
40
+ <xsl:otherwise>
41
+ <xsl:copy-of select="."/>
42
+ </xsl:otherwise>
43
+ </xsl:choose>
44
+ </xsl:template>
45
+ </xsl:stylesheet>
@@ -0,0 +1,94 @@
1
+ module MiniTransformer
2
+
3
+ def self.generate_html(resource, mapping=nil)
4
+ #puts "generate html"
5
+ builder = Nokogiri::HTML::Builder.new(:encoding => 'utf-8') do |doc|
6
+ doc.html(:lang=> "en") {
7
+ doc.head {
8
+ doc.meta(:id => "book-resource-type", :content => resource.type)
9
+ doc.meta(:id => "identifier", :content => "//apple_ref/doc/uid/#{resource.uid}")
10
+ doc.title(resource.title)
11
+ }
12
+
13
+ doc.body() {
14
+ doc.header {
15
+ doc.text "\n"
16
+ resource.introduction.children.each do |para|
17
+ doc.p(para.text)
18
+ end
19
+ doc.text "\n"
20
+ }
21
+
22
+ doc.text "\n"
23
+
24
+ doc.details {
25
+ doc.text "\n"
26
+ doc.summary(:class => "key-list-name") {doc.text resource.key_list.name}
27
+ doc.text "\n"
28
+ }
29
+
30
+ doc.div(:class => "key-list") {
31
+ doc.text "\n"
32
+ doc.span(:class => "key-label") {
33
+ doc.text resource.key_list.key_label
34
+ }
35
+ doc.text "\n"
36
+ doc.span(:class => "description-label") {
37
+ doc.text resource.key_list.description_label
38
+ }
39
+ doc.text "\n"
40
+
41
+ if mapping
42
+ doc.send(mapping['entries'], :class => 'entries') {
43
+ resource.key_list.entries.each do |entry|
44
+ if mapping['entry']
45
+ doc.send(mapping['entry'], :class => 'entry') {
46
+ doc.send(mapping['key-name'], :class => 'key-name') {
47
+ doc.text entry.key_name
48
+ }
49
+ doc.send(mapping['key-description'], :class => 'key-description') {
50
+ doc.text entry.description
51
+ }
52
+ }
53
+ else
54
+ doc.send(mapping['key-name'], :class => 'key-name') {
55
+ doc.text entry.key_name
56
+ }
57
+ doc.send(mapping['key-description'], :class => 'key-description') {
58
+ doc.text entry.description
59
+ }
60
+ end
61
+ end
62
+ }
63
+ else
64
+ doc.dl(:class => "entries") {
65
+ resource.key_list.entries.each do |entry|
66
+ doc.dt(:class => 'key-name') {
67
+ doc.text entry.key_name
68
+ }
69
+ doc.dd(:class => 'key-description') {
70
+ doc.text entry.description
71
+ }
72
+ end
73
+ }
74
+ end
75
+ }
76
+ }
77
+ }
78
+ end
79
+
80
+ html = builder.to_html
81
+
82
+ # Use an XSLT Stylesheet to pretty generate the HTML file. Adjustments follow.
83
+ xsl = Nokogiri::XSLT(File.read(File.join(File.dirname(__FILE__), "format.xsl")))
84
+ html = xsl.apply_to(Nokogiri::HTML html).to_s
85
+
86
+ # Nokogiri's HTML Builder creates an HTML4 file and cannot be easily changed due to DTD Validation
87
+ html = html.gsub(/<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd">\n/, '<!DOCTYPE html>')
88
+
89
+ # Realign the meta tag that is added by the XSLT transform with Nokogiri
90
+ html = html.gsub(/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\">\s+$/, ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8">')
91
+ html = html.gsub(/UTF-8/, 'utf-8')
92
+ end
93
+
94
+ end
@@ -0,0 +1,14 @@
1
+ module MiniTransformer
2
+ class KeyList
3
+ attr_accessor :name, :key_label, :description_label, :entries
4
+
5
+ def to_json(*a)
6
+ {
7
+ 'name' => self.name,
8
+ 'key-label' => self.key_label,
9
+ 'description-label' => self.description_label#,
10
+ }.to_json(*a)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module MiniTransformer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mini_transformer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mini_transformer"
7
+ s.version = MiniTransformer::VERSION
8
+ s.authors = ["John Wang"]
9
+ s.email = ["john@johntwang.com"]
10
+ s.homepage = "https://github.com/jwang/mini_transformer"
11
+ s.summary = %q{A converter for XML and JSON to HTML and JSON.}
12
+ s.description = %q{A configurable converter for XML and JSON to HTML and JSON.}
13
+
14
+ s.rubyforge_project = "mini_transformer"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ #s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.executables = %w(mini_transform)
20
+ s.require_paths = ["lib"]
21
+ # specify any dependencies here; for example:
22
+
23
+ s.add_development_dependency "rspec"
24
+ s.add_development_dependency 'guard-rspec'
25
+ s.add_development_dependency 'mocha'
26
+ #s.add_development_dependency 'rb-fsevent'
27
+ #s.add_development_dependency 'growl_notify'
28
+ s.add_development_dependency 'rake'
29
+ s.add_development_dependency 'simplecov'
30
+
31
+ s.add_runtime_dependency "nokogiri"
32
+ s.add_runtime_dependency 'activesupport'
33
+ s.add_runtime_dependency 'i18n'
34
+ s.add_runtime_dependency 'json'
35
+ s.add_runtime_dependency "thor"
36
+ end
@@ -0,0 +1,31 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
+ <meta id="book-resource-type" content="Reference">
6
+ <meta id="identifier" content="//apple_ref/doc/uid/TP40010215">
7
+ <title>Example of a Key List (a list of key/value pairs)</title>
8
+ </head>
9
+ <body>
10
+ <header>
11
+ <p>This book describes some common User Interface elements.</p>
12
+ <p>Use standard elements in favor of custom elements.
13
+ Do not create custom elements without a good reason.</p>
14
+ </header>
15
+ <details>
16
+ <summary class="key-list-name">Standard symbols for use in menus</summary>
17
+ </details>
18
+ <div class="key-list">
19
+ <span class="key-label">Symbol</span>
20
+ <span class="description-label">Meaning</span>
21
+ <dl class="entries">
22
+ <dt class="key-name">✓</dt>
23
+ <dd class="key-description">In the Window menu, the active document; in other menus, a setting that applies to the entire selection</dd>
24
+ <dt class="key-name">–</dt>
25
+ <dd class="key-description">A setting that applies to only part of the selection</dd>
26
+ <dt class="key-name">•</dt>
27
+ <dd class="key-description">A window with unsaved changes (typically, when Auto Save is not available)</dd>
28
+ </dl>
29
+ </div>
30
+ </body>
31
+ </html>
@@ -0,0 +1,4 @@
1
+ <root>
2
+ <open>foo
3
+ <closed>bar</closed>
4
+ </root>
@@ -0,0 +1,5 @@
1
+ {
2
+ "title" : "Example of a Key List (a list of key/value pairs)",
3
+ "type" : "Reference",
4
+ "uid" : "TP40010215"
5
+ }
@@ -0,0 +1,27 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+ <Book id="23">
3
+ <Introduction>
4
+ <Para>This book describes some common User Interface elements.</Para>
5
+ <Para>Use standard elements in favor of custom elements.
6
+ Do not create custom elements without a good reason.</Para>
7
+ </Introduction>
8
+ <KeyList>
9
+ <Name>Standard symbols for use in menus</Name>
10
+ <KeyLabel>Symbol</KeyLabel>
11
+ <DescriptionLabel>Meaning</DescriptionLabel>
12
+ <Entries>
13
+ <Entry>
14
+ <KeyName>✓</KeyName>
15
+ <Description>In the Window menu, the active document; in other menus, a setting that applies to the entire selection</Description>
16
+ </Entry>
17
+ <Entry>
18
+ <KeyName>–</KeyName>
19
+ <Description>A setting that applies to only part of the selection</Description>
20
+ </Entry>
21
+ <Entry>
22
+ <KeyName>•</KeyName>
23
+ <Description>A window with unsaved changes (typically, when Auto Save is not available)</Description>
24
+ </Entry>
25
+ </Entries>
26
+ </KeyList>
27
+ </Book>
@@ -0,0 +1,3 @@
1
+ entries: dl
2
+ key-name: dt
3
+ key-description: dd
@@ -0,0 +1,4 @@
1
+ {
2
+ "title" : "Example of a Key List (a list of key/value pairs)",
3
+ "type" : "Reference"
4
+ }
@@ -0,0 +1,3 @@
1
+ entries: ol
2
+ key-name: li
3
+ key-description: li
@@ -0,0 +1,4 @@
1
+ entries: table
2
+ entry: tr
3
+ key-name: td
4
+ key-description: td
@@ -0,0 +1,3 @@
1
+ entries: ul
2
+ key-name: li
3
+ key-description: li
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+ require 'mini_transformer'
3
+ require 'mini_transformer/book'
4
+
5
+ describe "Book" do
6
+
7
+ context "Given a new Book" do
8
+ it "should respond to all Book attributes" do
9
+ @book = MiniTransformer::Book.new
10
+ expect { @book.title }.to_not raise_error
11
+ @book.respond_to?(:title).should be_true
12
+ @book.respond_to?(:id).should be_true
13
+ @book.respond_to?(:uid).should be_true
14
+ @book.respond_to?(:introduction).should be_true
15
+ @book.respond_to?(:key_list).should be_true
16
+ @book.respond_to?(:type).should be_true
17
+
18
+ @book.respond_to?(:adfadfasfasdff).should be_false
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'mini_transformer'
3
+ require 'mini_transformer/cli'
4
+
5
+ describe "CLI" do
6
+ it "should check for a version" do
7
+ mini_transform :version, :exitstatus => true
8
+ @exitstatus.should eq(0)
9
+ out.should == "Mini Transform version 0.0.1"
10
+ end
11
+
12
+ it "should be able to check version with -v" do
13
+ mini_transform "-v"
14
+ out.should == "Mini Transform version 0.0.1"
15
+ end
16
+
17
+ it "should be able to check version with --version" do
18
+ mini_transform "--version"
19
+ out.should == "Mini Transform version 0.0.1"
20
+ end
21
+ end
22
+
23
+ describe "MiniTransformer::CLI" do
24
+
25
+ before(:all) do
26
+ @cli = MiniTransformer::CLI.new
27
+ end
28
+
29
+ it "should check version" do
30
+ @cli.version.should == "Mini Transform version 0.0.1"
31
+ end
32
+
33
+ it "should run a minitransformer with input.json input.xml" do
34
+ @cli.transform("#{FIXTURES_DIR}/input.json", "#{FIXTURES_DIR}/input.xml")
35
+ end
36
+
37
+ it "should raise an error if the either the input.json or input.xml cannot be found" do
38
+ expect { @cli.transform("input.json", "input.xml") }.to raise_error(RuntimeError, "File Not Found.")
39
+ end
40
+
41
+
42
+ it "should be able to export out to json if format=json is given" do
43
+ @cli.transform("#{FIXTURES_DIR}/input.json", "#{FIXTURES_DIR}/input.xml", "ouput.json", "json")
44
+ end
45
+
46
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'mini_transformer'
3
+ require 'mini_transformer/entry'
4
+
5
+ describe "Entry" do
6
+
7
+ context "Given a new Entry" do
8
+ it "should respond to all Entry attributes" do
9
+ @entry = MiniTransformer::Entry.new
10
+ expect { @entry.key_name }.to_not raise_error
11
+ @entry.respond_to?(:key_name).should be_true
12
+ @entry.respond_to?(:description).should be_true
13
+ end
14
+
15
+ it "should convert to JSON" do
16
+ @entry = MiniTransformer::Entry.new
17
+ @entry.key_name = "-"
18
+ @entry.description = "A setting that applies to only part of the selection"
19
+ json = @entry.to_json
20
+ expect { JSON.parse json }.to_not raise_error(JSON::JSONError)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ require 'mini_transformer'
3
+ require 'mini_transformer/key_list'
4
+
5
+ describe "KeyList" do
6
+ context "Given a new KeyList" do
7
+ it "should respond to all KeyList attributes" do
8
+ @key_list = MiniTransformer::KeyList.new
9
+ expect { @key_list.name }.to_not raise_error
10
+ @key_list.respond_to?(:name).should be_true
11
+ @key_list.respond_to?(:key_label).should be_true
12
+ @key_list.respond_to?(:description_label).should be_true
13
+ @key_list.respond_to?(:entries).should be_true
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,146 @@
1
+ require 'spec_helper'
2
+ require 'mini_transformer'
3
+ require 'nokogiri'
4
+
5
+ describe MiniTransformer do
6
+ FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
7
+ describe "Version" do
8
+ it "should have a version" do
9
+ MiniTransformer::VERSION.should_not be_nil
10
+ end
11
+ end
12
+
13
+ describe "Parser" do
14
+ before(:all) do
15
+ @parser = MiniTransformer::Parser.new
16
+ @file = File.new(File.join(FIXTURES_DIR, "input.xml"), 'rb')
17
+ @json = File.new(File.join(FIXTURES_DIR, "input.json"), 'rb')
18
+ end
19
+
20
+ it "should Ensure Valid file" do
21
+ badly_formed = <<-EOXML
22
+ <root>
23
+ <open>foo
24
+ <closed>bar</closed>
25
+ </root>
26
+ EOXML
27
+
28
+ bad_doc = Nokogiri::XML badly_formed
29
+ end
30
+
31
+ it "should raise exception if passed in a bad file" do
32
+ expect { @parser.setup(@json, "input.xml") }.to raise_error(RuntimeError, "File Not Found.")
33
+ end
34
+
35
+ it "should setup with good XML" do
36
+ expect { @parser.setup(@json.path, @file.path) }.to_not raise_error
37
+ end
38
+
39
+ it "should validate the XML format before continuing" do
40
+ @parser.setup(@json.path, @file.path)
41
+ @parser.validate.should be_empty
42
+ end
43
+
44
+ it "should have errors on invalid XML" do
45
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad_xml.xml"), 'rb')
46
+ @parser.setup(@json.path, @bad_file.path)
47
+ expect { @parser.validate }.to raise_error(RuntimeError, "Malformed XML")
48
+ end
49
+
50
+ it "should parse" do
51
+ @parser.setup(@json.path, @file.path)
52
+ @parser.validate
53
+ @parser.parse
54
+ end
55
+
56
+ context "HTML" do
57
+ it "should create a HTML doc" do
58
+ @result_file = File.open(File.join(FIXTURES_DIR, "TP40010215.html"), 'rb:UTF-8')
59
+ @parser.setup(@json.path, @file.path)
60
+ @parser.validate
61
+ @parser.parse
62
+
63
+ @result_doc = ""
64
+ @result_file.each do |line|
65
+ @result_doc = @result_doc + line
66
+ end
67
+ @result_file.close
68
+ @parser.to_html.should eq(@result_doc)
69
+ end
70
+
71
+ it "should create an output.html file if UID is not present in JSON" do
72
+ json_uid = File.new(File.join(FIXTURES_DIR, "no_uid.json"), 'rb')
73
+ @parser.setup(json_uid.path, @file.path)
74
+ @parser.validate
75
+ @parser.parse
76
+ @parser.to_html
77
+ File.exists?('output.html').should be_true
78
+ end
79
+
80
+ end
81
+
82
+ context "JSON Format" do
83
+ it "should export to JSON with uid if filename is nil" do
84
+ @parser.setup(@json.path, @file.path, nil, 'json')
85
+ @parser.validate
86
+ @parser.parse
87
+ @parser.to_json
88
+ File.exists?('TP40010215.json').should be_true
89
+ end
90
+
91
+ it "should export to output.json if no UID exists in json input" do
92
+ json_uid = File.new(File.join(FIXTURES_DIR, "no_uid.json"), 'rb')
93
+ @parser.setup(json_uid.path, @file.path, nil, 'json')
94
+ @parser.validate
95
+ @parser.parse
96
+ @parser.to_json
97
+ File.exists?('output.json').should be_true
98
+ end
99
+
100
+ it "should export to JSON if the format specified is 'json'" do
101
+ @parser.setup(@json.path, @file.path, "output.json", 'json')
102
+ @parser.validate
103
+ @parser.parse
104
+ @parser.to_json
105
+ File.exists?('output.json').should be_true
106
+ end
107
+
108
+ end
109
+
110
+ context "Mappings" do
111
+ it "should accept a valid mapping for the children" do
112
+ mapping = File.open(File.join(FIXTURES_DIR, "mapping.yml"), 'rb:UTF-8')
113
+ @parser.setup(@json.path, @file.path, "altered.html", 'html', mapping.path)
114
+ @parser.validate
115
+ @parser.parse
116
+ @parser.to_html
117
+ end
118
+
119
+ it "should accept a valid ol mapping for the children" do
120
+ mapping = File.open(File.join(FIXTURES_DIR, "ol_mapping.yml"), 'rb:UTF-8')
121
+ @parser.setup(@json.path, @file.path, "ol.html", 'html', mapping.path)
122
+ @parser.validate
123
+ @parser.parse
124
+ @parser.to_html
125
+ end
126
+
127
+ it "should accept a valid ul mapping for the children" do
128
+ mapping = File.open(File.join(FIXTURES_DIR, "ul_mapping.yml"), 'rb:UTF-8')
129
+ @parser.setup(@json.path, @file.path, "ul.html", 'html', mapping.path)
130
+ @parser.validate
131
+ @parser.parse
132
+ @parser.to_html
133
+ end
134
+
135
+ it "should accept a valid table mapping for the children" do
136
+ mapping = File.open(File.join(FIXTURES_DIR, "table_mapping.yml"), 'rb:UTF-8')
137
+ @parser.setup(@json.path, @file.path, "table.html", 'html', mapping.path)
138
+ @parser.validate
139
+ @parser.parse
140
+ @parser.to_html
141
+ end
142
+ end
143
+
144
+ end
145
+
146
+ end
@@ -0,0 +1,34 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+ $:.unshift File.expand_path('..', __FILE__)
6
+ $:.unshift File.expand_path('../../lib', __FILE__)
7
+ require 'rspec'
8
+ require 'mini_transformer'
9
+
10
+ # Require the correct version of popen for the current platform
11
+ if RbConfig::CONFIG['host_os'] =~ /mingw|mswin/
12
+ begin
13
+ require 'win32/open3'
14
+ rescue LoadError
15
+ abort "Run `gem install win32-open3` to be able to run specs"
16
+ end
17
+ else
18
+ require 'open3'
19
+ end
20
+
21
+ Dir["#{File.expand_path('../support', __FILE__)}/*.rb"].each do |file|
22
+ require file
23
+ end
24
+
25
+ RSpec.configure do |config|
26
+ #config.include Spec::Builders
27
+ config.include Spec::Helpers
28
+ #config.include Spec::Indexes
29
+ #config.include Spec::Matchers
30
+ #config.include Spec::Path
31
+ #config.include Spec::Rubygems
32
+ #config.include Spec::Platforms
33
+ #config.include Spec::Sudo
34
+ end
@@ -0,0 +1,57 @@
1
+ module Spec
2
+ module Helpers
3
+ attr_reader :out, :err, :exitstatus
4
+
5
+ def lib
6
+ File.expand_path('../../../lib', __FILE__)
7
+ end
8
+
9
+ def mini_transform(cmd, options = {})
10
+ #p "mini transforming"
11
+ expect_err = options.delete(:expect_err)
12
+ exitstatus = options.delete(:exitstatus)
13
+ #options["no-color"] = true unless options.key?("no-color") || cmd.to_s[0..3] == "exec"
14
+
15
+ mini_transform = File.expand_path('../../../bin/mini_transform', __FILE__)
16
+
17
+ #requires = options.delete(:requires) || []
18
+ #requires << File.expand_path('../fakeweb/'+options.delete(:fakeweb)+'.rb', __FILE__) if options.key?(:fakeweb)
19
+ #requires << File.expand_path('../artifice/'+options.delete(:artifice)+'.rb', __FILE__) if options.key?(:artifice)
20
+ #requires_str = requires.map{|r| "-r#{r}"}.join(" ")
21
+
22
+ #env = (options.delete(:env) || {}).map{|k,v| "#{k}='#{v}' "}.join
23
+ args = options.map do |k,v|
24
+ v == true ? " --#{k}" : " --#{k} #{v}" if v
25
+ end.join
26
+
27
+ cmd = "#{Gem.ruby} -I#{lib} #{mini_transform} #{cmd}#{args}"
28
+ #p "cmd: #{cmd}"
29
+ if exitstatus
30
+ sys_status(cmd)
31
+ else
32
+ sys_exec(cmd, expect_err){|i| yield i if block_given? }
33
+ end
34
+ end
35
+
36
+ def sys_status(cmd)
37
+ @err = nil
38
+ @out = %x{#{cmd}}.strip
39
+ @exitstatus = $?.exitstatus
40
+ end
41
+
42
+ def sys_exec(cmd, expect_err = false)
43
+ Open3.popen3(cmd.to_s) do |stdin, stdout, stderr|
44
+ @in_p, @out_p, @err_p = stdin, stdout, stderr
45
+
46
+ yield @in_p if block_given?
47
+ @in_p.close
48
+ @out = @out_p.read_available_bytes.strip unless @out_p.closed?
49
+ @err = @err_p.read_available_bytes.strip unless @err_p.closed?
50
+ end
51
+
52
+ puts @err unless expect_err || @err.empty? || !$show_err
53
+ @out
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,20 @@
1
+ class IO
2
+ def read_available_bytes(chunk_size = 16384, select_timeout = 0.02)
3
+ buffer = []
4
+
5
+ return "" if closed? || eof?
6
+ # IO.select cannot be used here due to the fact that it
7
+ # just does not work on windows
8
+ while true
9
+ begin
10
+ IO.select([self], nil, nil, select_timeout)
11
+ break if eof? # stop raising :-(
12
+ buffer << self.readpartial(chunk_size)
13
+ rescue(EOFError)
14
+ break
15
+ end
16
+ end
17
+
18
+ return buffer.join
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,208 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_transformer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Wang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70175232360960 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70175232360960
25
+ - !ruby/object:Gem::Dependency
26
+ name: guard-rspec
27
+ requirement: &70175232360540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70175232360540
36
+ - !ruby/object:Gem::Dependency
37
+ name: mocha
38
+ requirement: &70175232360120 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70175232360120
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &70175232359640 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70175232359640
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &70175232359160 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70175232359160
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: &70175232358680 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70175232358680
80
+ - !ruby/object:Gem::Dependency
81
+ name: activesupport
82
+ requirement: &70175232358120 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: *70175232358120
91
+ - !ruby/object:Gem::Dependency
92
+ name: i18n
93
+ requirement: &70175232357500 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: *70175232357500
102
+ - !ruby/object:Gem::Dependency
103
+ name: json
104
+ requirement: &70175232356540 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: *70175232356540
113
+ - !ruby/object:Gem::Dependency
114
+ name: thor
115
+ requirement: &70175232356120 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :runtime
122
+ prerelease: false
123
+ version_requirements: *70175232356120
124
+ description: A configurable converter for XML and JSON to HTML and JSON.
125
+ email:
126
+ - john@johntwang.com
127
+ executables:
128
+ - mini_transform
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - .rvmrc
134
+ - .travis.yml
135
+ - Gemfile
136
+ - Guardfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/mini_transform
141
+ - lib/mini_transformer.rb
142
+ - lib/mini_transformer/book.rb
143
+ - lib/mini_transformer/cli.rb
144
+ - lib/mini_transformer/entry.rb
145
+ - lib/mini_transformer/format.xsl
146
+ - lib/mini_transformer/html.rb
147
+ - lib/mini_transformer/key_list.rb
148
+ - lib/mini_transformer/version.rb
149
+ - mini_transformer.gemspec
150
+ - spec/fixtures/TP40010215.html
151
+ - spec/fixtures/bad_xml.xml
152
+ - spec/fixtures/input.json
153
+ - spec/fixtures/input.xml
154
+ - spec/fixtures/mapping.yml
155
+ - spec/fixtures/no_uid.json
156
+ - spec/fixtures/ol_mapping.yml
157
+ - spec/fixtures/table_mapping.yml
158
+ - spec/fixtures/ul_mapping.yml
159
+ - spec/mini_transformer/book_spec.rb
160
+ - spec/mini_transformer/cli_spec.rb
161
+ - spec/mini_transformer/entry_spec.rb
162
+ - spec/mini_transformer/key_list_spec.rb
163
+ - spec/mini_transformer_spec.rb
164
+ - spec/spec_helper.rb
165
+ - spec/support/helpers.rb
166
+ - spec/support/ruby_ext.rb
167
+ homepage: https://github.com/jwang/mini_transformer
168
+ licenses: []
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ none: false
175
+ requirements:
176
+ - - ! '>='
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ none: false
181
+ requirements:
182
+ - - ! '>='
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project: mini_transformer
187
+ rubygems_version: 1.8.10
188
+ signing_key:
189
+ specification_version: 3
190
+ summary: A converter for XML and JSON to HTML and JSON.
191
+ test_files:
192
+ - spec/fixtures/TP40010215.html
193
+ - spec/fixtures/bad_xml.xml
194
+ - spec/fixtures/input.json
195
+ - spec/fixtures/input.xml
196
+ - spec/fixtures/mapping.yml
197
+ - spec/fixtures/no_uid.json
198
+ - spec/fixtures/ol_mapping.yml
199
+ - spec/fixtures/table_mapping.yml
200
+ - spec/fixtures/ul_mapping.yml
201
+ - spec/mini_transformer/book_spec.rb
202
+ - spec/mini_transformer/cli_spec.rb
203
+ - spec/mini_transformer/entry_spec.rb
204
+ - spec/mini_transformer/key_list_spec.rb
205
+ - spec/mini_transformer_spec.rb
206
+ - spec/spec_helper.rb
207
+ - spec/support/helpers.rb
208
+ - spec/support/ruby_ext.rb