ulysses 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94a96891b11b517de41e2a090402f02e12404b96
4
- data.tar.gz: 894df84f95414f2b8e328439e05fc7c57447bd73
3
+ metadata.gz: 9b1b20c6d6d61c8638523318dc7da82510af3750
4
+ data.tar.gz: 22b91e891cb856c8951bfa1f9b4ec6083186a4e7
5
5
  SHA512:
6
- metadata.gz: caa083c9df77861877daeb0b5fa3967de65dc3c2799fe57aaa40a2e1fdd6cfbf7f21fc70d051996536e079bb044fa539e597d15a0bbc661a088efaebccc631f5
7
- data.tar.gz: ff82e2446f4dbe38300af87dd0a5509e4522c72d2d67899a6d72966576dc8ffc420b6e36ffbc1f82f5cc479fa297d79bffe47946ee5913e4754dd8038bb2ab9a
6
+ metadata.gz: ca03cee526afcf4da3524e79b82864fdf1f32642a46ddd7c2a7a5dd9064887b86c4019db1d3726402d1e4ed75e27360c8f4387a8b0831b4e0cf60bc1f74f76bd
7
+ data.tar.gz: 6428353c07a468d956121901d20e6f4f9aed5e2a13550f83fef024e75818d925b34634a2b056808f327c6680c1b2fa8932965cd54778ea538061484f7f7cb90e
@@ -1,8 +1,11 @@
1
1
  module Ulysses
2
2
  class Printer
3
3
 
4
- INLINE_MARKUPS = %w(strong emph mark delete inlineComment code inlineNative)
5
4
  SHEET_CONTENT_XPATH = '/sheet/string[@xml:space="preserve"]'
5
+ SIMPLE_ELEMENTS = {strong: 'strong', emph: 'em', mark: 'mark',
6
+ delete: 'del', code: 'code', inlineNative: 'span' }
7
+ HIDDEN_ELEMENTS = [:inlineComment]
8
+ HIDDEN_TAGS = [:comment]
6
9
 
7
10
  def initialize(target)
8
11
  @target = target
@@ -12,6 +15,12 @@ module Ulysses
12
15
  end
13
16
 
14
17
  def print
18
+ [print_body, print_footnotes].compact.join("\n")
19
+ end
20
+
21
+ private
22
+
23
+ def print_body
15
24
  if @target.is_a? Library
16
25
  print_library(@target)
17
26
  elsif @target.is_a? Group
@@ -23,16 +32,6 @@ module Ulysses
23
32
  end
24
33
  end
25
34
 
26
- def footnotes
27
- @footnotes
28
- end
29
-
30
- def annotations
31
- @annotations
32
- end
33
-
34
- private
35
-
36
35
  def print_library(library)
37
36
  library.groups.map { |g| print_group(g) }.join("\n")
38
37
  end
@@ -43,31 +42,47 @@ module Ulysses
43
42
 
44
43
  def print_sheet(sheet)
45
44
  paragraphs = sheet.xml.xpath(SHEET_CONTENT_XPATH).children.select { |n| n.element? }
46
- paragraphs.map{ |p| print_paragraph(p) }.join("\n")
45
+ paragraphs.map{ |p| print_paragraph(p) }.compact.join("\n")
46
+ end
47
+
48
+ def print_footnotes
49
+ return nil if @footnotes.empty?
50
+ html = '<ol class="footnotes">'
51
+ @footnotes.each_with_index do |fn, index|
52
+ html += "<li><a href=\"#fn-#{index+1}\">&#94;</a> #{fn}</li>"
53
+ end
54
+ html + '</ol>'
55
+ end
56
+
57
+ def print_annotations
58
+ return nil if @annotations.empty?
59
+ html = '<ol class="annotations">'
60
+ @annotations.each_with_index do |an, index|
61
+ html += "<li id=\"annotation-#{index + 1}\">#{an}</li>"
62
+ end
63
+ html + '</ol>'
47
64
  end
48
65
 
49
66
  def print_paragraph(p)
50
67
  children = p.children
51
68
  tags = (children.any? && children.first.name === 'tags') ? parse_tags(children.shift) : []
52
- content = parse_content(children)
69
+ return nil if (tags & HIDDEN_TAGS).any?
53
70
 
54
- tabs = tags.count('tab')
55
- if tabs > 0
56
- tags.delete('tab')
57
- tags << "tabs_#{tabs}"
58
- end
59
- if tags.any?
60
- tags = tags.uniq.map{|t| snake_case(t)}.join(' ')
61
- "<p class=\"#{tags}\">#{content}</p>"
71
+ content = parse_content(children)
72
+
73
+ heading_tag = tags.find {|t| t.to_s.start_with? 'heading' }
74
+ if heading_tag
75
+ heading_level = /heading(\d)/.match(heading_tag)[1]
76
+ "<h#{heading_level}>#{content}</h#{heading_level}>"
62
77
  else
63
- "<p>#{content}</p>"
78
+ "<p class=\"#{parse_line_class(tags)}\">#{content}</p>"
64
79
  end
65
80
  end
66
81
 
67
82
  def parse_content(nodes)
68
83
  nodes.map do |node|
69
84
  if node.text?
70
- node.content
85
+ @html_entities.encode node.content
71
86
  else
72
87
  send("parse_#{node.name}", node)
73
88
  end
@@ -77,9 +92,9 @@ module Ulysses
77
92
  def parse_tags(tags)
78
93
  tags.children.map do |tag|
79
94
  if tag.attributes.has_key? 'kind'
80
- tag.attributes['kind'].value
95
+ tag.attributes['kind'].value.to_sym
81
96
  elsif tag.content === "\t"
82
- 'tab'
97
+ :tab
83
98
  end
84
99
  end
85
100
  end
@@ -97,16 +112,20 @@ module Ulysses
97
112
  end
98
113
 
99
114
  def parse_element(node)
100
- kind = node.attributes['kind'].value
101
- if INLINE_MARKUPS.include?(kind)
102
- parse_inline_style(kind, node)
103
- else
104
- send "parse_element_#{node.attributes['kind'].value}", node
105
- end
115
+ kind = node.attributes['kind'].value.to_sym
116
+ return '' if HIDDEN_ELEMENTS.include? kind
117
+ return parse_simple_element(kind, node) if SIMPLE_ELEMENTS.has_key?(kind)
118
+
119
+ send("parse_element_#{kind}", node)
106
120
  end
107
121
 
108
- def parse_inline_style(markup, node)
109
- '<span class="' + snake_case(markup) + '">' + parse_content(node.children) + '</span>'
122
+ def parse_simple_element(kind, node)
123
+ content = parse_content(node.children)
124
+ if (html_tag = SIMPLE_ELEMENTS[kind]) === 'span'
125
+ '<span class="' + snake_case(kind) + '">' + content + '</span>'
126
+ else
127
+ "<#{html_tag}>#{content}</#{html_tag}>"
128
+ end
110
129
  end
111
130
 
112
131
  def parse_element_link(node)
@@ -128,7 +147,7 @@ module Ulysses
128
147
  def parse_element_footnote(node)
129
148
  attrs = parse_element_attributes(node)
130
149
  @footnotes << attrs['text']
131
- '<sup class="footnote-ref"><a href="#fn' + @footnotes.size.to_s + '">' + @footnotes.size.to_s + '</a></sup>'
150
+ '<sup class="footnote-ref"><a href="#fn-' + @footnotes.size.to_s + '">' + @footnotes.size.to_s + '</a></sup>'
132
151
  end
133
152
 
134
153
  def parse_element_annotation(node)
@@ -146,7 +165,7 @@ module Ulysses
146
165
  end
147
166
 
148
167
  def snake_case(tag)
149
- tag.gsub(/::/, '/')
168
+ tag.to_s.gsub(/::/, '/')
150
169
  .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
151
170
  .gsub(/([a-z\d])([A-Z])/,'\1_\2')
152
171
  .gsub(/([a-z])(\d)/i, '\1_\2')
@@ -154,5 +173,15 @@ module Ulysses
154
173
  .downcase
155
174
  end
156
175
 
176
+ def parse_line_class(tags)
177
+ tabs = tags.count(:tab)
178
+ if tabs > 1
179
+ tags.delete(:tab)
180
+ tags << :"tabs_#{tabs}"
181
+ end
182
+ tags.unshift :line
183
+ tags.uniq.map{ |t| snake_case(t) }.join(' ')
184
+ end
185
+
157
186
  end
158
187
  end
@@ -1,3 +1,3 @@
1
1
  module Ulysses
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
3
3
  end
@@ -20,7 +20,6 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_dependency 'nokogiri'
23
- spec.add_dependency 'kramdown'
24
23
  spec.add_dependency 'htmlentities'
25
24
 
26
25
  spec.add_development_dependency "bundler", "~> 1.11"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ulysses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaodong Zhao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-13 00:00:00.000000000 Z
11
+ date: 2016-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: kramdown
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: htmlentities
43
29
  requirement: !ruby/object:Gem::Requirement