kramdown-tablerize 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b83c03bc46d69faff77aee6e5eab7947a4c9cf08
4
+ data.tar.gz: b2c3def4fcaa0fb5a18343a18fa10c0a19e9284d
5
+ SHA512:
6
+ metadata.gz: f6cd7b548ad410e5270f6366029a92fea99bb39013b0dae51cf7bdbdac74cb8206236233dd5a337bc28efd70ba8c8093d1b2b4d2d99af9340bf4c95c4b5bed35
7
+ data.tar.gz: 077504cd4575d3e1bb7e4d0878475c74151a13937c20f0ae9f3d5f89bf8d064fed6ac972a8e281d4ff71bd07f081fa0aeae0f83077b88b1e6f10d1212c2cd0c0
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /kramdown-tablerize-*.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format=documentation
3
+ --tty
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ script: rspec
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - ruby-head
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 IFTTT Inc
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.
data/Makefile ADDED
@@ -0,0 +1,15 @@
1
+ GEM_NAME := kramdown-tablerize
2
+
3
+ all:
4
+ gem build $(GEM_NAME).gemspec
5
+
6
+ install: all
7
+ gem install $(GEM_NAME)
8
+
9
+ uninstall:
10
+ gem uninstall $(GEM_NAME)
11
+
12
+ clean:
13
+ rm -fv -- $(GEM_NAME)-*.gem
14
+
15
+ .PHONY: all install clean
data/README.md ADDED
@@ -0,0 +1,134 @@
1
+ kramdown-tablerize
2
+ ==================
3
+
4
+ A [kramdown] plugin to convert YAML to HTML tables inside Markdown, using
5
+ [Tablerize].
6
+
7
+ [kramdown]: http://kramdown.gettalong.org/
8
+ [Tablerize]: https://github.com/IFTTT/tablerize
9
+
10
+
11
+ ## Usage
12
+
13
+ To install:
14
+
15
+ ```shell
16
+ cd kramdown-tablerize
17
+ make install
18
+ # or (soon)
19
+ gem install kramdown-tablerize
20
+ ```
21
+
22
+ Then, in Ruby...
23
+
24
+ ```ruby
25
+ require 'kramdown-tablerize'
26
+
27
+ Kramdown::Document.new(content, input: 'KramdownYamlTablerize').to_html
28
+ ```
29
+
30
+ ...and in Markdown
31
+
32
+ ```markdown
33
+ ## Surveillance conducted pursuant to Titles I, III, IV and VII of FISA
34
+
35
+ --- table ---
36
+
37
+ class: [statistics-table, nsa-surveillance-details]
38
+
39
+ cols:
40
+ - name: authority
41
+ - name: num_orders
42
+ - name: num_targets
43
+
44
+ data:
45
+ - class: table-header
46
+ authority: Legal Authority
47
+ num_orders: Annual Number of Orders
48
+ num_targets: Estimated Number of Targets Affected
49
+ - authority: |
50
+ __FISA Orders__
51
+ Based on probable cause
52
+ (Title I and III of FISA, Sections 703 and 704 of FISA)
53
+ num_orders: "1,167 orders"
54
+ num_targets: "1,144"
55
+ - authority: |
56
+ __Section 702__
57
+ of FISA
58
+ num_orders: "1 order"
59
+ num_targets: "89,138"
60
+ - authority: |
61
+ __FISA Pen Register/Trap and Trace__
62
+ (Title IV of FISA)
63
+ num_orders: "131 orders"
64
+ num_targets: "319"
65
+
66
+ --- /table ---
67
+
68
+ wow such surveillance
69
+ ```
70
+
71
+
72
+ ## Tips
73
+
74
+ - In YAML, leading indent doesn't matter, so you might want to indent
75
+ everything (as shown above) so that it renders as a code block in naive
76
+ Markdown implementations.
77
+
78
+ - You currently can't nest `--- table ---`…`--- /table ---` blocks
79
+ (the parser doesn't keep track of nesting levels). However, you can nest
80
+ tables within each other within the YAML itself:
81
+
82
+ ```markdown
83
+ --- table ---
84
+
85
+ cols:
86
+ - name: 1
87
+ - name: 2
88
+ data:
89
+ - 1: cell 1,1
90
+ 2: cell 1,2
91
+ - 1: cell 2,1
92
+ 2:
93
+ cols:
94
+ - name: a
95
+ - name: b
96
+ data:
97
+ - a: "wow it's"
98
+ - b: a nested table
99
+
100
+ --- /table ---
101
+ ```
102
+
103
+
104
+ ## Wish List
105
+
106
+ - Support using custom delimiters for the start and end of the YAML. Find a
107
+ custom delimiter that plays nicely with kramdown and markdown, i.e. one that
108
+ renders the best when the plugin is not enabled. It should ideally also be
109
+ easy for text editors to target in case anyone wants to make a syntax
110
+ highlighter for it (which should involve little more than marking off a
111
+ region of Markdown as YAML).
112
+
113
+ - Allow outputting to Markdown, for GitHub and other sites that don't allow
114
+ HTML in Markdown.
115
+
116
+ - Improve interactive error handling, including outputting on which line of
117
+ kramdown source the error occurred. Possibly also do some pre-emptive error
118
+ checking for less confusion down the line.
119
+
120
+ - After the delimiters are finalized, add support to syntax-highlight YAML
121
+ inside Markdown.
122
+
123
+
124
+ ## Credit
125
+
126
+ The structure of [rfc1459/kramdown-gist] was used as a guideline for making this
127
+ library.
128
+
129
+ **kramdown-tablerize** was originally designed and written by [@szhu] at
130
+ [@IFTTT].
131
+
132
+ [rfc1459/kramdown-gist]: https://github.com/rfc1459/kramdown-gist
133
+ [@szhu]: https://github.com/szhu
134
+ [@IFTTT]: https://github.com/IFTTT
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # Command-line interface to the kramdown-tablerize gem.
3
+
4
+ if ARGV.empty?
5
+ $stderr.puts "usage: #{$PROGRAM_NAME} path/to/document.md [...]"
6
+ exit 1
7
+ end
8
+
9
+ begin
10
+ require_relative '../lib/kramdown-tablerize'
11
+ rescue LoadError
12
+ require 'kramdown-tablerize'
13
+ end
14
+
15
+ ARGV.each do |path|
16
+ puts Kramdown::Document.new(File.read(path), input: 'KramdownYamlTablerize').to_html
17
+ end
@@ -0,0 +1,14 @@
1
+ <h2 id="surveillance-conducted-pursuant-to-titles-i-iii-iv-and-vii-of-fisa">Surveillance conducted pursuant to Titles I, III, IV and VII of FISA</h2>
2
+
3
+ <table class="statistics-table nsa-surveillance-details"><tbody>
4
+ <tr class="table-header"><td class="statistics-table-authority">Legal Authority</td><td class="statistics-table-num_orders">Annual Number of Orders</td><td class="statistics-table-num_targets">Estimated Number of Targets Affected</td></tr>
5
+ <tr><td class="statistics-table-authority"><strong>FISA Orders</strong>
6
+ Based on probable cause
7
+ (Title I and III of FISA, Sections 703 and 704 of FISA)</td><td class="statistics-table-num_orders">1,167 orders</td><td class="statistics-table-num_targets">1,144</td></tr>
8
+ <tr><td class="statistics-table-authority"><strong>Section 702</strong>
9
+ of FISA</td><td class="statistics-table-num_orders">1 order</td><td class="statistics-table-num_targets">89,138</td></tr>
10
+ <tr><td class="statistics-table-authority"><strong>FISA Pen Register/Trap and Trace</strong>
11
+ (Title IV of FISA)</td><td class="statistics-table-num_orders">131 orders</td><td class="statistics-table-num_targets">319</td></tr>
12
+ </tbody></table>
13
+
14
+ <p>wow such surveillance</p>
@@ -0,0 +1,36 @@
1
+ ## Surveillance conducted pursuant to Titles I, III, IV and VII of FISA
2
+
3
+ --- table ---
4
+
5
+ class: [statistics-table, nsa-surveillance-details]
6
+
7
+ cols:
8
+ - name: authority
9
+ - name: num_orders
10
+ - name: num_targets
11
+
12
+ data:
13
+ - class: table-header
14
+ authority: Legal Authority
15
+ num_orders: Annual Number of Orders
16
+ num_targets: Estimated Number of Targets Affected
17
+ - authority: |
18
+ __FISA Orders__
19
+ Based on probable cause
20
+ (Title I and III of FISA, Sections 703 and 704 of FISA)
21
+ num_orders: "1,167 orders"
22
+ num_targets: "1,144"
23
+ - authority: |
24
+ __Section 702__
25
+ of FISA
26
+ num_orders: "1 order"
27
+ num_targets: "89,138"
28
+ - authority: |
29
+ __FISA Pen Register/Trap and Trace__
30
+ (Title IV of FISA)
31
+ num_orders: "131 orders"
32
+ num_targets: "319"
33
+
34
+ --- /table ---
35
+
36
+ wow such surveillance
@@ -0,0 +1,17 @@
1
+ .*
2
+ Tablerize YAML error for the following table:
3
+ .*
4
+ 1
5
+ 2
6
+ 3 class: \[statistics-table, nsa-surveillance-details\]
7
+ 4
8
+ \* 5 cols
9
+ 6 - name
10
+ 7 - num_orders
11
+ 8 - name: num_targets
12
+ 9
13
+ 10 data:
14
+ backtrace:
15
+ .*
16
+ could not find expected ':' while scanning a simple key at line 5 column 5
17
+ .*
@@ -0,0 +1,36 @@
1
+ ## Surveillance conducted pursuant to Titles I, III, IV and VII of FISA
2
+
3
+ --- table ---
4
+
5
+ class: [statistics-table, nsa-surveillance-details]
6
+
7
+ cols
8
+ - name
9
+ - num_orders
10
+ - name: num_targets
11
+
12
+ data:
13
+ - class: table-header
14
+ authority: Legal Authority
15
+ num_orders: Annual Number of Orders
16
+ num_targets: Estimated Number of Targets Affected
17
+ - authority: |
18
+ __FISA Orders__
19
+ Based on probable cause
20
+ (Title I and III of FISA, Sections 703 and 704 of FISA)
21
+ num_orders: "1,167 orders"
22
+ num_targets: "1,144"
23
+ - authority: |
24
+ __Section 702__
25
+ of FISA
26
+ num_orders: "1 order"
27
+ num_targets: "89,138"
28
+ - authority: |
29
+ __FISA Pen Register/Trap and Trace__
30
+ (Title IV of FISA)
31
+ num_orders: "131 orders"
32
+ num_targets: "319"
33
+
34
+ --- /table ---
35
+
36
+ wow such surveillance
@@ -0,0 +1,23 @@
1
+ require './lib/kramdown-tablerize/version'
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'kramdown-tablerize'
5
+ gem.summary = 'Convert YAML to HTML tables in kramdown.'
6
+ gem.description = <<-END
7
+ This kramdown plugin uses Tablerize to convert YAML tables in Markdown
8
+ to HTML.
9
+ END
10
+
11
+ gem.version = Tablerize::Plugin::Kramdown::VERSION
12
+
13
+ gem.homepage = 'https://github.com/IFTTT/kramdown-tablerize'
14
+ gem.authors = ['Sean Zhu']
15
+ gem.email = 'opensource+tablerize@szhu.me'
16
+ gem.license = 'MIT'
17
+
18
+ gem.add_dependency 'kramdown', '~> 1.2', '>= 1.2.0'
19
+ gem.add_dependency 'tablerize', '~> 1.0', '>= 1.0.0'
20
+ gem.executables = ['kramdown-tablerize']
21
+ gem.files = `git ls-files`.split
22
+ gem.require_paths = ['lib']
23
+ end
@@ -0,0 +1,56 @@
1
+ require_relative 'tablerize'
2
+
3
+ require 'kramdown'
4
+
5
+ module Kramdown
6
+ # Register :yaml_tablerize as a block-level element
7
+ # @private
8
+ class Element
9
+ # Register :yaml_tablerize as a block-level element
10
+ CATEGORY[:yaml_tablerize] = :block
11
+ end
12
+
13
+ module Converter
14
+ class Html
15
+ # Convert :yaml_tablerize -> HTML
16
+ # @api private
17
+ def convert_yaml_tablerize(el, _indent)
18
+ Tablerize::Plugin::Kramdown.convert_yaml_tablerize_to_html(el.value)
19
+ end
20
+ end
21
+
22
+ class Kramdown
23
+ # Convert :yaml_tablerize -> Markdown (not implemented)
24
+ def convert_yaml_tablerize(_el, _opts)
25
+ raise NotImplementedError
26
+ end
27
+ end
28
+
29
+ class Latex
30
+ # Convert :yaml_tablerize -> LaTEX (not implemented)
31
+ def convert_yaml_tablerize(_el, _opts)
32
+ raise NotImplementedError
33
+ end
34
+ end
35
+ end
36
+
37
+ module Parser
38
+ class KramdownYamlTablerize < ::Kramdown::Parser::Kramdown
39
+ YAML_TABLE_PATTERN = /^#{OPT_SPACE}---[ \t]*table[ \t]*---(.*?)---[ \t]*\/table[ \t]*---/m
40
+
41
+ def initialize(source, options)
42
+ super
43
+ @block_parsers.unshift(:yaml_tablerize)
44
+ end
45
+
46
+ # Convert Markdown -> :yaml_tablerize
47
+ # @api private
48
+ def parse_yaml_tablerize
49
+ @src.pos += @src.matched_size
50
+ @tree.children << Element.new(:yaml_tablerize, @src[1])
51
+ end
52
+
53
+ define_parser(:yaml_tablerize, YAML_TABLE_PATTERN)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,36 @@
1
+ require 'tablerize'
2
+
3
+ module Tablerize
4
+ module Plugin
5
+ module Kramdown
6
+ ERROR_CONTEXT_LINES = 5
7
+
8
+ # Prints out the 5 lines before and after the line where an error
9
+ # occurred. This is useful for debugging embedded YAML, as the line
10
+ # number where the error occurred in YAML won't be the same as where it
11
+ # occurred in the Markdown source.
12
+ def self.printable_error_context(text, target_line)
13
+ raw_lines = text.lines
14
+ start_line = [target_line - ERROR_CONTEXT_LINES, 1].max
15
+ end_line = [target_line + ERROR_CONTEXT_LINES, raw_lines.length].min
16
+ fmt_lines = (start_line..end_line).map do |line|
17
+ marker = line == target_line ? '*' : ' '
18
+ "#{marker} #{line} #{raw_lines[line - 1].chomp}"
19
+ end
20
+ fmt_lines.join("\n")
21
+ end
22
+
23
+ def self.convert_yaml_tablerize_to_html(yaml_tablerize)
24
+ Tablerize.load(yaml_tablerize).to_html + "\n"
25
+ rescue Psych::SyntaxError => e
26
+ e.message.prepend [
27
+ "Tablerize YAML error for the following table:",
28
+ printable_error_context(yaml_tablerize, e.line),
29
+ "backtrace:",
30
+ ''
31
+ ].join("\n")
32
+ raise e
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ module Tablerize
2
+ module Plugin
3
+ module Kramdown
4
+ VERSION = "1.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'kramdown-tablerize/version'
2
+ require_relative 'kramdown-tablerize/kramdown'
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tablerize do
4
+ ['example-1'].each do |test_name|
5
+ context test_name do
6
+ reference_result = File.read "examples/#{test_name}.html"
7
+
8
+ it "should be correctly converted" do
9
+ test_result = Kramdown::Document.new(
10
+ File.read("examples/#{test_name}.md"), input: 'KramdownYamlTablerize'
11
+ ).to_html
12
+ expect(test_result.chomp).to eq(reference_result.chomp)
13
+ end
14
+ end
15
+ end
16
+
17
+ ['example-2'].each do |test_name|
18
+ context test_name do
19
+ reference_result = File.read "examples/#{test_name}.error.out"
20
+ reference_result.gsub!(/\n?\.\*\n?/, '.*')
21
+
22
+ it "should have the appropriate error output" do
23
+ document = Kramdown::Document.new(
24
+ File.read("examples/#{test_name}.md"), input: 'KramdownYamlTablerize'
25
+ )
26
+ expect { document.to_html }.to raise_error Regexp.new(reference_result, Regexp::MULTILINE)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1 @@
1
+ require_relative '../lib/kramdown-tablerize'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kramdown-tablerize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sean Zhu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.2.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: tablerize
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.0.0
53
+ description: |2
54
+ This kramdown plugin uses Tablerize to convert YAML tables in Markdown
55
+ to HTML.
56
+ email: opensource+tablerize@szhu.me
57
+ executables:
58
+ - kramdown-tablerize
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - LICENSE
66
+ - Makefile
67
+ - README.md
68
+ - bin/kramdown-tablerize
69
+ - examples/example-1.html
70
+ - examples/example-1.md
71
+ - examples/example-2.error.out
72
+ - examples/example-2.md
73
+ - kramdown-tablerize.gemspec
74
+ - lib/kramdown-tablerize.rb
75
+ - lib/kramdown-tablerize/kramdown.rb
76
+ - lib/kramdown-tablerize/tablerize.rb
77
+ - lib/kramdown-tablerize/version.rb
78
+ - spec/kramdown-tablerize_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: https://github.com/IFTTT/kramdown-tablerize
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.2.2
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Convert YAML to HTML tables in kramdown.
104
+ test_files: []