paru 0.3.0.1 → 0.3.1.0.a

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
- SHA256:
3
- metadata.gz: 3dd51d6ba5f8dc6542535bb8717ab3da0264cb5b6256486b2894bddbee8099d3
4
- data.tar.gz: 931d68edec6acc217278d5505c527746b4ec722187b3e0ba50e4bfeb6532c2e1
2
+ SHA1:
3
+ metadata.gz: 38bef6b5bd0c2c11c1b636159c0d19f3f61d8696
4
+ data.tar.gz: 64bbaee8ba0e98d000888ad230347e5586699135
5
5
  SHA512:
6
- metadata.gz: 9164982754224b190dcc7782b684ec0cf3aad0abcbe250d5ad566f6c3b18d7cc1952477993f5830a166fba4ffaa0524d54c03f7bab89ef50f3217c21fca30f1c
7
- data.tar.gz: aaaf68c9f181e71c52127806b78c477c35c9191999b8f7587a3bbff1f5105385ca5654348e77fe91ced494170d2d01fc1d27793841877e3f5bd9d226a88dd9f8
6
+ metadata.gz: 20be16827b545e7287f4409b0ce7fafbcd3be2b949ba723f727af88370ddbe155646436e29193729b007a62a202e5a3622efc174939d3dec2e994608ffcddf1e
7
+ data.tar.gz: 256d22abf8bf5385e99e12c8c3ea826cc30386f7aaef80a87523162afbaf7bebda26884a584c4dd73dfeef55c130f031097020619257bce9b5f1d9860566ea7d
data/lib/paru.rb CHANGED
@@ -18,5 +18,5 @@
18
18
  #++
19
19
  module Paru
20
20
  # Paru's current version
21
- VERSION = [0, 3, 0, 1]
21
+ VERSION = [0, 3, 1, 0, "a"]
22
22
  end
@@ -22,6 +22,16 @@ module Paru
22
22
  module PandocFilter
23
23
  # BulletList, contains a list of list of Block nodes.
24
24
  class BulletList < List
25
+
26
+ # Create a new BulletList from an array of markdown strings
27
+ #
28
+ # @param items [String[]] array of markdown strings as items of
29
+ # the new BulletList
30
+ # @return [BulletList]
31
+ def self.from_array(items)
32
+ ast_items = items.map {|item| [Block.from_markdown(item).to_ast]}
33
+ BulletList.new ast_items
34
+ end
25
35
  end
26
36
  end
27
37
  end
@@ -57,6 +57,48 @@ module Paru
57
57
  def has_string?()
58
58
  true
59
59
  end
60
+
61
+ # Write this CodeBlock's contents to file
62
+ #
63
+ # @param filename {String} the path to the file to write
64
+ def to_file(filename)
65
+ File.open(filename, "w") do |file|
66
+ file.write "#{@string}\n"
67
+ end
68
+ end
69
+
70
+ # Create a new CodeBlock based on the contents of a file, and,
71
+ # optionally, a language
72
+ #
73
+ # @param filename {String} the path to the file to read the
74
+ # contents from
75
+ # @param language {String} the language of the contents
76
+ #
77
+ # @return [CodeBlock]
78
+ def self.from_file(filename, language = "")
79
+ return self.from_code_string(File.read(filename), language)
80
+ end
81
+
82
+ # Get this CodeBlock's contents as a string
83
+ #
84
+ # @return [String]
85
+ def to_code_string()
86
+ return @string
87
+ end
88
+
89
+ # Create a new CodeBlock based on a string and, optionally, a
90
+ # language
91
+ #
92
+ #
93
+ # @param code_string [String] the string with code to use as the
94
+ # contents of the CodeBlock
95
+ # @param language [String] the optional language class
96
+ # @return [CodeBlock]
97
+ def self.from_code_string(code_string, language = "")
98
+ attributes = ["", [language], []]
99
+ code_block = CodeBlock.new [attributes, code_string]
100
+ return code_block
101
+ end
60
102
  end
61
103
  end
62
104
  end
@@ -17,6 +17,8 @@
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  require_relative "./block.rb"
20
+ require_relative "./list.rb"
21
+ require_relative "./para.rb"
20
22
 
21
23
  module Paru
22
24
  module PandocFilter
@@ -37,6 +39,39 @@ module Paru
37
39
  @children.map {|child| child.to_ast}
38
40
  end
39
41
 
42
+ # Convert this DefinitionList to a hash of term => definitions
43
+ #
44
+ # @return [Array]
45
+ def to_array()
46
+ @children.map do |def_item|
47
+ def_item.to_array
48
+ end
49
+ end
50
+
51
+ # Create a new DefinitionList based on a hash of term =>
52
+ # definitions
53
+ #
54
+ # @param definitions [Array] Array of arrays with terms and their definitions
55
+ # @return [DefinitionList]
56
+ def self.from_array(definitions)
57
+ ast_items = definitions.map do |definition|
58
+ term = Block.from_markdown(definition[0]).ast_contents
59
+ defin = List.from_markdown(definition[1])
60
+
61
+ if not defin.has_block?
62
+ para = Para.new []
63
+ para.inner_markdown = definition[1]
64
+ defin = [para.to_ast]
65
+ else
66
+ defin = defin.children.map{|c| c.to_ast}
67
+ end
68
+
69
+ [term, [defin]]
70
+ end
71
+
72
+ DefinitionList.new ast_items
73
+ end
74
+
40
75
  end
41
76
  end
42
77
  end
@@ -48,6 +48,15 @@ module Paru
48
48
  @definition.ast_contents
49
49
  ]
50
50
  end
51
+
52
+ # Convert this DefinitionListItem to a pair of term and definition
53
+ #
54
+ # @return [Array]
55
+ def to_array
56
+ term = @term.children.map{|c| c.markdown.strip}.select{|c| !c.empty?}.join(" ").strip
57
+ definition = @definition.children.map{|c| c.children.map{|d| d.markdown}}.join("\n").strip
58
+ [term, definition]
59
+ end
51
60
  end
52
61
  end
53
62
  end
@@ -46,6 +46,16 @@ module Paru
46
46
  def has_block?()
47
47
  true
48
48
  end
49
+
50
+ # Convert this List to an array of markdown strings
51
+ #
52
+ # @return [String[]]
53
+ def to_array()
54
+ @children.map do |block|
55
+ block.children.map{|c| c.markdown.strip}.join("\n")
56
+ end
57
+ end
58
+
49
59
  end
50
60
  end
51
61
  end
@@ -18,6 +18,7 @@
18
18
  #++
19
19
  require_relative "./list.rb"
20
20
  require_relative "./list_attributes.rb"
21
+ require_relative "./block.rb"
21
22
 
22
23
  module Paru
23
24
  module PandocFilter
@@ -53,6 +54,21 @@ module Paru
53
54
  ]
54
55
  end
55
56
 
57
+ # Create a new OrderedList from an array of markdown strings
58
+ #
59
+ # @param items [String[]] an array of markdown strings
60
+ # @param config [Hash] configuration of the list. Can have
61
+ # properties :start (Int), :style (String), and :delim (String)
62
+ #
63
+ # @return [OrderedList]
64
+ def self.from_array(items, **config )
65
+ start = if config.has_key? :start then config[:start] else 1 end
66
+ style = if config.has_key? :style then config[:style] else "Decimal" end
67
+ delim = if config.has_key? :delim then config[:delim] else "Period" end
68
+ ast_items = items.map {|item| [Block.from_markdown(item).to_ast]}
69
+ OrderedList.new [[start, {"t" => style}, {"t" => delim}], ast_items]
70
+ end
71
+
56
72
  end
57
73
  end
58
74
  end
@@ -16,6 +16,7 @@
16
16
  # You should have received a copy of the GNU General Public License
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
+ require "csv"
19
20
  require_relative "./block.rb"
20
21
  require_relative "./inline.rb"
21
22
 
@@ -70,6 +71,112 @@ module Paru
70
71
  @children.map {|row| row.ast_contents}
71
72
  ]
72
73
  end
74
+
75
+ # Convert this table to a 2D table of markdown strings for each
76
+ # cell
77
+ #
78
+ # @param config [Hash] configuraton of the table output
79
+ # Config can contain properties :headers
80
+ #
81
+ # @return [String[][]] This Table as a 2D array of cells
82
+ # represented by their markdown strings.
83
+ def to_array(**config)
84
+ headers = if config.has_key? :headers then config[:headers] else false end
85
+
86
+ data = []
87
+ if headers then
88
+ data.push @headers.to_array
89
+ end
90
+
91
+ @children.each do |row|
92
+ data.push row.to_array
93
+ end
94
+
95
+ data
96
+ end
97
+
98
+ # Convert this Table to a CSV file. See to_array for the config
99
+ # options
100
+ #
101
+ # @param filename [String] filename to write to
102
+ # @param config [Hash] See #to_array for config options
103
+ def to_file(filename, **config)
104
+ CSV.open(filename, "wb") do |csv|
105
+ to_array(config).each {|row| csv << row}
106
+ end
107
+ end
108
+
109
+ # Create a new Table from an 2D array and an optional
110
+ # configuration
111
+ #
112
+ # @param data [String[][]] an array of markdown strings
113
+ # @param config [Hash] configuration of the list.
114
+ # properties:
115
+ # :headers [Boolean] True if data includes headers on first
116
+ # row
117
+ # :caption [String] The table's caption
118
+ # :alignment [String[]] An array with alignments for each
119
+ # column. Should have an alignment for all columns. Defaults
120
+ # to "AlignLeft"
121
+ # :widhts [Number[]] An array with column widths. Should have
122
+ # a width for all columns. Use 0 for no set width. Defaults to
123
+ # 0
124
+ #
125
+ # @return [Table]
126
+ def self.from_array(data, **config)
127
+ return Table.new [[],[],[],[],[]] if data.empty?
128
+
129
+ headers = if config.has_key? :headers then
130
+ config[:headers]
131
+ else
132
+ false
133
+ end
134
+ caption = if config.has_key? :caption then
135
+ Block.from_markdown(config[:caption]).ast_contents
136
+ else
137
+ []
138
+ end
139
+
140
+ alignment = if config.has_key? :alignment then
141
+ config[:alignment].map {|a| {"t" => "#{a}"}}
142
+ else
143
+ data.first.map {|_| {"t"=>"AlignLeft"}}
144
+ end
145
+
146
+ widths = if config.has_key? :widths then
147
+ config[:widths]
148
+ else
149
+ data.first.map {|_| 0}
150
+ end
151
+
152
+ header = []
153
+ rows = data
154
+ if headers then
155
+ header = data.first
156
+ header = header.map {|cell| [Block.from_markdown(cell).to_ast]}
157
+ rows = data.slice(1..-1)
158
+ end
159
+
160
+ rows = rows.map {|row| row.map {|cell| [Block.from_markdown(cell).to_ast]}}
161
+
162
+ Table.new [caption, alignment, widths, header, rows]
163
+ end
164
+
165
+
166
+ # Create a new Table from a CSV file.
167
+ #
168
+ # @param filename [String] filename to read CSV data from
169
+ # @param config [Hash] See #from_file for details
170
+ #
171
+ # @return [Table]
172
+ def self.from_file(filename, **config)
173
+ data = []
174
+ CSV.foreach(filename) do |row|
175
+ data << row
176
+ end
177
+
178
+ return self.from_array(data, config)
179
+ end
73
180
  end
74
181
  end
75
182
  end
@@ -38,6 +38,16 @@ module Paru
38
38
  def ast_contents
39
39
  @children.map {|child| child.ast_contents}
40
40
  end
41
+
42
+ # Convert this TableRow to an array of markdown strings, one for
43
+ # each cell
44
+ #
45
+ # @return [String[]] An Array representation of this TableRow.
46
+ def to_array()
47
+ @children.map do |cell|
48
+ cell.children.map{|c| c.markdown.strip}.join("\n")
49
+ end
50
+ end
41
51
  end
42
52
  end
43
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.1
4
+ version: 0.3.1.0.a
5
5
  platform: ruby
6
6
  authors:
7
7
  - Huub de Beer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-05 00:00:00.000000000 Z
11
+ date: 2018-07-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Use Pandoc (http://www.pandoc.org) with ruby
14
14
  email: Huub@heerdebeer.org
@@ -99,15 +99,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
- version: '0'
102
+ version: 2.3.7
103
103
  required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  requirements:
105
- - - ">="
105
+ - - ">"
106
106
  - !ruby/object:Gem::Version
107
- version: '0'
107
+ version: 1.3.1
108
108
  requirements: []
109
109
  rubyforge_project:
110
- rubygems_version: 2.7.3
110
+ rubygems_version: 2.5.2.1
111
111
  signing_key:
112
112
  specification_version: 4
113
113
  summary: Paru is a ruby wrapper around pandoc