booklab-sml 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/booklab/sml/renderer.rb +4 -1
- data/lib/booklab/sml/rules/base.rb +14 -0
- data/lib/booklab/sml/rules/heading.rb +12 -1
- data/lib/booklab/sml/rules/link.rb +1 -1
- data/lib/booklab/sml/rules/list.rb +47 -21
- data/lib/booklab/sml/rules/paragraph.rb +5 -1
- data/lib/booklab/sml/rules/span_with_mark.rb +2 -3
- data/lib/booklab/sml/rules/td.rb +1 -9
- data/lib/booklab/sml/utils.rb +3 -1
- data/lib/booklab/sml/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7221135ee40e21ca054073ec862e70f9f298b858a47254cabc978fd5b1b2ea1
|
4
|
+
data.tar.gz: b0aa1e7faaba0780cd2ac13a02d4247f5ed76a980ed4a5874f7084afe95336aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e924228f106d6885dce973fdb0a9abf97f1171ebeefd829529424a161627ffafefacbb56d448c5bd441d2605c9bf53aa429ea94e8a369d40b2abc8824feef92
|
7
|
+
data.tar.gz: f673b5dcffe277844ccd491b93c26919e6ffdc7076610ef138bfa2335eb7ff74deb44ca2eb4f9b3a07a8d4f5c59a5221f4d87c57fc4fa800fc3ced60e55df067
|
data/lib/booklab/sml/renderer.rb
CHANGED
@@ -8,6 +8,9 @@ module BookLab::SML
|
|
8
8
|
|
9
9
|
attr_accessor :sml, :config
|
10
10
|
|
11
|
+
# For table, list for temp mark in block
|
12
|
+
attr_accessor :in_block
|
13
|
+
|
11
14
|
def initialize(sml, options)
|
12
15
|
@sml = sml
|
13
16
|
@config = Config.new
|
@@ -34,7 +37,7 @@ module BookLab::SML
|
|
34
37
|
prev_node = idx > 0 ? children[idx - 1] : nil
|
35
38
|
next_node = idx < children.length ? children[idx + 1] : nil
|
36
39
|
|
37
|
-
node_to_html(child)
|
40
|
+
node_to_html(child, { prev: prev_node, next: next_node })
|
38
41
|
end.join("")
|
39
42
|
end
|
40
43
|
end
|
@@ -8,6 +8,8 @@ module BookLab::SML::Rules
|
|
8
8
|
class Base
|
9
9
|
include BookLab::SML::Utils
|
10
10
|
|
11
|
+
INDENT_PX = 8
|
12
|
+
|
11
13
|
class << self
|
12
14
|
include EscapeUtils
|
13
15
|
end
|
@@ -24,6 +26,18 @@ module BookLab::SML::Rules
|
|
24
26
|
end
|
25
27
|
|
26
28
|
protected
|
29
|
+
def self.style_for_attrs(attrs, style = {})
|
30
|
+
attrs ||= {}
|
31
|
+
if attrs[:align]
|
32
|
+
style["text-align"] = attrs[:align]
|
33
|
+
end
|
34
|
+
if attrs[:indent]
|
35
|
+
style["text-indent"] = "#{4 * INDENT_PX}px"
|
36
|
+
end
|
37
|
+
props = styleize(style)
|
38
|
+
return "" if props.strip.blank?
|
39
|
+
%( style="#{props}")
|
40
|
+
end
|
27
41
|
|
28
42
|
def self.styleize(style)
|
29
43
|
style.map { |k, v| "#{k}: #{v};" }.join(" ")
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "digest/md5"
|
4
|
+
|
3
5
|
module BookLab::SML::Rules
|
4
6
|
class Heading < Base
|
5
7
|
def self.match?(node)
|
@@ -10,7 +12,16 @@ module BookLab::SML::Rules
|
|
10
12
|
renderer = opts[:renderer]
|
11
13
|
title = renderer.children_to_html(node)
|
12
14
|
heading_tag = tag_name(node)
|
13
|
-
|
15
|
+
|
16
|
+
title_length = title.length
|
17
|
+
min_length = title_length * 0.3
|
18
|
+
words_length = /[a-z0-9]/i.match(title)&.length || 0
|
19
|
+
header_id = title.gsub(/[^a-z0-9]+/i, "-").downcase.gsub(/^\-|\-$/, "")
|
20
|
+
if title_length - header_id.length > min_length
|
21
|
+
header_id = Digest::MD5.hexdigest(title.strip)[0..8]
|
22
|
+
end
|
23
|
+
|
24
|
+
%(<#{heading_tag} id="#{header_id}"><a href="##{header_id}" class="heading-anchor">#</a>#{title}</#{heading_tag}>)
|
14
25
|
end
|
15
26
|
end
|
16
27
|
end
|
@@ -12,7 +12,7 @@ module BookLab::SML::Rules
|
|
12
12
|
name = renderer.children_to_html(node)
|
13
13
|
return name if attrs[:href].blank?
|
14
14
|
|
15
|
-
%(<a href="#{attrs[:href]}" title="#{attrs[:title]}">#{name}</a>)
|
15
|
+
%(<a href="#{attrs[:href]}" title="#{attrs[:title]}" target="_blank">#{name}</a>)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -23,36 +23,62 @@ module BookLab::SML::Rules
|
|
23
23
|
nid = attrs[:nid]
|
24
24
|
list_type = attrs[:type] || "bulleted"
|
25
25
|
level = attrs[:level] || 1
|
26
|
-
num = attrs[:num] || 1
|
27
|
-
pstyle = attrs[:pstyle] || "paragraph"
|
26
|
+
num = (attrs[:num] || 0) + 1
|
28
27
|
|
29
|
-
|
30
|
-
|
28
|
+
style_attrs = style_for_attrs(attrs, {
|
29
|
+
"padding-left": "#{indent_left * INDENT_PX}px"
|
30
|
+
})
|
31
31
|
|
32
|
-
|
33
|
-
when "paragraph" then item_tag = "p"
|
34
|
-
when "heading1" then item_tag = "h1"
|
35
|
-
when "heading2" then item_tag = "h2"
|
36
|
-
else item_tag = "p"
|
37
|
-
end
|
32
|
+
wrap_tag = list_type == "bulleted" ? "ul" : "ol"
|
38
33
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
34
|
+
|
35
|
+
# <ul>
|
36
|
+
# <li>Bold text</li>
|
37
|
+
# <li>Important text
|
38
|
+
# <ul>
|
39
|
+
# <li>Emphasized text</li>
|
40
|
+
# <li>
|
41
|
+
# Small text
|
42
|
+
# <ul>
|
43
|
+
# <li>Subscript text</li>
|
44
|
+
# </ul>
|
45
|
+
# </li>
|
46
|
+
# </ul>
|
47
|
+
# </li>
|
48
|
+
# </ul>
|
49
|
+
|
50
|
+
# get prev attrs
|
51
|
+
prev_name = tag_name(opts[:prev])
|
52
|
+
next_name = tag_name(opts[:next])
|
53
|
+
prev_level = attributes(opts[:prev])[:level]
|
54
|
+
next_level = attributes(opts[:next])[:level]
|
44
55
|
|
45
56
|
outs = []
|
46
|
-
|
47
|
-
|
57
|
+
|
58
|
+
|
59
|
+
if prev_name != "list" || prev_level != level
|
60
|
+
outs << %(<#{wrap_tag} data-level="#{level}">)
|
48
61
|
end
|
49
62
|
|
50
|
-
|
63
|
+
li_item = "<li>#{children}"
|
51
64
|
|
52
|
-
if
|
53
|
-
|
65
|
+
if next_name == "list"
|
66
|
+
if next_level < level
|
67
|
+
(level - next_level + 1).times do
|
68
|
+
li_item += "</li></#{wrap_tag}>"
|
69
|
+
end
|
70
|
+
elsif next_level == level
|
71
|
+
li_item += "</li>"
|
72
|
+
else
|
73
|
+
li_item += "\n"
|
74
|
+
end
|
75
|
+
else
|
76
|
+
li_item += "</li></#{wrap_tag}>"
|
54
77
|
end
|
55
|
-
|
78
|
+
|
79
|
+
outs << li_item
|
80
|
+
|
81
|
+
outs.join("\n")
|
56
82
|
end
|
57
83
|
end
|
58
84
|
end
|
@@ -9,7 +9,11 @@ module BookLab::SML::Rules
|
|
9
9
|
def self.to_html(node, opts = {})
|
10
10
|
renderer = opts[:renderer]
|
11
11
|
children = renderer.children_to_html(node)
|
12
|
-
|
12
|
+
attrs = attributes(node)
|
13
|
+
|
14
|
+
style_attrs = style_for_attrs(attrs)
|
15
|
+
|
16
|
+
%(<p#{style_attrs}>#{children}</p>)
|
13
17
|
end
|
14
18
|
end
|
15
19
|
end
|
@@ -5,11 +5,10 @@ module BookLab::SML::Rules
|
|
5
5
|
MARKS = {
|
6
6
|
cd: ["<code>", "</code>"],
|
7
7
|
b: ["<strong>", "</strong>"],
|
8
|
-
i: ["<
|
8
|
+
i: ["<i>", "</i>"],
|
9
9
|
s: ["<del>", "</del>"],
|
10
10
|
u: ["<u>", "</u>"],
|
11
|
-
|
12
|
-
m: ["==", "=="],
|
11
|
+
m: ["<mark>", "</mark>"],
|
13
12
|
sup: ["<sup>", "</sup>"],
|
14
13
|
sub: ["<sub>", "</sub>"],
|
15
14
|
}
|
data/lib/booklab/sml/rules/td.rb
CHANGED
@@ -12,15 +12,7 @@ module BookLab::SML::Rules
|
|
12
12
|
children = renderer.children_to_html(node)
|
13
13
|
|
14
14
|
style = {}
|
15
|
-
|
16
|
-
style["text-align"] = attrs[:align]
|
17
|
-
end
|
18
|
-
|
19
|
-
style_attrs = ""
|
20
|
-
if style.any?
|
21
|
-
style_attrs = %( style="#{styleize(style)}")
|
22
|
-
end
|
23
|
-
|
15
|
+
style_attrs = style_for_attrs(attrs, style)
|
24
16
|
%(<td#{style_attrs}>#{children}</td>)
|
25
17
|
end
|
26
18
|
end
|
data/lib/booklab/sml/utils.rb
CHANGED
@@ -10,12 +10,14 @@ module BookLab::SML
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def tag_name(node)
|
13
|
+
return nil if node.blank?
|
13
14
|
node[0] || ""
|
14
15
|
end
|
15
16
|
|
16
17
|
def attributes(node, add_if_mission: false)
|
18
|
+
return {} if node.blank?
|
17
19
|
return node[1].deep_symbolize_keys if has_attributes?(node)
|
18
|
-
return
|
20
|
+
return {} unless add_if_mission
|
19
21
|
|
20
22
|
name = node.shift || ""
|
21
23
|
attrs = {}
|
data/lib/booklab/sml/version.rb
CHANGED
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.
|
4
|
+
version: 0.2.1
|
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-
|
11
|
+
date: 2019-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|