eggshell 1.0.0 → 1.0.1

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: 8e4e19e3f5d40c57dfd10437821105c1cb38d836
4
- data.tar.gz: d96074a2df9b741327a3a7ebe626afdcab7a27d8
3
+ metadata.gz: eb6b09972391e525c2214c576105739d3e7e52c3
4
+ data.tar.gz: 1b244ac23b2f76448562925f5f2ab672f2a2d26c
5
5
  SHA512:
6
- metadata.gz: a4664c527b16f16c3fe1ad8f908e67e05cc3b11c8b7012fc0224571a184a33d889c2767064da38aca52c48aac2b1d6e520b185af13f7e7d72d104c5df62d705a
7
- data.tar.gz: 55e1c0cfc1ec5788ce799446bfd717e9fcd18b211b6fb80ac6453d2c6627246a1abf16de726383a7dcd55434064d6db139f8c488c34bab8e993076e7abe09ea6
6
+ metadata.gz: e152a0a278690c2b05e1b77e6a62b1ec00287bafc7d1217465d26fee83c2245ff06b8d6014b402d148135d2556ab6086ab93e1a3109c3fbd49fbb03b778b356b
7
+ data.tar.gz: 714427f89fb5e5bb1ee4db6d4c1b25f324e9ac53fdae1358c16ca0240b19d52c557d548111dc6bfd71e55819612a55067c5411b49c8cc1ea90c8a6ee70a0fc8a
data/bin/eggshell CHANGED
@@ -1,8 +1,14 @@
1
1
  #!/usr/bin/ruby
2
- helptext = """
3
- Process Eggshell files from the command line:
2
+ require_relative '../lib/eggshell.rb'
4
3
 
4
+ helptext = """Eggshell (ver. #{Eggshell::VERSION})
5
+ #{'-'*80}
6
+ eggshell -h # this
7
+ eggshell -docs # outputs path of sample documents
5
8
  eggshell <source> # outputs to console
9
+ """
10
+
11
+ helptext_v2 = """
6
12
  eggshell <source> out=filename
7
13
  eggshell <source> format=format_hint
8
14
 
@@ -12,14 +18,26 @@ Note that 'format' parameter hints at the source file (e.g. supplying 'md' loads
12
18
  # @todo parse args
13
19
  # @todo set output redirect
14
20
 
15
- require_relative '../lib/eggshell.rb'
21
+ if ARGV[0] == '-h'
22
+ puts helptext
23
+ exit
24
+ elsif ARGV[0] == '-docs'
25
+ puts "Eggshell (ver. #{Eggshell::VERSION})", '-'*80, "Example docs are located at:"
26
+ puts "\t#{File.realpath(File.dirname(__FILE__))}/doc"
27
+ exit
28
+ end
29
+
30
+ $stderr.write "Eggshell (ver. #{Eggshell::VERSION})\n#{'-'*80}\n"
16
31
 
17
32
  f = ARGV[0]
18
33
  if !f || !File.exists?(f)
19
34
  puts "Invalid file: #{f}"
35
+ puts "use '-h' to show usage"
20
36
  exit
21
37
  end
22
38
 
39
+ Eggshell::Bundles::Registry.log_level(1)
40
+
23
41
  file_dir = File.realdirpath(File.dirname(f))
24
42
  $eggshell = Eggshell::Processor.new
25
43
  $eggshell.vars[:include_paths] << file_dir
data/lib/eggshell.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  # Eggshell.
2
2
  module Eggshell
3
+ VERSION_MAJOR = 1
4
+ VERSION_MINOR = 0
5
+ VERSION_PATCH = 1
6
+ VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
3
7
  # Encapsulates core parts of a line. Handler can use whatever parts are needed to
4
8
  # construct final output. Line number is provided for error reporting.
5
9
  class Line
@@ -145,13 +145,27 @@ module Eggshell::BlockHandler
145
145
 
146
146
  # Useful methods for generating HTML tags
147
147
  module HtmlUtils
148
- def create_tag(tag, attribs, open = true)
149
- nattribs = attribs.is_a?(Hash) ? attribs.clone : {}
150
- if nattribs[BlockParams::STYLE].is_a?(Hash)
151
- nattribs[BlockParams::STYLE] = css_string(nattribs[BlockParams::STYLE])
148
+ def create_tag(tag, attribs, open = true, body = nil)
149
+ str_attribs = ''
150
+ if attribs.is_a?(String)
151
+ str_attribs = attribs
152
+ else
153
+ nattribs = attribs.is_a?(Hash) ? attribs.clone : {}
154
+ if nattribs[BlockParams::STYLE].is_a?(Hash)
155
+ nattribs[BlockParams::STYLE] = css_string(nattribs[BlockParams::STYLE])
156
+ end
157
+ str_attribs = attrib_string(nattribs, BlockParams::KEYS)
152
158
  end
153
159
 
154
- "<#{tag}#{attrib_string(nattribs, BlockParams::KEYS)}#{open ? '' : '/'}>"
160
+ if !open
161
+ return "<#{tag}#{str_attribs}/>"
162
+ else
163
+ if body
164
+ return "<#{tag}#{str_attribs}>#{body}</#{tag}>"
165
+ else
166
+ return "<#{tag}#{str_attribs}>"
167
+ end
168
+ end
155
169
  end
156
170
 
157
171
  def attrib_string(map, keys = nil)
@@ -24,6 +24,11 @@ module Eggshell::Bundles
24
24
  # Maintains central registry of bundles.
25
25
  class Registry
26
26
  @@reg = {}
27
+ @@log_level = 0
28
+
29
+ def self.log_level(lvl)
30
+ @@log_level = lvl
31
+ end
27
32
 
28
33
  def self.register_bundle(bundle, id)
29
34
  if !bundle.respond_to?(:new_instance)
@@ -33,7 +38,7 @@ module Eggshell::Bundles
33
38
 
34
39
  if !@@reg[id]
35
40
  @@reg[id] = bundle
36
- $stderr.write "registering bundle #{id} => #{bundle}\n"
41
+ $stderr.write "registering bundle #{id} => #{bundle}\n" if @@log_level > 0
37
42
  else
38
43
  $stderr.write "registering bundle failed: #{id} already registered\n"
39
44
  end
@@ -134,6 +134,8 @@ module Eggshell::Bundles::Basic
134
134
 
135
135
  """
136
136
  /head|head
137
+ !caption:caption text|att=val|att=val
138
+ !col:att=val|att=val
137
139
  |col|col|col
138
140
  |>col|>col|>col
139
141
  |!@att=val att2=val2@!col|col
@@ -143,22 +145,25 @@ module Eggshell::Bundles::Basic
143
145
  include BH
144
146
  include BH::BlockParams
145
147
  include BH::HtmlUtils
148
+ include FH::Utils
146
149
 
147
150
  def initialize
148
151
  @block_types = ['table']
149
152
  end
150
153
 
151
154
  # @todo support opening row with !@ (which would then get applied to <tr>)
155
+ # @todo support thead/tbody/tfoot attributes? maybe these things can be handled same as how caption/colgroup is defined
152
156
  DELIM1 = '|'
153
157
  DELIM2 = '|>'
154
158
  HEADER = '/'
159
+ SPECIAL_DEF = /^!(\w+):(.+$)/
155
160
  CELL_ATTR_START = '!@'
156
161
  CELL_ATTR_IDX = 1
157
162
  CELL_ATTR_END = '@!'
158
163
 
159
164
  def can_handle(line)
160
165
  if !@block_type
161
- if line.match(/^(table[(.]?|\||\|>|\/)/)
166
+ if line.match(/^(table[(.]?|\||\|>|\/)/) || line.match(SPECIAL_DEF)
162
167
  @block_type = 'table'
163
168
  return BH::COLLECT
164
169
  end
@@ -167,7 +172,7 @@ module Eggshell::Bundles::Basic
167
172
  end
168
173
 
169
174
  def continue_with(line)
170
- if line.match(/^(\||\|>|\/)/)
175
+ if line.match(/^(\||\|>|\/)/) || line.match(SPECIAL_DEF)
171
176
  return BH::COLLECT
172
177
  end
173
178
  return BH::RETRY
@@ -179,34 +184,58 @@ module Eggshell::Bundles::Basic
179
184
  bp = get_block_params(type, args[0])
180
185
  row_classes = bp['row.classes']
181
186
  row_classes = ['odd', 'even'] if !row_classes.is_a?(Array)
187
+
188
+ o_out = out
189
+ out = []
182
190
 
183
191
  @eggshell.vars[T_ROW] = 0
184
192
  out << create_tag('table', bp)
193
+ out << "%caption%\n%colgroup%"
194
+ caption = {:text => '', :atts => {}}
195
+ colgroup = []
196
+
185
197
  cols = []
186
198
  rows = 0
187
199
  rc = 0
200
+ data_started = false
201
+
188
202
  lines.each do |line_obj|
189
203
  ccount = 0
190
204
  line = line_obj.line
191
- if line[0] == '/' && rows == 0
205
+ special = line.match(SPECIAL_DEF)
206
+ if special
207
+ type = special[1]
208
+ atts = special[2]
209
+ if type == 'caption'
210
+ text, atts = atts.split('|', 2)
211
+ caption[:text] = text
212
+ caption[:attributes] = parse_args(atts, true)[0]
213
+ else
214
+ atts = parse_args(atts, true)[0]
215
+ puts atts.inspect
216
+ colgroup << create_tag('col', attrib_string(atts), false)
217
+ end
218
+ elsif line[0] == '/' && rc == 0
192
219
  cols = line[1..line.length].split('|')
193
- out << "<thead><tr class='#{map['head.class']}'>"
220
+ out << "<thead><tr class='#{bp['head.class']}'>"
194
221
  cols.each do |col|
195
222
  out << "\t#{fmt_cell(col, true, ccount)}"
196
223
  ccount += 1
197
224
  end
198
225
  out << '</tr></thead>'
199
- out << '<tbody>'
200
226
  elsif line[0] == '/'
201
227
  # implies footer
228
+ out << '</tbody>' if rc > 0
202
229
  cols = line[1..line.length].split('|')
203
- out << "<tfoot><tr class='#{map['foot.class']}'>"
230
+ out << "<tfoot><tr class='#{bp['foot.class']}'>"
204
231
  cols.each do |col|
205
232
  out << "\t#{fmt_cell(col, true, ccount)}"
206
233
  ccount += 1
207
234
  end
208
235
  out << '</tr></tfoot>'
236
+ break
209
237
  elsif line[0] == DELIM1 || line[0..1] == DELIM2
238
+ out << '<tbody>' if rc == 0
210
239
  idx = 1
211
240
  sep = /(?<!\\)\|/
212
241
  if line[1] == '>'
@@ -229,11 +258,21 @@ module Eggshell::Bundles::Basic
229
258
  rows += 1
230
259
  end
231
260
 
232
- out << '</tbody>'
233
- if cols.length > 0
234
- # @todo process footer
235
- end
236
261
  out << "</table>"
262
+
263
+ if caption[:text] != ''
264
+ out[1].gsub!('%caption%', create_tag('caption', attrib_string(caption[:attributes]), true, caption[:text]))
265
+ else
266
+ out[1].gsub!("%caption%\n", '')
267
+ end
268
+
269
+ if colgroup.length > 0
270
+ out[1].gsub!('%colgroup%', "<colgroup>\n\t#{colgroup.join("\n\t")}\n</colgroup>")
271
+ else
272
+ out[1].gsub("%colgroup%\n", '')
273
+ end
274
+
275
+ o_out << out.join("\n")
237
276
  end
238
277
 
239
278
  def fmt_cell(val, header = false, colnum = 0)
@@ -11,7 +11,7 @@ module Eggshell::FormatHandler
11
11
  end
12
12
  end
13
13
 
14
- # @param String tag The opening delimeter.
14
+ # @param String|MatchData tag The opening delimeter.
15
15
  # @param String str The string between delimeters.
16
16
  def format(tag, str)
17
17
  end
@@ -24,10 +24,19 @@ module Eggshell::FormatHandler
24
24
  # piped args are key-value pairs (use `\\|` to escape) which might
25
25
  # be embedded in the tag or signal some further transformations.
26
26
  #
27
+ # If any contexts don't use direct arguments, set {{no_direct}} to `true`.
28
+ # This will produce a 1-element array with a map.
29
+ #
30
+ # @param String arg_str A pipe-separated argument list, with first argument
31
+ # interpreted as a direct arguments list.
32
+ # @param Boolean no_direct If true, doesn't treat first argument as direct
33
+ # arguments.
27
34
  # @return An array where the last element is always a {{Hash}}
28
- def parse_args(arg_str)
35
+ def parse_args(arg_str, no_direct = false)
36
+ return [] if !arg_str || arg_str == ''
29
37
  raw_args = arg_str.split(/(?<!\\)\|/)
30
- args = raw_args.shift.split(/ ; /)
38
+ args = []
39
+ args << raw_args.shift.split(/ ; /) if !no_direct
31
40
  map = {}
32
41
  raw_args.each do |rarg|
33
42
  k, v = rarg.split('=', 2)
@@ -241,6 +241,9 @@ module Eggshell
241
241
  end
242
242
 
243
243
  BH = Eggshell::BlockHandler
244
+
245
+ COMMENT = '#!'
246
+ DIRECTIVE = '#>'
244
247
 
245
248
  def preprocess(lines, line_count = 0)
246
249
  line_start = line_count
@@ -271,7 +274,7 @@ module Eggshell
271
274
  line_count += 1
272
275
 
273
276
  hdr = oline.lstrip[0..1]
274
- if hdr == '#!'
277
+ if hdr == COMMENT
275
278
  next
276
279
  end
277
280
 
@@ -527,6 +530,7 @@ module Eggshell
527
530
  #
528
531
  # @param Array tags Each entry should be a 2- or 3-element array in the
529
532
  # following form: {{[open, close[, non_nest]]}}
533
+ # @todo if opening tag is regex, don't escape (but make sure it doesn't contain {{^}} or {{$}})
530
534
  def add_format_handler(handler, tags)
531
535
  return if !tags.is_a?(Array)
532
536
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eggshell
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaiser Shahid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-19 00:00:00.000000000 Z
11
+ date: 2017-02-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: From fast and basic HTML to complex decouments and more, Eggshell aims
14
14
  to provide you with all the document generation power you need through simple markup.