martile 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/martile.rb +45 -12
  2. metadata +2 -2
data/lib/martile.rb CHANGED
@@ -5,6 +5,9 @@
5
5
  require 'rexle-builder'
6
6
  require 'rexle'
7
7
 
8
+ # modified: 28-Mar-2012; Added dynarex_to_table
9
+ # modified: 28-Aug-2011; Added escaping of HTML within a code block
10
+
8
11
  class Martile
9
12
 
10
13
  attr_reader :to_html
@@ -12,40 +15,70 @@ class Martile
12
15
  def initialize(s)
13
16
  s2 = code_block_to_html(s)
14
17
  s3 = ordered_list_to_html(s2)
15
- s4 = table_to_html(s3)
16
- @to_html = s4
18
+ s4 = dynarex_to_table(s3)
19
+ s5 = table_to_html(s4)
20
+ @to_html = s5
17
21
  end
18
22
 
19
23
  private
20
24
 
21
25
  def code_block_to_html(s)
22
26
 
23
- s.split(/(?=\n\n\s{4})/).map do |x|
24
- raw_code_block = x[/.*\s{4}[^\n]+/m]
25
-
26
- if raw_code_block then
27
+ b =[]
28
+
29
+ while s =~ /^\s{4}/ do
30
+
31
+ a = s.lines.to_a
32
+ r = a.take_while{|x| x[/^(\s{4}|\n)/]}
33
+
34
+ if r.join.strip.length > 0 then
27
35
  code_block = "<pre><code>%s</code></pre>" % \
28
- raw_code_block.lines.map{|x| x[4..-1]}[1..-1].join
29
- code_block + ($').to_s
30
- else
31
- x
36
+ a.shift(r.length).map{|x| x.sub(/^\s{4}/,'')}.join\
37
+ .gsub('<','&lt;').gsub('>','&gt;')
38
+ b << code_block
39
+ s = a.join
40
+ i = r.length
41
+ else
42
+ i = (s =~ /^\s{4}/)
32
43
  end
33
- end.join("\n")
34
44
 
45
+ b << s.slice!(0,i)
46
+
47
+ end
48
+
49
+ b.join("\n") + s
50
+
51
+ end
52
+
53
+ def dynarex_to_table(s)
54
+
55
+ s.gsub(/-\[((https?:\/\/)?[\w\/\.]+)\]/) do |match|
56
+
57
+ dynarex = Dynarex.new($1)
58
+ dynarex.to_h.map(&:values)
59
+ '[' + dynarex.to_h.map{|x| x.values.join('|') + "\n"}
60
+ .join('|').chomp + ']'
61
+ end
35
62
  end
36
63
 
37
64
  def ordered_list_to_html(s)
38
65
 
39
66
  s.split(/(?=\[#)/).map do |x|
67
+
40
68
  s2, remainder = [x[/\[#.*#[^\]]+\]/m], ($').to_s] if x.strip.length > 0
69
+
41
70
  if s2 then
42
71
 
43
72
  raw_list = s2[1..-2].split(/^#/).reject(&:empty?).map(&:strip)
44
73
  list = "<ol>%s</ol>" % raw_list.map {|x| "<li>%s</li>" % x}.join
45
74
  list + remainder.to_s
75
+
46
76
  else
77
+
47
78
  x
79
+
48
80
  end
81
+
49
82
  end.join
50
83
 
51
84
  end
@@ -73,4 +106,4 @@ class Martile
73
106
  return s
74
107
  end
75
108
 
76
- end
109
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: martile
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.4
5
+ version: 0.1.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-09-08 00:00:00 +01:00
13
+ date: 2012-03-28 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency