mediacloth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/README +37 -0
  2. data/lib/mediacloth/mediawikiast.rb +50 -0
  3. data/lib/mediacloth/mediawikihtmlgenerator.rb +105 -0
  4. data/lib/mediacloth/mediawikihtmlgenerator.rb~ +105 -0
  5. data/lib/mediacloth/mediawikilexer.rb +407 -0
  6. data/lib/mediacloth/mediawikiparams.rb +33 -0
  7. data/lib/mediacloth/mediawikiparser.rb +429 -0
  8. data/lib/mediacloth/mediawikiparser.y +172 -0
  9. data/lib/mediacloth/mediawikiparser.y~ +172 -0
  10. data/lib/mediacloth/mediawikiwalker.rb +62 -0
  11. data/lib/mediacloth/mediawikiwalker.rb~ +62 -0
  12. data/lib/mediacloth.rb +23 -0
  13. data/lib/mediacloth.rb~ +23 -0
  14. data/test/data/html1 +21 -0
  15. data/test/data/html2 +2 -0
  16. data/test/data/html3 +1 -0
  17. data/test/data/html4 +1 -0
  18. data/test/data/html6 +8 -0
  19. data/test/data/html7 +1 -0
  20. data/test/data/input1 +29 -0
  21. data/test/data/input2 +2 -0
  22. data/test/data/input3 +2 -0
  23. data/test/data/input4 +1 -0
  24. data/test/data/input5 +12 -0
  25. data/test/data/input6 +8 -0
  26. data/test/data/input7 +2 -0
  27. data/test/data/lex1 +23 -0
  28. data/test/data/lex2 +2 -0
  29. data/test/data/lex3 +1 -0
  30. data/test/data/lex4 +1 -0
  31. data/test/data/lex5 +12 -0
  32. data/test/data/lex6 +8 -0
  33. data/test/data/lex7 +2 -0
  34. data/test/data/result1 +48 -0
  35. data/test/dataproducers/html.rb +23 -0
  36. data/test/dataproducers/html.rb~ +23 -0
  37. data/test/dataproducers/lex.rb +15 -0
  38. data/test/debugwalker.rb +63 -0
  39. data/test/debugwalker.rb~ +63 -0
  40. data/test/htmlgenerator.rb +25 -0
  41. data/test/htmlgenerator.rb~ +25 -0
  42. data/test/lexer.rb +57 -0
  43. data/test/lexer.rb~ +57 -0
  44. data/test/parser.rb +23 -0
  45. data/test/parser.rb~ +23 -0
  46. data/test/testhelper.rb +27 -0
  47. data/test/testhelper.rb~ +28 -0
  48. metadata +97 -0
@@ -0,0 +1,63 @@
1
+ require 'mediacloth/mediawikiwalker'
2
+
3
+ #A walker to build textual representation of the parse tree.
4
+ #
5
+ #Use as any other walker and then query for tree:
6
+ # parser = MediaWikiParser.new
7
+ # parser.lexer = MediaWikiLexer.new
8
+ # ast = parser.parse(input)
9
+ # walker = DebugWalker.new
10
+ # walker.parse(ast)
11
+ # puts walker.tree
12
+ class DebugWalker < MediaWikiWalker
13
+
14
+ #The textual representation of the parse tree.
15
+ attr_reader :tree
16
+
17
+ def initialize
18
+ super
19
+ @tree = ""
20
+ end
21
+
22
+ protected
23
+
24
+ def parse_wiki_ast(ast)
25
+ info(ast)
26
+ super(ast)
27
+ end
28
+
29
+ def parse_text(ast)
30
+ info(ast, ast.formatting)
31
+ super(ast)
32
+ end
33
+
34
+ def parse_list(ast)
35
+ info(ast, ast.type)
36
+ super(ast)
37
+ end
38
+
39
+ def parse_list_item(ast)
40
+ info(ast)
41
+ super(ast)
42
+ end
43
+
44
+ def parse_preformatted(ast)
45
+ info(ast)
46
+ end
47
+
48
+ def parse_section(ast)
49
+ info(ast)
50
+ end
51
+
52
+ private
53
+ #Pretty-print ast node information
54
+ def info(ast, *args)
55
+ @tree += "#{ast.class}"
56
+ if args.length > 0
57
+ @tree += ": "
58
+ args.each { |arg| @tree += "#{arg} " }
59
+ end
60
+ @tree += "\n"
61
+ end
62
+
63
+ end
@@ -0,0 +1,63 @@
1
+ require 'mediawikiwalker'
2
+
3
+ #A walker to build textual representation of the parse tree.
4
+ #
5
+ #Use as any other walker and then query for tree:
6
+ # parser = MediaWikiParser.new
7
+ # parser.lexer = MediaWikiLexer.new
8
+ # ast = parser.parse(input)
9
+ # walker = DebugWalker.new
10
+ # walker.parse(ast)
11
+ # puts walker.tree
12
+ class DebugWalker < MediaWikiWalker
13
+
14
+ #The textual representation of the parse tree.
15
+ attr_reader :tree
16
+
17
+ def initialize
18
+ super
19
+ @tree = ""
20
+ end
21
+
22
+ protected
23
+
24
+ def parse_wiki_ast(ast)
25
+ info(ast)
26
+ super(ast)
27
+ end
28
+
29
+ def parse_text(ast)
30
+ info(ast, ast.formatting)
31
+ super(ast)
32
+ end
33
+
34
+ def parse_list(ast)
35
+ info(ast, ast.type)
36
+ super(ast)
37
+ end
38
+
39
+ def parse_list_item(ast)
40
+ info(ast)
41
+ super(ast)
42
+ end
43
+
44
+ def parse_preformatted(ast)
45
+ info(ast)
46
+ end
47
+
48
+ def parse_section(ast)
49
+ info(ast)
50
+ end
51
+
52
+ private
53
+ #Pretty-print ast node information
54
+ def info(ast, *args)
55
+ @tree += "#{ast.class}"
56
+ if args.length > 0
57
+ @tree += ": "
58
+ args.each { |arg| @tree += "#{arg} " }
59
+ end
60
+ @tree += "\n"
61
+ end
62
+
63
+ end
@@ -0,0 +1,25 @@
1
+ require 'mediacloth/mediawikilexer'
2
+ require 'mediacloth/mediawikiparser'
3
+ require 'mediacloth/mediawikihtmlgenerator'
4
+
5
+ require 'test/unit'
6
+ require 'testhelper'
7
+
8
+ class Parser_Test < Test::Unit::TestCase
9
+
10
+ include TestHelper
11
+
12
+ def test_input
13
+ test_files("html") { |input,result|
14
+ parser = MediaWikiParser.new
15
+ parser.lexer = MediaWikiLexer.new
16
+ ast = parser.parse(input)
17
+ MediaWikiParams.instance.time = Time.mktime(2000, 1, 1, 1, 1, 1, 1)
18
+ generator = MediaWikiHTMLGenerator.new
19
+ generator.parse(ast)
20
+ assert_equal generator.html, result
21
+ # puts generator.html
22
+ }
23
+ end
24
+
25
+ end
@@ -0,0 +1,25 @@
1
+ require 'mediawikilexer'
2
+ require 'mediawikiparser'
3
+ require 'mediawikihtmlgenerator'
4
+
5
+ require 'test/unit'
6
+ require 'testhelper'
7
+
8
+ class Parser_Test < Test::Unit::TestCase
9
+
10
+ include TestHelper
11
+
12
+ def test_input
13
+ test_files("html") { |input,result|
14
+ parser = MediaWikiParser.new
15
+ parser.lexer = MediaWikiLexer.new
16
+ ast = parser.parse(input)
17
+ MediaWikiParams.instance.time = Time.mktime(2000, 1, 1, 1, 1, 1, 1)
18
+ generator = MediaWikiHTMLGenerator.new
19
+ generator.parse(ast)
20
+ assert_equal generator.html, result
21
+ # puts generator.html
22
+ }
23
+ end
24
+
25
+ end
data/test/lexer.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'mediacloth/mediawikilexer'
2
+ require 'test/unit'
3
+ require 'testhelper'
4
+
5
+ class Lexer_Test < Test::Unit::TestCase
6
+
7
+ include TestHelper
8
+
9
+ def test_input
10
+ test_files("lex") { |input,result|
11
+ lexer = MediaWikiLexer.new
12
+ tokens = lexer.tokenize(input)
13
+ assert_equal(tokens.to_s, result)
14
+ }
15
+ end
16
+
17
+ def test_empty
18
+ assert_equal(lex(""), [[false,false]])
19
+ end
20
+
21
+ def test_preformatted
22
+ #assure preformatted text works as expected at the start of the text
23
+ assert_equal(lex(" Foo\n"), [[:PRE, "Foo\n"], [false, false]])
24
+ assert_equal(lex(" Foo"), [[:PRE, "Foo"], [false, false]])
25
+ end
26
+
27
+ def test_hline
28
+ #assure that at the start of the text hline still works
29
+ assert_equal(lex("----"), [[:HLINE, "----"], [false, false]])
30
+ assert_equal(lex("\n----"), [[:HLINE, "----"], [false, false]])
31
+ end
32
+
33
+ def test_ending_text_token
34
+ #check for a problem when the last token is TEXT and it's not included
35
+ assert_equal(lex("\n----\nfoo\n"), [[:HLINE, "----"], [:TEXT, "\nfoo\n"], [false, false]])
36
+ assert_equal(lex("\n----\nfoo\n Hehe"),
37
+ [[:HLINE, "----"], [:TEXT, "\nfoo\n"], [:PRE, "Hehe"], [false, false]])
38
+ end
39
+
40
+ def test_bullets
41
+ assert_equal(lex("* Foo"),
42
+ [[:UL_START, ""], [:LI_START, ""], [:TEXT, "Foo"], [:LI_END, ""], [:UL_END, ""], [false, false]])
43
+ end
44
+
45
+ def test_nested_bullets
46
+ assert_equal(lex("**Foo"), [[:UL_START, ""], [:LI_START, ""],
47
+ [:UL_START, ""], [:LI_START, ""], [:TEXT, "Foo"], [:LI_END, ""],
48
+ [:UL_END, ""], [:LI_END, ""], [:UL_END, ""], [false, false]])
49
+ end
50
+
51
+ private
52
+ def lex(string)
53
+ lexer = MediaWikiLexer.new
54
+ lexer.tokenize(string)
55
+ end
56
+
57
+ end
data/test/lexer.rb~ ADDED
@@ -0,0 +1,57 @@
1
+ require 'mediawikilexer'
2
+ require 'test/unit'
3
+ require 'testhelper'
4
+
5
+ class Lexer_Test < Test::Unit::TestCase
6
+
7
+ include TestHelper
8
+
9
+ def test_input
10
+ test_files("lex") { |input,result|
11
+ lexer = MediaWikiLexer.new
12
+ tokens = lexer.tokenize(input)
13
+ assert_equal(tokens.to_s, result)
14
+ }
15
+ end
16
+
17
+ def test_empty
18
+ assert_equal(lex(""), [[false,false]])
19
+ end
20
+
21
+ def test_preformatted
22
+ #assure preformatted text works as expected at the start of the text
23
+ assert_equal(lex(" Foo\n"), [[:PRE, "Foo\n"], [false, false]])
24
+ assert_equal(lex(" Foo"), [[:PRE, "Foo"], [false, false]])
25
+ end
26
+
27
+ def test_hline
28
+ #assure that at the start of the text hline still works
29
+ assert_equal(lex("----"), [[:HLINE, "----"], [false, false]])
30
+ assert_equal(lex("\n----"), [[:HLINE, "----"], [false, false]])
31
+ end
32
+
33
+ def test_ending_text_token
34
+ #check for a problem when the last token is TEXT and it's not included
35
+ assert_equal(lex("\n----\nfoo\n"), [[:HLINE, "----"], [:TEXT, "\nfoo\n"], [false, false]])
36
+ assert_equal(lex("\n----\nfoo\n Hehe"),
37
+ [[:HLINE, "----"], [:TEXT, "\nfoo\n"], [:PRE, "Hehe"], [false, false]])
38
+ end
39
+
40
+ def test_bullets
41
+ assert_equal(lex("* Foo"),
42
+ [[:UL_START, ""], [:LI_START, ""], [:TEXT, "Foo"], [:LI_END, ""], [:UL_END, ""], [false, false]])
43
+ end
44
+
45
+ def test_nested_bullets
46
+ assert_equal(lex("**Foo"), [[:UL_START, ""], [:LI_START, ""],
47
+ [:UL_START, ""], [:LI_START, ""], [:TEXT, "Foo"], [:LI_END, ""],
48
+ [:UL_END, ""], [:LI_END, ""], [:UL_END, ""], [false, false]])
49
+ end
50
+
51
+ private
52
+ def lex(string)
53
+ lexer = MediaWikiLexer.new
54
+ lexer.tokenize(string)
55
+ end
56
+
57
+ end
data/test/parser.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'mediacloth/mediawikilexer'
2
+ require 'mediacloth/mediawikiparser'
3
+
4
+ require 'test/unit'
5
+ require 'testhelper'
6
+ require 'debugwalker'
7
+
8
+ class Parser_Test < Test::Unit::TestCase
9
+
10
+ include TestHelper
11
+
12
+ def test_input
13
+ test_files("result") { |input,result|
14
+ parser = MediaWikiParser.new
15
+ parser.lexer = MediaWikiLexer.new
16
+ ast = parser.parse(input)
17
+ walker = DebugWalker.new
18
+ walker.parse(ast)
19
+ puts walker.tree
20
+ }
21
+ end
22
+
23
+ end
data/test/parser.rb~ ADDED
@@ -0,0 +1,23 @@
1
+ require 'mediawikilexer'
2
+ require 'mediawikiparser'
3
+
4
+ require 'test/unit'
5
+ require 'testhelper'
6
+ require 'debugwalker'
7
+
8
+ class Parser_Test < Test::Unit::TestCase
9
+
10
+ include TestHelper
11
+
12
+ def test_input
13
+ test_files("result") { |input,result|
14
+ parser = MediaWikiParser.new
15
+ parser.lexer = MediaWikiLexer.new
16
+ ast = parser.parse(input)
17
+ walker = DebugWalker.new
18
+ walker.parse(ast)
19
+ puts walker.tree
20
+ }
21
+ end
22
+
23
+ end
@@ -0,0 +1,27 @@
1
+ module TestHelper
2
+
3
+ #Helper method for file-based comparison tests.
4
+ #
5
+ #Looks in "data/" directory for files named "inputXXX",
6
+ #substitutes "input" with baseName and loads the contents
7
+ #of "inputXXX" file into the "input" variable and the
8
+ #contents of "baseNameXXX" into the "result" variable.
9
+ #
10
+ #Then it calls the block with input and result as parameters.
11
+ def test_files(baseName, &action)
12
+ Dir.glob(File.dirname(__FILE__) + "/data/input*").each do |filename|
13
+ resultname = filename.gsub(/input(.*)/, "#{baseName}\\1")
14
+ #exclude backup files
15
+ if not resultname.include?("~")
16
+ input_file = File.new(filename, "r")
17
+ input = input_file.read
18
+ if File.exists?(resultname)
19
+ result_file = File.new(resultname, "r")
20
+ result = result_file.read
21
+
22
+ yield(input, result)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module TestHelper
2
+
3
+ #Helper method for file-based comparison tests.
4
+ #
5
+ #Looks in "data/" directory for files named "inputXXX",
6
+ #substitutes "input" with baseName and loads the contents
7
+ #of "inputXXX" file into the "input" variable and the
8
+ #contents of "baseNameXXX" into the "result" variable.
9
+ #
10
+ #Then it calls the block with input and result as parameters.
11
+ def test_files(baseName, &action)
12
+ puts File.dirname(__FILE__) + "/data/input*"
13
+ Dir.glob(File.dirname(__FILE__) + "/data/input*").each do |filename|
14
+ resultname = filename.gsub(/input(.*)/, "#{baseName}\\1")
15
+ #exclude backup files
16
+ if not resultname.include?("~")
17
+ input_file = File.new(filename, "r")
18
+ input = input_file.read
19
+ if File.exists?(resultname)
20
+ result_file = File.new(resultname, "r")
21
+ result = result_file.read
22
+
23
+ yield(input, result)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: mediacloth
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2006-07-21 00:00:00 +03:00
8
+ summary: A MediaWiki syntax parser and HTML generator.
9
+ require_paths:
10
+ - lib
11
+ email: support@pluron.com
12
+ homepage: http://mediacloth.rubyforge.org/
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: mediacloth
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Pluron Inc.
30
+ files:
31
+ - lib/mediacloth.rb~
32
+ - lib/mediacloth
33
+ - lib/mediacloth.rb
34
+ - lib/mediacloth/mediawikihtmlgenerator.rb
35
+ - lib/mediacloth/mediawikiparams.rb
36
+ - lib/mediacloth/mediawikiwalker.rb
37
+ - lib/mediacloth/mediawikiparser.rb
38
+ - lib/mediacloth/mediawikiparser.y~
39
+ - lib/mediacloth/mediawikihtmlgenerator.rb~
40
+ - lib/mediacloth/mediawikiparser.y
41
+ - lib/mediacloth/mediawikiwalker.rb~
42
+ - lib/mediacloth/mediawikilexer.rb
43
+ - lib/mediacloth/mediawikiast.rb
44
+ - test/data
45
+ - test/testhelper.rb~
46
+ - test/parser.rb
47
+ - test/parser.rb~
48
+ - test/testhelper.rb
49
+ - test/htmlgenerator.rb~
50
+ - test/debugwalker.rb~
51
+ - test/lexer.rb
52
+ - test/debugwalker.rb
53
+ - test/lexer.rb~
54
+ - test/dataproducers
55
+ - test/htmlgenerator.rb
56
+ - test/data/lex1
57
+ - test/data/lex2
58
+ - test/data/lex3
59
+ - test/data/lex4
60
+ - test/data/lex5
61
+ - test/data/lex6
62
+ - test/data/lex7
63
+ - test/data/result1
64
+ - test/data/html1
65
+ - test/data/html2
66
+ - test/data/html3
67
+ - test/data/html4
68
+ - test/data/html6
69
+ - test/data/html7
70
+ - test/data/input1
71
+ - test/data/input2
72
+ - test/data/input3
73
+ - test/data/input4
74
+ - test/data/input5
75
+ - test/data/input6
76
+ - test/data/input7
77
+ - test/dataproducers/html.rb~
78
+ - test/dataproducers/lex.rb
79
+ - test/dataproducers/html.rb
80
+ - README
81
+ test_files: []
82
+
83
+ rdoc_options:
84
+ - --title
85
+ - MediaCloth
86
+ - --main
87
+ - README
88
+ extra_rdoc_files:
89
+ - README
90
+ executables: []
91
+
92
+ extensions: []
93
+
94
+ requirements: []
95
+
96
+ dependencies: []
97
+