grokdown 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2610494d2d55b51fff09fc8261700aaa085db6acce5ae4b973ed0c025f4b79a
4
- data.tar.gz: d3ad26529b84db710837f961c7891be88205a0eba39d87491df7e54df94195e4
3
+ metadata.gz: 6bdc2e44b41d0aacb6dfd2e67004c1863d3b9966b96a01bd9b2230f0f85f3dc6
4
+ data.tar.gz: 7f69c1ec83a3045132fa24771f42299b3250af22f054e2e4343aa57d1c1330ef
5
5
  SHA512:
6
- metadata.gz: 6caabf7d77e571f747e7b934e3e263143802c9a111ca63a3b300adefb65487928ecdbca065c83d60e64a322b6a7be2223ed26de8c363ae7854c5e7988ec6644a
7
- data.tar.gz: c527be737d96d1e171cdd44fa12ef46b48e1c2c02b4c0bc094e737be04cee85fad515b08d301b9ba8b3254a57555b360106dbbb4ed5b752fb13b7f72ec526313
6
+ metadata.gz: 7427512cd53cea029a0b8ddfea690d78e8819c8ce8fff180013ce4ce8e35d9dded96a43ac6d978b83550f93b1e59ee0d0e0329989b65af85b7e9b55303730c4e
7
+ data.tar.gz: d3f70a0ff18db0f393a19dcc6312e42566e2545bb139d2210808dfccd746d7a4efc874aed15d63f3be43f789141d10ce9d806ec52343a254c8c143787088d6f5
@@ -0,0 +1,35 @@
1
+ require 'grokdown'
2
+
3
+ module Grokdown
4
+ module Consuming
5
+ def self.extended(base)
6
+ base.send(:include,InstanceMethods)
7
+ end
8
+
9
+ def consumes?(node)
10
+ @consumables ||= {}
11
+ @consumables.has_key?(node.class)
12
+ end
13
+
14
+ def consumes(mapping={})
15
+ @consumables = mapping
16
+ end
17
+
18
+ def consume(inst,node)
19
+ @consumables ||= {}
20
+ inst.send(@consumables.fetch(node.class),node)
21
+ rescue KeyError
22
+ raise ArgumentError, "#{inst.class} cannot consume #{node.class}"
23
+ end
24
+
25
+ module InstanceMethods
26
+ def consumes?(node)
27
+ self.class.consumes?(node)
28
+ end
29
+
30
+ def consume(node)
31
+ self.class.consume(self,node)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,54 @@
1
+ require 'grokdown'
2
+
3
+ module Grokdown
4
+ module Creating
5
+
6
+ def self.extended(base)
7
+ base.send(:include,InstanceMethods)
8
+ end
9
+
10
+ mod = self
11
+ define_method(:recurse) { mod }
12
+
13
+ def create(&block)
14
+ @create = block
15
+ end
16
+
17
+ def build(node)
18
+ if @create
19
+ args = begin
20
+ @create.call(node)
21
+ rescue NoMethodError => e
22
+ raise Error, "cannot find #{e.name} from #{node.to_commonmark.inspect} at #{node.sourcepos[:start_line]}"
23
+ end
24
+
25
+ case args
26
+ when Hash
27
+ if self < Hash
28
+ new.merge!(args)
29
+ else
30
+ new(**args)
31
+ end
32
+ else
33
+ new(*args)
34
+ end
35
+ else
36
+ new
37
+ end.tap do |i| i.node=node end
38
+ end
39
+
40
+ module InstanceMethods
41
+ def sourcepos
42
+ @node&.sourcepos || {}
43
+ end
44
+
45
+ def node=(node)
46
+ @node = node
47
+ end
48
+
49
+ def node
50
+ @node
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,47 @@
1
+ require 'commonmarker'
2
+ require 'grokdown'
3
+ require 'grokdown/matching'
4
+ require 'grokdown/never_consumes'
5
+
6
+ module Grokdown
7
+ class Document
8
+ def initialize(markdown, options: %i[DEFAULT], extensions: %i[table tasklist strikethrough autolink])
9
+ @walk = []
10
+ @nodes = []
11
+
12
+ CommonMarker.render_doc(markdown,options,extensions).reduce(self) do |doc, node|
13
+ decorated_node = case node
14
+ when Matching
15
+ Matching.for(node).build(node)
16
+ else
17
+ NeverConsumes.new(node)
18
+ end
19
+
20
+ doc.push decorated_node
21
+ end
22
+ end
23
+
24
+ def push(node)
25
+ if accepts = @walk.reverse.find {|i| i.consumes?(node) }
26
+ accepts.consume(node)
27
+ else
28
+ @nodes.push(node)
29
+ end
30
+
31
+ @walk.push(node)
32
+
33
+ self
34
+ end
35
+
36
+ attr_reader :nodes
37
+
38
+ include Enumerable
39
+ def each(&block)
40
+ @nodes.each(&block)
41
+ end
42
+
43
+ def walk(&block)
44
+ @walk.each(&block)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,34 @@
1
+ require 'commonmarker/node'
2
+ require 'grokdown'
3
+
4
+ module Grokdown
5
+ module Matching
6
+ class << self
7
+ @@knowns = []
8
+
9
+ def extended(base)
10
+ @@knowns.push(base)
11
+ end
12
+
13
+ def matches?(node)
14
+ @@knowns.any? {|i| i.matches?(node)}
15
+ end
16
+
17
+ def for(node)
18
+ @@knowns.find {|i| i.matches?(node)}
19
+ end
20
+
21
+ alias_method :===, :matches?
22
+ end
23
+
24
+ def match(&block)
25
+ @matcher = block
26
+ end
27
+
28
+ def matches?(node)
29
+ node.is_a?(self) || (node.is_a?(CommonMarker::Node) && @matcher.call(node))
30
+ end
31
+
32
+ alias_method :===, :matches?
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ module Grokdown
2
+ class NeverConsumes < BasicObject
3
+ def initialize(node)
4
+ @node = node
5
+ end
6
+
7
+ def consumes?(*)
8
+ false
9
+ end
10
+
11
+ def method_missing(name,*args,**kargs,&block)
12
+ if kargs.empty?
13
+ @node.send(name,*args,&block)
14
+ else
15
+ @node.send(name,*args,**kargs,&block)
16
+ end
17
+ end
18
+
19
+ def node
20
+ @node
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Grokdown
2
- VERSION = "0.0.0"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grokdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Buxton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-25 00:00:00.000000000 Z
11
+ date: 2019-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: commonmarker
@@ -91,6 +91,11 @@ files:
91
91
  - README.md
92
92
  - grokdown.gemspec
93
93
  - lib/grokdown.rb
94
+ - lib/grokdown/consuming.rb
95
+ - lib/grokdown/creating.rb
96
+ - lib/grokdown/document.rb
97
+ - lib/grokdown/matching.rb
98
+ - lib/grokdown/never_consumes.rb
94
99
  - lib/grokdown/version.rb
95
100
  homepage: https://github.com/cpb/grokdown
96
101
  licenses: []