paru 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/paru/error.rb +4 -0
- data/lib/paru/filter.rb +77 -68
- data/lib/paru/filter/alignment.rb +12 -0
- data/lib/paru/filter/ast_manipulation.rb +43 -0
- data/lib/paru/filter/attr.rb +40 -0
- data/lib/paru/filter/block.rb +11 -0
- data/lib/paru/filter/block_quote.rb +11 -0
- data/lib/paru/filter/bullet_list.rb +8 -0
- data/lib/paru/filter/citation.rb +29 -0
- data/lib/paru/filter/cite.rb +25 -0
- data/lib/paru/filter/code.rb +30 -0
- data/lib/paru/filter/code_block.rb +26 -0
- data/lib/paru/filter/definition_list.rb +19 -0
- data/lib/paru/filter/definition_list_item.rb +22 -0
- data/lib/paru/filter/div.rb +25 -0
- data/lib/paru/filter/document.rb +47 -0
- data/lib/paru/filter/emph.rb +8 -0
- data/lib/paru/filter/header.rb +28 -0
- data/lib/paru/filter/horizontal_rule.rb +11 -0
- data/lib/paru/filter/image.rb +9 -0
- data/lib/paru/filter/inline.rb +19 -0
- data/lib/paru/filter/line_break.rb +16 -0
- data/lib/paru/filter/link.rb +27 -0
- data/lib/paru/filter/list.rb +22 -0
- data/lib/paru/filter/list_attributes.rb +24 -0
- data/lib/paru/filter/markdown.rb +52 -0
- data/lib/paru/filter/math.rb +51 -0
- data/lib/paru/filter/meta.rb +24 -0
- data/lib/paru/filter/meta_blocks.rb +13 -0
- data/lib/paru/filter/meta_bool.rb +9 -0
- data/lib/paru/filter/meta_inlines.rb +13 -0
- data/lib/paru/filter/meta_list.rb +9 -0
- data/lib/paru/filter/meta_map.rb +37 -0
- data/lib/paru/filter/meta_string.rb +9 -0
- data/lib/paru/filter/meta_value.rb +20 -0
- data/lib/paru/filter/node.rb +149 -0
- data/lib/paru/filter/note.rb +17 -0
- data/lib/paru/filter/null.rb +11 -0
- data/lib/paru/filter/ordered_list.rb +23 -0
- data/lib/paru/filter/para.rb +16 -0
- data/lib/paru/filter/plain.rb +15 -0
- data/lib/paru/filter/quoted.rb +23 -0
- data/lib/paru/filter/raw_block.rb +25 -0
- data/lib/paru/filter/raw_inline.rb +30 -0
- data/lib/paru/filter/small_caps.rb +9 -0
- data/lib/paru/filter/soft_break.rb +16 -0
- data/lib/paru/filter/space.rb +15 -0
- data/lib/paru/filter/span.rb +23 -0
- data/lib/paru/filter/str.rb +23 -0
- data/lib/paru/filter/strikeout.rb +9 -0
- data/lib/paru/filter/string.rb +9 -0
- data/lib/paru/filter/subscript.rb +9 -0
- data/lib/paru/filter/superscript.rb +9 -0
- data/lib/paru/filter/table.rb +34 -0
- data/lib/paru/filter/table_row.rb +18 -0
- data/lib/paru/filter/target.rb +19 -0
- data/lib/paru/pandoc_options.yaml +4 -2
- data/lib/paru/selector.rb +151 -0
- metadata +60 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed2fa44eb537e3b82708eef4e06729a2c926554f
|
4
|
+
data.tar.gz: b4873e14f2f4cb457fd223a4ca9f2ac87ff0fae1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2c82d26a75ff0323525279b6c30969e4546e8332d2d31d60a3070b0e30cacc69db0fc6c2c7b9f83e0eab5fc587b3bbbfd6c554aa2440255720582150f5e4886
|
7
|
+
data.tar.gz: 8eacf827ff67d6ff598aeebbc9a44fa3e2ea6462d24269322dcdf507a9eee5b49d27182f101defd7e3746576bd159f3d21902d971c416e3ccd6d63db8d2d3adc
|
data/lib/paru/error.rb
ADDED
data/lib/paru/filter.rb
CHANGED
@@ -1,73 +1,82 @@
|
|
1
1
|
module Paru
|
2
2
|
|
3
|
-
|
3
|
+
require_relative "./selector"
|
4
|
+
require_relative "filter/document"
|
5
|
+
|
6
|
+
PANDOC_BLOCK = [
|
7
|
+
"Plain",
|
8
|
+
"Para",
|
9
|
+
"CodeBlock",
|
10
|
+
"RawBlock",
|
11
|
+
"BlockQuote",
|
12
|
+
"OrderedList",
|
13
|
+
"BulletList",
|
14
|
+
"DefinitionList",
|
15
|
+
"Header",
|
16
|
+
"HorizontalRule",
|
17
|
+
"Table",
|
18
|
+
"Div",
|
19
|
+
"Null"
|
20
|
+
]
|
21
|
+
PANDOC_INLINE = [
|
22
|
+
"Str",
|
23
|
+
"Emph",
|
24
|
+
"Strong",
|
25
|
+
"Strikeout",
|
26
|
+
"Superscript",
|
27
|
+
"Subscript",
|
28
|
+
"SmallCaps",
|
29
|
+
"Quoted",
|
30
|
+
"Cite",
|
31
|
+
"Space",
|
32
|
+
"SoftBreak",
|
33
|
+
"LineBreak",
|
34
|
+
"Math",
|
35
|
+
"RawInline",
|
36
|
+
"Link",
|
37
|
+
"Image",
|
38
|
+
"Note",
|
39
|
+
"Span"
|
40
|
+
]
|
41
|
+
PANDOC_TYPES = PANDOC_BLOCK + PANDOC_INLINE
|
42
|
+
|
43
|
+
class Filter
|
44
|
+
|
45
|
+
def self.run &block
|
46
|
+
Filter.new().filter(&block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def document
|
50
|
+
meta, contents = JSON.parse $stdin.read
|
51
|
+
document = PandocFilter::Document.new meta, contents
|
52
|
+
document
|
53
|
+
end
|
54
|
+
|
55
|
+
def filter &block
|
56
|
+
@selectors = Hash.new
|
57
|
+
@filtered_nodes = []
|
58
|
+
@doc = document
|
59
|
+
|
60
|
+
@doc.each_depth_first do |node|
|
61
|
+
@filtered_nodes.push node
|
62
|
+
instance_eval(&block)
|
63
|
+
end
|
64
|
+
|
65
|
+
puts @doc.to_json
|
66
|
+
end
|
67
|
+
|
68
|
+
def current_node
|
69
|
+
@filtered_nodes.last
|
70
|
+
end
|
71
|
+
|
72
|
+
def with selector
|
73
|
+
@selectors[selector] = Selector.new selector unless @selectors.has_key? selector
|
74
|
+
yield current_node if @selectors[selector].matches? current_node, @filtered_nodes
|
75
|
+
end
|
76
|
+
|
77
|
+
def metadata
|
78
|
+
@doc.meta
|
79
|
+
end
|
4
80
|
|
5
|
-
class Filter
|
6
|
-
|
7
|
-
def self.ast &filter
|
8
|
-
ast_hash = JSON.parse $stdin.read
|
9
|
-
warn ast_hash
|
10
|
-
ast_tree = yield AST.new ast_hash
|
11
|
-
$stdout.write JSON.generate ast_tree.to_h
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
class AST
|
17
|
-
|
18
|
-
def initialize ast_hash
|
19
|
-
@metadata = Meta.new ast_hash.first
|
20
|
-
@content = ast_hash.last.map {|n| Node.new n}
|
21
81
|
end
|
22
|
-
|
23
|
-
def to_h
|
24
|
-
[@metadata.to_h].push @content.map {|n| n.to_h }
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
# see
|
30
|
-
# http://hackage.haskell.org/package/pandoc-types-1.12.4.1/docs/Text-Pandoc-Definition.html
|
31
|
-
# for spec of different kinds of nodes
|
32
|
-
|
33
|
-
class Meta
|
34
|
-
|
35
|
-
def initialize(hash)
|
36
|
-
hash = hash["unMeta"]
|
37
|
-
@content = {}
|
38
|
-
hash.keys.each {|key| @content[key] = Node.new hash[key]}
|
39
|
-
end
|
40
|
-
|
41
|
-
def [](key)
|
42
|
-
@content[key]
|
43
|
-
end
|
44
|
-
|
45
|
-
def to_h
|
46
|
-
hash = {}
|
47
|
-
@content.keys.each {|key| hash[key] = @content[key].to_h}
|
48
|
-
{"unMeta" => hash}
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
class Node
|
53
|
-
|
54
|
-
attr_reader :type, :contents
|
55
|
-
|
56
|
-
def initialize(ast_hash)
|
57
|
-
@type = ast_hash["t"]
|
58
|
-
@contents = ast_hash["c"]
|
59
|
-
end
|
60
|
-
|
61
|
-
def to_h
|
62
|
-
{
|
63
|
-
"t" => @type,
|
64
|
-
"c" => @contents
|
65
|
-
}
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
82
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Paru
|
2
|
+
module PandocFilter
|
3
|
+
module ASTManipulation
|
4
|
+
|
5
|
+
def insert index, child
|
6
|
+
@children.insert index, child
|
7
|
+
end
|
8
|
+
|
9
|
+
def delete child
|
10
|
+
@children.delete child
|
11
|
+
end
|
12
|
+
|
13
|
+
def remove_at index
|
14
|
+
@children.delete_at index
|
15
|
+
end
|
16
|
+
|
17
|
+
def append child
|
18
|
+
@children.push child
|
19
|
+
end
|
20
|
+
alias << append
|
21
|
+
|
22
|
+
def prepend child
|
23
|
+
insert 0, child
|
24
|
+
end
|
25
|
+
|
26
|
+
def replace old_child, new_child
|
27
|
+
@children.find_index old_child do |index|
|
28
|
+
replace_at index, new_child
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def replace_at index, new_child
|
33
|
+
@children[index] = new_child
|
34
|
+
end
|
35
|
+
|
36
|
+
def each_depth_first &block
|
37
|
+
yield self
|
38
|
+
each {|child| child.each_depth_first(&block)} if has_children?
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Paru
|
2
|
+
module PandocFilter
|
3
|
+
class Attr
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
attr_accessor :id, :classes
|
7
|
+
def initialize(attributes)
|
8
|
+
@id = attributes[0]
|
9
|
+
@classes = attributes[1]
|
10
|
+
@data = attributes[2]
|
11
|
+
end
|
12
|
+
|
13
|
+
def each
|
14
|
+
@data.each
|
15
|
+
end
|
16
|
+
|
17
|
+
def [](key)
|
18
|
+
if @data.key_exists? key
|
19
|
+
@data[key]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def has_key? name
|
24
|
+
@data.key_exists? name
|
25
|
+
end
|
26
|
+
|
27
|
+
def has_class? name
|
28
|
+
@classes.include? name
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_ast
|
32
|
+
[
|
33
|
+
@id,
|
34
|
+
@classes,
|
35
|
+
@data
|
36
|
+
]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Paru
|
2
|
+
module PandocFilter
|
3
|
+
require_relative "./inline"
|
4
|
+
|
5
|
+
class Citation
|
6
|
+
attr_accessor :id, :prefix, :suffix, :mode, :note_num, :hash
|
7
|
+
|
8
|
+
def initialize spec
|
9
|
+
@id = spec["citationId"] if spec.has_key? "citationId"
|
10
|
+
@prefix = Inline.new spec["citationPrefix"] if spec.has_key? "citationPrefix"
|
11
|
+
@suffix = Inline.new spec["citationSuffix"] if spec.has_key? "citationSuffix"
|
12
|
+
@mode = spec["citationMode"] if spec.has_key? "citationMode"
|
13
|
+
@note_num = spec["citationNoteNum"] if spec.has_key? "citationNoteNum"
|
14
|
+
@hash = spec["citationHash"] if spec.has_key? "citationHash"
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_ast
|
18
|
+
citation = Hash.new
|
19
|
+
citation["citationId"] = @id if not @id.nil?
|
20
|
+
citation["citationPrefix"] = @prefix.ast_contents if not @prefix.nil?
|
21
|
+
citation["citationSuffix"] = @suffix.ast_contents if not @suffix.nil?
|
22
|
+
citation["citationMode"] = @mode if not @mode.nil?
|
23
|
+
citation["citationNoteNum"] = @note_num if not @note_num.nil?
|
24
|
+
citation["citationHash"] = @hash if not @hash.nil?
|
25
|
+
citation
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Paru
|
2
|
+
module PandocFilter
|
3
|
+
require_relative "./inline"
|
4
|
+
|
5
|
+
class Cite < Inline
|
6
|
+
attr_accessor :citations
|
7
|
+
|
8
|
+
def initialize contents
|
9
|
+
@citations = []
|
10
|
+
contents[0].each do |citation|
|
11
|
+
@citations.push Citation.new(citation)
|
12
|
+
end
|
13
|
+
super contents[1]
|
14
|
+
end
|
15
|
+
|
16
|
+
def ast_contents
|
17
|
+
[
|
18
|
+
@citations.map {|citation| citation.to_ast},
|
19
|
+
super
|
20
|
+
]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Paru
|
2
|
+
module PandocFilter
|
3
|
+
require_relative "./inline"
|
4
|
+
require_relative "./attr"
|
5
|
+
|
6
|
+
class Code < Inline
|
7
|
+
attr_accessor :attr, :string
|
8
|
+
|
9
|
+
def initialize contents
|
10
|
+
@attr = Attr.new contents[0]
|
11
|
+
@string = contents[1]
|
12
|
+
end
|
13
|
+
|
14
|
+
def ast_contents
|
15
|
+
[
|
16
|
+
@attr.to_ast,
|
17
|
+
@string
|
18
|
+
]
|
19
|
+
end
|
20
|
+
|
21
|
+
def has_string?
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
def has_inline?
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Paru
|
2
|
+
module PandocFilter
|
3
|
+
require_relative "./block"
|
4
|
+
require_relative "./attr"
|
5
|
+
|
6
|
+
class CodeBlock < Block
|
7
|
+
attr_accessor :attr, :string
|
8
|
+
|
9
|
+
def initialize(contents)
|
10
|
+
@attr = Attr.new contents[0]
|
11
|
+
@string = contents[1]
|
12
|
+
end
|
13
|
+
|
14
|
+
def ast_contents
|
15
|
+
[
|
16
|
+
@attr.to_ast,
|
17
|
+
@string
|
18
|
+
]
|
19
|
+
end
|
20
|
+
|
21
|
+
def has_string?
|
22
|
+
true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Paru
|
2
|
+
module PandocFilter
|
3
|
+
require_relative "./block"
|
4
|
+
|
5
|
+
class DefinitionList < Block
|
6
|
+
def initialize contents
|
7
|
+
super []
|
8
|
+
contents.each do |item|
|
9
|
+
@children.push DefinitionListItem.new item
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def ast_contents
|
14
|
+
@children.map {|child| child.to_ast}
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Paru
|
2
|
+
module PandocFilter
|
3
|
+
require_relative "./block"
|
4
|
+
require_relative "./list"
|
5
|
+
require_relative "./inline"
|
6
|
+
|
7
|
+
class DefinitionListItem < Block
|
8
|
+
attr_accessor :term, :definition
|
9
|
+
def initialize item
|
10
|
+
@term = Block.new item[0]
|
11
|
+
@definition = List.new item[1]
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_ast
|
15
|
+
[
|
16
|
+
@term.ast_contents,
|
17
|
+
@definition.ast_contents
|
18
|
+
]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|