arti_mark 0.1.beta3 → 0.1.beta5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,147 +0,0 @@
1
- module ArtiMark
2
- class Syntax
3
- include CommandLexer
4
-
5
- attr_accessor :inline_handler, :linecommand_handler
6
-
7
- def initialize
8
- @inline_handler = Class.new do extend CommandLexer end
9
- @linecommand_handler = Class.new do extend CommandLexer end
10
- @block_parsers = []
11
- @block_parsers <<
12
- [
13
- Proc.new { |lines| lex_line_command(lines[0])[:cmd] == 'newpage' },
14
- Proc.new {
15
- |lines, context, syntax|
16
- lexed = lex_line_command(lines.shift)
17
- if lexed[:params].size > 0
18
- title = escape_html lexed[:params].first
19
- else
20
- title = nil
21
- end
22
- context.start_html(title)
23
- }
24
- ]
25
-
26
- [DivParser.instance, ArticleParser.instance, ParagraphParser.instance, HeadParser.instance, BlockImageParser.instance, OrderedListParser.instance, UnorderedListParser.instance, DefinitionListParser.instance].each {
27
- |parser|
28
- @block_parsers << [
29
- parser.method(:accept?),
30
- parser.method(:parse)
31
- ]
32
- }
33
- def @inline_handler.l(lexed, context)
34
- ref = lexed[:params][0].strip
35
- "<a#{ids_string(lexed[:ids])}#{class_string(lexed[:cls])} href='#{ref}'>#{lexed[:text].strip}</a>"
36
- end
37
-
38
- def @inline_handler.link(lexed, context)
39
- l(lexed, context)
40
- end
41
-
42
- def @inline_handler.s(lexed, context)
43
- cls, text = lexed[:cls], lexed[:text]
44
- "<span#{ids_string(lexed[:ids])}#{class_string(cls)}>#{text.strip}</span>"
45
- end
46
-
47
- def @inline_handler.img(lexed, context)
48
- cls, param, text = lexed[:cls], lexed[:params], lexed[:text]
49
- "<img#{ids_string(lexed[:ids])}#{class_string(cls)} src='#{text.strip}' alt='#{param.join(' ')}' />"
50
- end
51
-
52
- def @inline_handler.ruby(lexed, context)
53
- cls, param, text = lexed[:cls], lexed[:params], lexed[:text]
54
- "<ruby#{ids_string(lexed[:ids])}#{class_string(cls)}>#{text.strip}<rp>(</rp><rt>#{param.join}</rt><rp>)</rp></ruby>"
55
- end
56
-
57
- def @inline_handler.tcy(lexed, context)
58
- cls, text = lexed[:cls], lexed[:text]
59
- cls << 'tcy'
60
- "<span#{ids_string(lexed[:ids])}#{class_string(cls)}>#{text.strip}</span>"
61
- end
62
-
63
- # universal inline command handler
64
- def @inline_handler.method_missing(cmd, *args)
65
- ids, cls, text = args[0][:ids], args[0][:cls], args[0][:text]
66
- "<#{cmd}#{ids_string(ids)}#{class_string(cls)}>#{text.strip}</#{cmd}>"
67
- end
68
-
69
- def @linecommand_handler.p(lexed, context)
70
- cls, text = lexed[:cls], lexed[:text]
71
- "<p#{ids_string(lexed[:ids])}#{class_string(cls)}>#{text.strip}</p>\n"
72
- end
73
-
74
- def @linecommand_handler.stylesheets(lexed, context)
75
- context.stylesheets = lexed[:text].split(',').map {|s|
76
- s.strip!
77
- if s =~ /^(.+?\.css):\((.+?)\)$/
78
- [$1, $2]
79
- else
80
- s
81
- end
82
- }
83
- ''
84
- end
85
-
86
- def @linecommand_handler.title(lexed, context)
87
- context.title = lexed[:text].strip
88
- ''
89
- end
90
-
91
- def @linecommand_handler.lang(lexed, context)
92
- context.lang = lexed[:text].strip
93
- ''
94
- end
95
-
96
- #univarsal line command handler
97
- def @linecommand_handler.method_missing(cmd, *args)
98
- "<#{cmd}#{ids_string(args[0][:ids])}#{class_string(args[0][:cls])}>#{args[0][:text].strip}</#{cmd}>\n"
99
- end
100
- end
101
-
102
- def determine_parser(lines, opt = {})
103
- @block_parsers.each {
104
- |accept, parser|
105
- return parser if accept.call(lines)
106
- }
107
-
108
- if UniversalBlockParser.instance.accept?(lines)
109
- UniversalBlockParser.instance.method(:parse)
110
- elsif opt[:get_default]
111
- default_parser
112
- else
113
- nil
114
- end
115
- end
116
-
117
- def append_parser(parser)
118
- if parser.respond_to?(:accept?) && parser.respond_to?(:parse)
119
- @block_parsers << [
120
- parser.method(:accept?),
121
- parser.method(:parse)
122
- ]
123
- elsif parser[:accept?] && parser[:parse]
124
- @block_parsers << [
125
- parser[:accept?],
126
- parser[:parse]
127
- ]
128
- else
129
- raise 'parser should define accept? and parse' if !(parser.respond_to?(:accept?) && parser.respond_to?(:parse))
130
- end
131
- end
132
-
133
- def default_parser
134
- ParagraphParser.instance.method(:parse)
135
- end
136
-
137
- def parse(lines, context)
138
- throw "something wrong: #{lines}" if lines[0] == '}' # TODO: should do something here with paragraph_parser
139
- if parser = determine_parser(lines)
140
- parser.call(lines, context, self)
141
- else
142
- default_parser.call(lines, context, self)
143
- end
144
- end
145
-
146
- end
147
- end
@@ -1,10 +0,0 @@
1
- require 'singleton'
2
-
3
- module ArtiMark
4
- class UniversalBlockParser
5
- include CommonBlockParser, Singleton
6
- def initialize
7
- @command = /\w+/
8
- end
9
- end
10
- end
@@ -1,14 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require 'singleton'
3
-
4
- module ArtiMark
5
- class UnorderedListParser
6
- include ListParser, Singleton
7
-
8
- def initialize
9
- @cmd = /\*/
10
- @blockname = 'ul'
11
- end
12
-
13
- end
14
- end