md2dw 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f54031624c612c43ffba948c99fd76b10d0a393
4
+ data.tar.gz: 14d39b6c2c83d2f748d64645c099b1fa3fe65331
5
+ SHA512:
6
+ metadata.gz: 566f9f14e0d4604f4ed119d2b3d05d5b733cf02836722767da03c62b3bf93b8b616e8089018e6193411b7953675eae8fba66883154b8a2358625432d4388f68a
7
+ data.tar.gz: 075286cda8a1c3b05b095ab9411d3d4004d5e2904e38c00349e4547dabd194f257df6dc4c3111facbd749b33c7f3844e38e7535e972afa1a013e3c573b2f8f49
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ *~
2
+ *.gem
3
+ *.log
4
+ *.out
5
+ *.pid
6
+ *.swp
7
+ *.lock
8
+ .DS_Store
9
+ .yardoc
10
+ doc
11
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'rake'
7
+ gem 'version'
8
+ gem 'rubygems-tasks'
9
+ end
10
+
11
+ group :test do
12
+ gem 'minitest'
13
+ end
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2014 Sean Clemmer
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # md2dw
2
+
3
+ `md2dw` is a simple command-line utility to convert Markdown files into DokuWiki
4
+ syntax.
5
+
6
+ $ git clone http://github.com/sczizzo/md2dw.git
7
+ $ cd md2dw
8
+ $ gem build md2dw.gemspec
9
+ $ gem install md2dw-VERSION.gem
10
+ $ md2dw --help
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rake'
4
+
5
+ task :default => :test
6
+
7
+ require 'rake/testtask'
8
+ Rake::TestTask.new(:test) do |test|
9
+ test.libs << 'lib' << 'test'
10
+ test.test_files = FileList['test/test*.rb']
11
+ end
12
+
13
+ require 'rubygems/tasks'
14
+ Gem::Tasks.new push: false, sign: {}
15
+ Gem::Tasks::Sign::Checksum.new sha2: true
16
+
17
+ require 'rake/version_task'
18
+ Rake::VersionTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/bin/md2dw ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ require 'trollop'
3
+ require 'md2dw'
4
+
5
+ argv = ARGV.empty? ? %w[ --help ] : ARGV
6
+
7
+ Trollop::options(argv) do
8
+ version MD2DW::VERSION
9
+ banner MD2DW::ART + "\n\n" + <<-EOS.gsub(/^ {4}/, '')
10
+ #{MD2DW::SUMMARY}
11
+
12
+ Usage: md2dw [OPTIONS] MARKDOWN_FILE
13
+
14
+ Options:
15
+ EOS
16
+ end
17
+
18
+ puts MD2DW.render(ARGF.read)
data/lib/md2dw.rb ADDED
@@ -0,0 +1,2 @@
1
+ require_relative 'md2dw/md2dw'
2
+ require_relative 'md2dw/metadata'
@@ -0,0 +1,144 @@
1
+ module MD2DW
2
+ class DokuwikiRenderer < Redcarpet::Render::Base
3
+
4
+ def block_code code, language
5
+ language = " #{language}" unless language.nil?
6
+ "\n<code%s>\n%s</code>\n" % [ language, code ]
7
+ end
8
+
9
+ def quote text
10
+ text
11
+ end
12
+
13
+ def block_quote quote
14
+ "\n<blockquote>%s</blockquote>\n" % quote
15
+ end
16
+
17
+ def block_html raw_html
18
+ raw_html
19
+ end
20
+
21
+ def footnotes content
22
+ "\n%s\n\n" % content
23
+ end
24
+
25
+ def footnote_def content, number
26
+ '[(r%s>%s)]' % [ number, content.strip ]
27
+ end
28
+
29
+ def header text, header_level
30
+ level = 7 - header_level
31
+ "\n%s %s %s\n" % [ '=' * level, text, '=' * level ]
32
+ end
33
+
34
+ def hrule
35
+ "\n-----\n"
36
+ end
37
+
38
+ def list contents, list_type
39
+ "\n" + contents
40
+ end
41
+
42
+ def list_item text, list_type
43
+ char = list_type =~ /unordered/ ? '*' : '-'
44
+ " %s %s" % [ char, text ]
45
+ end
46
+
47
+ def paragraph text
48
+ "\n%s\n" % text
49
+ end
50
+
51
+ def table header, body
52
+ "\n^ %s ^\n%s\n" % [ header.strip, body.strip ]
53
+ end
54
+
55
+ def table_row content
56
+ "| %s |\n" % content.strip
57
+ end
58
+
59
+ def table_cell content, alignment
60
+ '| %s |' % content.strip
61
+ end
62
+
63
+ def autolink link, link_type
64
+ link
65
+ end
66
+
67
+ def codespan code
68
+ "''%s''" % code
69
+ end
70
+
71
+ def image link, title, alt_text
72
+ '{{%s}}' % link
73
+ end
74
+
75
+ def linebreak
76
+ "\n"
77
+ end
78
+
79
+ def link link, title, content
80
+ '[[%s|%s]]' % [ link, content ]
81
+ end
82
+
83
+ def raw_html raw_html
84
+ '<html>%s</html>' % raw_html
85
+ end
86
+
87
+ def strikethrough text
88
+ '<del>%s</del>' % text
89
+ end
90
+
91
+ def superscript text
92
+ '<sup>%s</sup>' % text
93
+ end
94
+
95
+ def highlight text
96
+ '<mark>%s</mark>' % text
97
+ end
98
+
99
+ def footnote_ref number
100
+ '[(r%s)]' % number
101
+ end
102
+
103
+ def preprocess full_document
104
+ full_document.gsub!(/(\b|[^\\])??~~([^~].*?)~~/, '\1%_s_%\2%_s_%') # Strike
105
+ full_document.gsub!(/(\b|[^\\])??\*\*([^*].*?)\*\*/, '\1%_b_%\2%_b_%') # Bold
106
+ full_document.gsub!(/(\b|[^\\])??\*([^*].*?)\*/, '\1%_i_%\2%_i_%') # Italic
107
+ full_document.gsub!(/<sup>(.*?)<\/sup>/, '%_x_%\1%_x_%') # Superscript
108
+ full_document.gsub!(/<cite>(.*?)<\/cite>/, '%_q_%\1%_q_%') # Cite
109
+
110
+ # Fix tables
111
+ full_document = full_document.lines.map do |line|
112
+ if line =~ /\^\| (.*?) \|$/
113
+ "%s\n" % $1
114
+ else
115
+ line
116
+ end
117
+ end.join.strip
118
+
119
+ full_document
120
+ end
121
+
122
+ def postprocess full_document
123
+ full_document.gsub!(/%_s_%(.*?)%_s_%/, '<del>\1</del>') # Strike
124
+ full_document.gsub!(/%_b_%(.*?)%_b_%/, '**\1**') # Bold
125
+ full_document.gsub!(/%_i_%(.*?)%_i_%/, '//\1//') # Italic
126
+ full_document.gsub!(/%_x_%(.*?)%_x_%/, '<sup>\1</sup>') # Superscript
127
+ full_document.gsub!(/%_q_%(.*?)%_q_%/, '<q>\1</q>') # Cite
128
+
129
+ # Fix tables
130
+ full_document = full_document.lines.map do |line|
131
+ if line =~ /\^ \| \| (.+) \| \| \^$/
132
+ "^ %s ^\n" % $1.gsub(' || ', ' ^ ')
133
+ elsif line =~ /^\| \| (.+) \| \|$/
134
+ "| %s |\n" % $1.gsub(' || ', ' | ')
135
+ else
136
+ line
137
+ end
138
+ end.join.strip
139
+
140
+ full_document
141
+ end
142
+
143
+ end
144
+ end
@@ -0,0 +1,16 @@
1
+ require 'redcarpet'
2
+
3
+ require_relative 'dokuwiki_renderer'
4
+
5
+ module MD2DW
6
+ def self.render markdown
7
+ renderer = Redcarpet::Markdown.new \
8
+ DokuwikiRenderer,
9
+ no_intra_emphasis: true,
10
+ fenced_code_blocks: true,
11
+ strikethrough: true,
12
+ footnotes: true,
13
+ tables: true
14
+ renderer.render markdown
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module MD2DW
2
+ VERSION = File.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION')).strip
3
+ SUMMARY = 'Convert Markdown to DokuWiki syntax'
4
+ AUTHOR = 'Sean Clemmer'
5
+ EMAIL = 'sczizzo@gmail.com'
6
+ HOMEPAGE = 'https://github.com/sczizzo/md2dw'
7
+ ART = <<-'EOART' % VERSION
8
+
9
+ _____ __
10
+ ____ ___ ____/ /__ \ ____/ / __
11
+ / __ `__ \/ __ /__/ // __ / | /| / /
12
+ / / / / / / /_/ // __// /_/ /| |/ |/ /
13
+ /_/ /_/ /_/\__,_//____/\__,_/ |__/|__/
14
+ v%s
15
+ EOART
16
+ end
data/md2dw.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path(File.join('..', 'lib'), __FILE__)
3
+ require 'md2dw/metadata'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'md2dw'
7
+ s.version = MD2DW::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.author = MD2DW::AUTHOR
10
+ s.email = MD2DW::EMAIL
11
+ s.summary = MD2DW::SUMMARY
12
+ s.description = MD2DW::SUMMARY + '.'
13
+ s.homepage = MD2DW::HOMEPAGE
14
+ s.licenses = %w[ MIT ]
15
+
16
+ s.add_runtime_dependency 'trollop', '~> 2'
17
+ s.add_runtime_dependency 'redcarpet', '~> 3'
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- test/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File::basename(f) }
22
+ s.require_paths = %w[ lib ]
23
+ end
@@ -0,0 +1,203 @@
1
+ require 'tmpdir'
2
+ require 'tempfile'
3
+ require 'fileutils'
4
+ require 'minitest/autorun'
5
+
6
+ require_relative '../lib/md2dw'
7
+
8
+
9
+ class TestMD2DW < MiniTest::Test
10
+
11
+ def test_converts_markdown_to_dokuwiki
12
+ [ # Markdown => DokuWiki
13
+
14
+ # Text
15
+ [ '*EM*', '//EM//' ],
16
+ [ '**BOLD**', '**BOLD**' ],
17
+ [ '<sup>SUP</sup>', '<sup>SUP</sup>' ],
18
+ [ '`MONO`', "''MONO''" ],
19
+ [ '~~STRIKE~~', '<del>STRIKE</del>' ],
20
+
21
+ # Links
22
+ [ '[LINK](THIS)', '[[THIS|LINK]]' ],
23
+
24
+ # Headings
25
+ [ '# TEST', '====== TEST ======' ],
26
+ [ '## TEST', '===== TEST =====' ],
27
+ [ '### TEST', '==== TEST ====' ],
28
+ [ '#### TEST', '=== TEST ===' ],
29
+ [ '##### TEST', '== TEST ==' ],
30
+ [ '###### TEST', '= TEST =' ],
31
+
32
+ # Images
33
+ [ '![ALT](IMG.jpg)', '{{IMG.jpg}}' ],
34
+
35
+ # Lists
36
+ [ ul_list_md, ul_list_dw ],
37
+ [ ol_list_md, ol_list_dw ],
38
+
39
+ # Quotes
40
+ [ '<cite>CITE</cite>', '<q>CITE</q>' ],
41
+ [ blockquote_md, blockquote_dw ],
42
+
43
+ # Tables
44
+ [ table_md1, table_dw ],
45
+ [ table_md2, table_dw ],
46
+
47
+ # Code
48
+ [ code_block_md, code_block_dw ],
49
+ [ code_block_mde_md, code_block_mde_dw ],
50
+ [ code_block_gfm_md, code_block_gfm_dw ]
51
+
52
+ ].map { |tag| assert_renders tag[0], tag[1] }
53
+ end
54
+
55
+ private
56
+ def assert_renders md, dw
57
+ assert_equal dw, MD2DW.render(md)
58
+ end
59
+
60
+ def ul_list_md
61
+ <<-EOL.gsub(/^ {4}/, '').chomp
62
+ TEST
63
+
64
+ - ONE
65
+ - TWO
66
+ EOL
67
+ end
68
+
69
+ def ul_list_dw
70
+ <<-EOL.gsub(/^ {4}/, '').chomp
71
+ TEST
72
+
73
+ * ONE
74
+ * TWO
75
+ EOL
76
+ end
77
+
78
+ def ol_list_md
79
+ <<-EOL.gsub(/^ {4}/, '').chomp
80
+ TEST
81
+
82
+ 1. ONE
83
+ 2. TWO
84
+ EOL
85
+ end
86
+
87
+ def ol_list_dw
88
+ <<-EOL.gsub(/^ {4}/, '').chomp
89
+ TEST
90
+
91
+ - ONE
92
+ - TWO
93
+ EOL
94
+ end
95
+
96
+ def blockquote_md
97
+ <<-EOB.gsub(/^ {4}/, '').chomp
98
+ > ONE
99
+ > TWO
100
+ > THREE
101
+ EOB
102
+ end
103
+
104
+ def blockquote_dw
105
+ <<-EOB.gsub(/^ {4}/, '').chomp
106
+ <blockquote>
107
+ ONE
108
+ TWO
109
+ THREE
110
+ </blockquote>
111
+ EOB
112
+ end
113
+
114
+ def table_md1
115
+ <<-EOT.gsub(/^ {4}/, '')
116
+ First Header | Second Header
117
+ ------------- | -------------
118
+ Content Cell | Content Cell
119
+ Content Cell | Content Cell
120
+ EOT
121
+ end
122
+
123
+ def table_md2
124
+ <<-EOT.gsub(/^ {4}/, '')
125
+ | First Header | Second Header |
126
+ | ------------- | ------------- |
127
+ | Content Cell | Content Cell |
128
+ | Content Cell | Content Cell |
129
+ EOT
130
+ end
131
+
132
+ def table_dw
133
+ <<-EOT.gsub(/^ {4}/, '').chomp
134
+ ^ First Header ^ Second Header ^
135
+ | Content Cell | Content Cell |
136
+ | Content Cell | Content Cell |
137
+ EOT
138
+ end
139
+
140
+ def code_block_md
141
+ <<-EOC.gsub(/^ {4}/, '')
142
+ TEST
143
+
144
+ ONE
145
+ TWO
146
+ EOC
147
+ end
148
+
149
+ def code_block_dw
150
+ <<-EOC.gsub(/^ {4}/, '').chomp
151
+ TEST
152
+
153
+ <code>
154
+ ONE
155
+ TWO
156
+ </code>
157
+ EOC
158
+ end
159
+
160
+ def code_block_mde_md
161
+ <<-EOC.gsub(/^ {4}/, '')
162
+ TEST
163
+
164
+ ~~~
165
+ ONE
166
+ TWO
167
+ ~~~
168
+ EOC
169
+ end
170
+
171
+ def code_block_mde_dw
172
+ <<-EOC.gsub(/^ {4}/, '').chomp
173
+ TEST
174
+
175
+ <code>
176
+ ONE
177
+ TWO
178
+ </code>
179
+ EOC
180
+ end
181
+
182
+ def code_block_gfm_md
183
+ <<-EOC.gsub(/^ {4}/, '')
184
+ TEST
185
+
186
+ ```ruby
187
+ ONE
188
+ TWO
189
+ ```
190
+ EOC
191
+ end
192
+
193
+ def code_block_gfm_dw
194
+ <<-EOC.gsub(/^ {4}/, '').chomp
195
+ TEST
196
+
197
+ <code ruby>
198
+ ONE
199
+ TWO
200
+ </code>
201
+ EOC
202
+ end
203
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: md2dw
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sean Clemmer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: trollop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redcarpet
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
41
+ description: Convert Markdown to DokuWiki syntax.
42
+ email: sczizzo@gmail.com
43
+ executables:
44
+ - md2dw
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - VERSION
54
+ - bin/md2dw
55
+ - lib/md2dw.rb
56
+ - lib/md2dw/dokuwiki_renderer.rb
57
+ - lib/md2dw/md2dw.rb
58
+ - lib/md2dw/metadata.rb
59
+ - md2dw.gemspec
60
+ - test/test_md2dw.rb
61
+ homepage: https://github.com/sczizzo/md2dw
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Convert Markdown to DokuWiki syntax
85
+ test_files:
86
+ - test/test_md2dw.rb