martile 0.1.0

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.
Files changed (2) hide show
  1. data/lib/martile.rb +77 -0
  2. metadata +56 -0
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: martile.rb
4
+
5
+ require 'rexle-builder'
6
+ require 'rexle'
7
+
8
+ class Martile
9
+
10
+ attr_reader :to_html
11
+
12
+ def initialize(s)
13
+ s2 = code_block_to_html(s)
14
+ s3 = numbered_list_to_html(s2)
15
+ s4 = table_to_html(s3)
16
+ @to_html = s4
17
+ end
18
+
19
+ private
20
+
21
+ def code_block_to_html(s)
22
+
23
+ s.split(/(?=\n\n\s{4})/).map do |x|
24
+ raw_code_block = x[/\s{4}.*\s{4}[^\n]+/m]
25
+
26
+ if raw_code_block then
27
+ code_block = "<pre><code>%s</code></pre>" % \
28
+ raw_code_block.lines.map(&:lstrip)[1..-1].join
29
+ code_block + ($').to_s
30
+ else
31
+ x
32
+ end
33
+ end.join("\n")
34
+
35
+ end
36
+
37
+ def numbered_list_to_html(s)
38
+
39
+ s.split(/(?=\[#)/).map do |x|
40
+ if x.strip.length > 0 then
41
+
42
+ s, remainder = x[/\[#.*#[^\]]+\]/m], ($').to_s
43
+ raw_list = s[1..-2].split(/^#/).reject(&:empty?).map(&:strip)
44
+ list = "<ol>%s</ol>" % raw_list.map {|x| "<li>%s</li>" % x}.join
45
+
46
+ list + remainder.to_s
47
+ else
48
+ x
49
+ end
50
+ end.join
51
+
52
+ end
53
+
54
+ def table_to_html(s)
55
+ # create any tables
56
+ s.gsub!(/\[[^|]+\|[^\n]+\n\|[^\]]+\]/) do |x|
57
+
58
+ rows = x.split(/\n/).map{|x| x.split(/[\[\|\]]/).reject(&:empty?)}
59
+
60
+ xml = RexleBuilder.new
61
+ a = xml.table do
62
+ rows.each do |cols|
63
+ xml.tr do
64
+ cols.each do |col|
65
+ xml.td col
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ Rexle.new(a).xml pretty: true, declaration: false
72
+
73
+ end
74
+ return s
75
+ end
76
+
77
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: martile
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - James Robertson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-27 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/martile.rb
27
+ has_rdoc: true
28
+ homepage:
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.5.2
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Converts a martile string to html
55
+ test_files: []
56
+