mdoc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ id: eo.file.name
2
+ category: [eo.personal]
3
+ title: The title for our document
4
+ date: 2009-08-01
5
+ tag: [human file]
6
+ author: unknown person
7
+
8
+ The content inside
@@ -0,0 +1,10 @@
1
+ % ---
2
+ id: eo.file.name
3
+ category: [eo.personal]
4
+ title: The title for our document
5
+ date: 2009-08-01
6
+ tag: [human file]
7
+ author: unknown person
8
+ % ----
9
+
10
+ The content inside
@@ -0,0 +1,7 @@
1
+ % Pandoc Title
2
+ % Author Like Me
3
+ % Date in Some Format
4
+
5
+ The first line of contents
6
+
7
+ The Third line of contents
@@ -0,0 +1,84 @@
1
+ require 'tempfile'
2
+ require 'launchy'
3
+ require 'rest_client'
4
+
5
+ module Mdoc
6
+ class Html < Text
7
+ def convert
8
+ htmlname = @file.gsub(/\.md$/, '.html')
9
+ # content = `curl -X POST --data-urlencode content@#{@file} http://documentup.com/compiled`
10
+ title = @title
11
+ title = 'Preview' unless (title and title.size > 0)
12
+
13
+ resp = RestClient.post 'http://documentup.com/compiled', :content => @body, :name => title
14
+ content = resp.body
15
+
16
+ # convert the received content
17
+ content.gsub!('<title>undefined</title>', "<title>#{title}</title>")
18
+ content.gsub!('<a href="#" id="logo">undefined</a>', "<a href='\#' id='logo'>#{title}</a>")
19
+ wfh = File.new(htmlname, 'w:utf-8')
20
+ wfh.write content
21
+ wfh.close
22
+ Launchy.open("file://" + htmlname)
23
+ end
24
+ end
25
+
26
+ class Pandoc < Text
27
+ def _convert type
28
+ base_name = @file.gsub(/\.md$/, '')
29
+ tmp_file = base_name + "__.md"
30
+
31
+ tfh = File.new(tmp_file, 'w:utf-8')
32
+ tfh.puts("% #{@title}") if @title
33
+ tfh.puts("% #{@author}") if @author
34
+ tfh.puts("% #{@date}") if @date
35
+ tfh.puts("")
36
+ tfh.puts(@body)
37
+ tfh.close
38
+
39
+ `pandoc -o #{base_name}.#{type} #{tmp_file}`
40
+ File.unlink tmp_file
41
+ end
42
+ end
43
+
44
+ class Rtf < Pandoc
45
+ def convert; self._convert('rtf') end
46
+ end
47
+
48
+ class Docx < Pandoc
49
+ def convert; self._convert('docx') end
50
+ end
51
+
52
+ class Pdf < Pandoc
53
+ def convert
54
+ self._convert('tex')
55
+ basename = @file.gsub(/\.md$/, '')
56
+ File.rename(basename + ".tex", basename + '__.tex')
57
+ fh = File.new(@file.gsub(/\.md$/, '.tex'), 'w:utf-8')
58
+ hd =<<ENDHEADER
59
+ \\documentclass[11pt,twocolumn]{article}
60
+ \\setlength{\\parindent}{2em}
61
+ \\usepackage{fontspec}
62
+ \\setmainfont{NSimSun}
63
+ \\usepackage{xeCJK}
64
+
65
+ \\begin{document}
66
+ ENDHEADER
67
+
68
+ fh.puts hd
69
+ rfh = File.new(basename + '__.tex', 'r:utf-8')
70
+ rfh.each do |line|
71
+ fh.puts line
72
+ end
73
+
74
+ fh.puts "\n\n\\end{document}\n\n"
75
+ rfh.close
76
+ fh.close
77
+
78
+ #`xelatex #{basename + ".tex"}`
79
+
80
+ #%w[log tex aux].each {|type| File.unlink(basename + ".#{type}")}
81
+ #File.unlink(basename + '__.tex')
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,80 @@
1
+ require 'yaml'
2
+
3
+ module Mdoc
4
+ class Text
5
+ attr_accessor :meta, :body, :title, :author, :date
6
+
7
+ def initialize file
8
+ @file = file
9
+ fh = file.is_a?(String) ? ::File.new(file, 'r:utf-8') : file
10
+
11
+ @raw_meta = Array.new
12
+ @body = String.new
13
+
14
+ first_line = true # if this is the first line
15
+ meta_type = nil # three meta format supported
16
+ on_meta = true # read lines on meta or on body
17
+
18
+ # parse the file, load all into the object
19
+ fh.each do |line|
20
+
21
+ # ignore heading blank lines
22
+ next if first_line and /^\s*$/.match(line)
23
+
24
+ # parse first line, determine the meta format
25
+ if first_line
26
+ first_line = false
27
+ if /\%\s*\-{3,}\s*$/ =~ line
28
+ meta_type = :o # original format
29
+ next # skip the first line
30
+ elsif /\%\s*.+$/ =~ line
31
+ meta_type = :p # pandoc format
32
+ elsif /^[a-z]+\:\s*/ =~ line
33
+ meta_type = :m # multi-key format
34
+ else
35
+ on_meta = false
36
+ end
37
+ end
38
+
39
+ if on_meta
40
+ # catch end of meta line
41
+ if meta_type == :o and /\%\s*\-{3,}\s*$/ =~ line
42
+ on_meta = false
43
+ next # skip the
44
+ elsif meta_type == :p
45
+ on_meta = false unless /^\%/ =~ line
46
+ elsif meta_type == :m and /^\s*$/ =~ line
47
+ on_meta = false
48
+ next
49
+ end
50
+
51
+ # puts line
52
+ @raw_meta << line.gsub(/^\s*\%\s*/, '').chomp
53
+ next
54
+ end
55
+
56
+ @body << line
57
+ end # parse lines
58
+
59
+ # set title, author and date
60
+ if meta_type == :p
61
+ @title = @raw_meta[0] || ''
62
+ @author= @raw_meta[1] || ''
63
+ @date = @raw_meta[2] || ''
64
+ @meta = {
65
+ 'title' => @title,
66
+ 'author' => @author,
67
+ 'date' => @date
68
+ }
69
+ else
70
+ @meta = YAML.load(@raw_meta.join("\n"))
71
+ if @meta
72
+ @title = @meta['title'] || ''
73
+ @author= @meta['author'] || ''
74
+ @date = @meta['date'].to_s || ''
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+
@@ -0,0 +1,4 @@
1
+ module Mdoc
2
+ VERSION = '0.0.1'
3
+ end
4
+
data/lib/mdoc.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'mdoc/parser'
2
+ require 'mdoc/convert'
data/mdoc.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+ require 'mdoc/version'
3
+
4
+ Gem::Specification.new 'mdoc', Mdoc::VERSION do |s|
5
+ s.description = "mdoc is a bundle of tools for converting markdown documents."
6
+ s.summary = "mdoc is a bundle of tools for converting markdown documents."
7
+ s.authors = ["Huang Wei"]
8
+ s.email = "huangw@pe-po.com"
9
+ s.homepage = "https://github.com/huangw/mdoc-gem"
10
+ s.files = `git ls-files`.split("\n") - %w[.gitignore]
11
+ s.executables << "mdoc"
12
+ s.test_files = Dir.glob("{spec,test}/**/*.rb")
13
+
14
+ s.add_dependency 'kramdown'
15
+ s.add_dependency 'rest-client'
16
+ s.add_dependency 'launchy'
17
+ s.add_development_dependency 'rspec', '~> 2.5'
18
+ end
19
+
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mdoc::Text do
4
+ context "pandoc format" do
5
+ subject(:mf) { Mdoc::Text.new('examples/pandoc.md') }
6
+
7
+ its(:title) { should eq('Pandoc Title') }
8
+ its(:author) { should eq('Author Like Me') }
9
+ its(:date) { should eq('Date in Some Format') }
10
+ its(:body) { should match('The first line of contents') }
11
+ its(:body) { should match('The Third line of contents') }
12
+ its(:body) { should eq("The first line of contents\r\n\r\nThe Third line of contents\r\n")}
13
+ end
14
+
15
+ context "original format" do
16
+ subject(:mf) { Mdoc::Text.new('examples/original.md') }
17
+
18
+ its(:title) { should eq('The title for our document') }
19
+ its(:author) { should eq('unknown person') }
20
+ its(:date) { should eq('2009-08-01') }
21
+ its(:body) { should eq("\r\nThe content inside\r\n")}
22
+ end
23
+
24
+ context "multi-key format" do
25
+ subject(:mf) { Mdoc::Text.new('examples/multikeys.md') }
26
+
27
+ its(:title) { should eq('The title for our document') }
28
+ its(:author) { should eq('unknown person') }
29
+ its(:date) { should eq('2009-08-01') }
30
+ its(:body) { should match("The content inside")}
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ require 'mdoc'
3
+
4
+ RSpec.configure do |config|
5
+ config.color_enabled = true
6
+ config.formatter = 'documentation'
7
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mdoc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Huang Wei
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: kramdown
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: launchy
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.5'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.5'
78
+ description: mdoc is a bundle of tools for converting markdown documents.
79
+ email: huangw@pe-po.com
80
+ executables:
81
+ - mdoc
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - README.md
86
+ - ROADMAP
87
+ - bin/mdoc
88
+ - config/members.yml
89
+ - docs/css/jsgantt.css
90
+ - docs/gantt.html
91
+ - docs/js/jsgantt.js
92
+ - examples/multikeys.md
93
+ - examples/original.md
94
+ - examples/pandoc.md
95
+ - lib/mdoc.rb
96
+ - lib/mdoc/convert.rb
97
+ - lib/mdoc/parser.rb
98
+ - lib/mdoc/version.rb
99
+ - mdoc.gemspec
100
+ - spec/parser_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: https://github.com/huangw/mdoc-gem
103
+ licenses: []
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.25
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: mdoc is a bundle of tools for converting markdown documents.
126
+ test_files:
127
+ - spec/parser_spec.rb
128
+ - spec/spec_helper.rb