booklab-sml 0.2.7 → 0.3.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: 951d1fb671a0362c10cc7fa76e5ed9d8b498ca314cedc3ab0b682aa2513fd0f2
4
- data.tar.gz: 6b2e52cc0076da731bb780426b8c915139890a75410bc064b98dd328a13b511c
3
+ metadata.gz: 1767629aeac09e97ae18ce1b056dc415ef31099c1affb9e4229c1ce77c66af98
4
+ data.tar.gz: 434420cf3da51edf14548330bf6d9ebc9d0a03139ced8f71aa5967e155f0c039
5
5
  SHA512:
6
- metadata.gz: 306b7e3297d818b0d94f357ef07a54cbde6cec8cd0335e29aebe84120f12fdcfc4ae081e4620d655373d8d816a61ffd14669675dda402351c2c0cb778392076c
7
- data.tar.gz: 55b623da9624ef8bf7f8ea2df272b4815d00a7125c79b7dfd87009790418fe664f11551d86742b8b799b4b61fac6d146372a752d42cbfd6dde0ca05c73aa0f22
6
+ metadata.gz: 44fe7eb237c13ed61305bcbb7023d2f5b04c2e37cb7bf02c86c198720332965500c9cfd8148e5e06568f774c1f1b652c7e9d9b4789d582b74557e8ccc4967c81
7
+ data.tar.gz: 1f060f38cb2d7fd70c4d142204297ef62c867fe8b6cf619c1d23b4b3cebb27edf50646e9b8e56667674d1dc2701c44edb473e76ff8c457a3199ddede7acd87f2
data/README.md CHANGED
@@ -16,8 +16,11 @@ and then run `bundle install`
16
16
 
17
17
  ```rb
18
18
  $ sml = %(["p", { align: "center", indent: 1 }, "Hello world"])
19
- $ BookLab::SML.parse(sml)
19
+ $ renderer = BookLab::SML.parse(sml)
20
+ $ renderer.to_html
20
21
  => <p style="text-align: center; text-indent: 32px;">Hello world</p>
22
+ $ renderer.to_text
23
+ => "Hello world"
21
24
  ```
22
25
 
23
26
  ## License
@@ -6,7 +6,7 @@ module BookLab
6
6
  module SML
7
7
  def self.parse(src, options = {})
8
8
  renderer = Renderer.new(src, options)
9
- renderer.to_html
9
+ renderer
10
10
  end
11
11
  end
12
12
  end
@@ -6,7 +6,7 @@ module BookLab::SML
6
6
  class Renderer
7
7
  include BookLab::SML::Utils
8
8
 
9
- attr_accessor :sml, :config
9
+ attr_accessor :sml, :value, :config
10
10
 
11
11
  # For table, list for temp mark in block
12
12
  attr_accessor :in_block
@@ -16,10 +16,15 @@ module BookLab::SML
16
16
  @config = Config.new
17
17
  @config.plantuml_service_host = options[:plantuml_service_host]
18
18
  @config.mathjax_service_host = options[:mathjax_service_host]
19
+ @value = YAML.load(sml)
19
20
  end
20
21
 
21
22
  def to_html
22
- node_to_html(YAML.load(sml))
23
+ node_to_html(self.value)
24
+ end
25
+
26
+ def to_s
27
+ to_html
23
28
  end
24
29
 
25
30
  def node_to_html(node, opts = {})
@@ -40,5 +45,24 @@ module BookLab::SML
40
45
  node_to_html(child, prev: prev_node, next: next_node)
41
46
  end.join("")
42
47
  end
48
+
49
+ def to_text
50
+ node_to_text(self.value)
51
+ end
52
+
53
+ def node_to_text(node, opts = {})
54
+ opts[:renderer] = self
55
+ rule = BookLab::SML::Rules::find_by_node(node)
56
+ rule.to_text(node, opts)&.strip
57
+ end
58
+
59
+ def children_to_text(node)
60
+ return node if node.is_a?(String)
61
+ children = self.class.get_children(node)
62
+ children.each_with_index.map do |child, idx|
63
+ text = node_to_text(child, {})
64
+ text.blank? ? nil : text
65
+ end.compact.join(" ")
66
+ end
43
67
  end
44
68
  end
@@ -25,6 +25,10 @@ module BookLab::SML::Rules
25
25
  %(<#{tag}>#{children}</#{tag}>)
26
26
  end
27
27
 
28
+ def self.to_text(node, opts = {})
29
+ opts[:renderer].children_to_text(node)
30
+ end
31
+
28
32
  protected
29
33
  def self.style_for_attrs(attrs, style = {})
30
34
  attrs ||= {}
@@ -23,7 +23,11 @@ module BookLab::SML::Rules
23
23
  def self.to_html(node, opts = {})
24
24
  attrs = attributes(node)
25
25
  renderer = opts[:renderer]
26
- children = renderer.children_to_html(node)
26
+ if attrs[:cd] == 1
27
+ children = renderer.children_to_text(node)
28
+ else
29
+ children = renderer.children_to_html(node)
30
+ end
27
31
 
28
32
  case attrs[:va]
29
33
  when "superscript" then attrs[:sup] = 1
@@ -2,6 +2,6 @@
2
2
 
3
3
  module BookLab
4
4
  module SML
5
- VERSION = "0.2.7"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: booklab-sml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-24 00:00:00.000000000 Z
11
+ date: 2019-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport