calico 0.1.0

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/calico.rb +90 -0
  2. metadata +54 -0
data/lib/calico.rb ADDED
@@ -0,0 +1,90 @@
1
+ class Calico < String
2
+
3
+ def to_html
4
+ render(parse)
5
+ end
6
+
7
+ def parse
8
+ strip!
9
+ split(/\n*^(---.*?^---)\n*|(?:\n\n+)/m).map do |block|
10
+ case block
11
+ when /\A\*\s/
12
+ [:list, list(block)]
13
+ when /\A---/
14
+ [:verb, verb(block)]
15
+ when /\A>\s/
16
+ [:quote, quote(block)]
17
+ else
18
+ [:para, inline(block)]
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def render(tokens)
26
+ tokens.inject([]) { |a, e|
27
+ type, contents = *e
28
+ a << case type
29
+ when :para
30
+ tag :p, contents
31
+ when :verb
32
+ tag :pre, contents
33
+ when :list
34
+ tag :ul, contents
35
+ when :quote
36
+ tag :blockquote, contents
37
+ end
38
+ }.join($/)
39
+ end
40
+
41
+ def list(block)
42
+ block.split(/^\* /m)[1..-1].map {|item|
43
+ tag 'li', inline(item)
44
+ }.join($/)
45
+ end
46
+
47
+ def quote(block)
48
+ Calico.new(block.gsub(/^>\n/, "\n").gsub(/^> /, '')).to_html
49
+ end
50
+
51
+ def verb(block)
52
+ block.gsub(/^---(.*)^---/m) { xml_escape($1.strip, :strict) }
53
+ end
54
+
55
+ def inline(text)
56
+ xml_escape(text).gsub(/\\\\$/, '<br />').
57
+ gsub(/\\([^\\\n]|\\(?!\n))/) { "&Calico#{$&.unpack("U*")[1]};" }.
58
+ gsub(/&(?!#\d+;|#x[\da-fA-F]+;|\w+;)/, "&amp;").
59
+ gsub(/(`+)(.+?)\1/m, tag(:code, '\2')).
60
+ gsub(/_(.+?)_/m, tag(:em, '\1')).
61
+ gsub(/\*(.+?)\*/m, tag(:strong, '\1')).
62
+ gsub(/\+(.+?)\+/m, tag(:ins, '\1')).
63
+ gsub(/~(.+?)~/m, tag(:del, '\1')).
64
+ gsub(/!\[([^\]]+)\]\(([^\)]+)\)/m, tag(:img, nil,
65
+ :src => '\2', :alt => '\1')).
66
+ gsub(/\[([^\]]+)\]\(([^\)]+)\)/m, tag(:a,
67
+ '\1', :href => '\2')).
68
+ gsub(/&Calico(\d+);/) { [$1.to_i].pack("U*") }.
69
+ strip
70
+ end
71
+
72
+ def xml_escape(text, strict=false)
73
+ text.gsub('&') { strict ? '&amp;' : '&' }.
74
+ gsub('<', '&lt;').
75
+ gsub('>', '&gt;').
76
+ gsub('"', '&quot;')
77
+ end
78
+
79
+ def tag(t, text, attrs={})
80
+ attrs = attrs.map {|k, v|
81
+ ' %s="%s"' % [k, xml_escape(v, :strict)]
82
+ }.join
83
+ if text
84
+ "<#{t}#{attrs}>#{text}</#{t}>"
85
+ else
86
+ "<#{t}#{attrs} />"
87
+ end
88
+ end
89
+
90
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calico
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - "Fran\xC3\xA7ois Vaux"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-21 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: A lightweight Markdown subset
17
+ email: madx@yapok.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/calico.rb
26
+ homepage: http://github.com/madx/calico
27
+ licenses: []
28
+
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.5
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Calico
53
+ test_files: []
54
+