mdpage 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/mdpage ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # Usage: mdpage [<file>...]
3
+ # Convert one or more Markdown files to styled html files.
4
+
5
+ if (ARGV.include? '--help') || (ARGV.include? '-h')
6
+ File.read(__FILE__).split("\n").grep(/^# /).each do |line|
7
+ puts line[2..-1]
8
+ end
9
+ exit
10
+ end
11
+
12
+ require 'mdpage'
13
+ MdPage.new(ARGV).to_html
14
+
@@ -0,0 +1,53 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset='utf-8'>
5
+ <title><%= @doc[:title] %></title>
6
+ <style type="text/css">
7
+ body {
8
+ font: 14px Helvetica, arial, freesans, clean, Meiryo, sans-serif;
9
+ margin: 0;
10
+ padding: 0;
11
+ }
12
+ div#markdown-body {
13
+ width: 880px;
14
+ margin: 0 auto;
15
+ }
16
+ #markdown-body h1, #markdown-body h2 {
17
+ border-bottom: 1px solid #ccc;
18
+ }
19
+ #markdown-body h3, #markdown-body h4 {
20
+ border-left: 2px solid #ccc;
21
+ padding-left: 3px;
22
+ }
23
+ #markdown-body table {
24
+ border-collapse: collapse;
25
+ border: 1px solid #eee;
26
+ }
27
+ #markdown-body table thead {
28
+ background: #eee;
29
+ }
30
+ #markdown-body th, #markdown-body td {
31
+ padding: 2px 3px;
32
+ border: 1px solid #ccc;
33
+ }
34
+ #markdown-body blockquote {
35
+ background: #eee;
36
+ border-left: 4px double #ccc;
37
+ margin: 5px 40px;
38
+ padding: 8px;
39
+ }
40
+ #markdown-body pre {
41
+ background: #efefef;
42
+ border: 1px solid #e7e7e7;
43
+ border-radius: 3px;
44
+ padding: 14px;
45
+ }
46
+ </style>
47
+ </head>
48
+ <body>
49
+ <div id="markdown-body">
50
+ <%= @doc[:contents] %>
51
+ </div>
52
+ </body>
53
+ </html>
data/lib/mdpage.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "rdiscount"
2
+ require "erb"
3
+
4
+ class MdPage
5
+ attr_reader :argv, :path_check, :doc
6
+
7
+ def initialize(argv)
8
+ @argv = argv
9
+ check_results = @argv.map {|av| File.exist? av}
10
+ @path_check = check_results.include?(false) ? false : true
11
+ tpl_path = File.dirname(__FILE__) + "/default.html.erb"
12
+ @template = File.open(tpl_path, "r:utf-8") {|f| f.read}
13
+ @doc = Hash.new
14
+ end
15
+
16
+ def md2html(file)
17
+ begin
18
+ raw_contents = File.open(file, "r:utf-8") {|f| f.read}
19
+ raw_contents =~ /^.*#\s?(.+)\n.+/
20
+ @doc[:title] = $1 || ''
21
+ rescue
22
+ raise "#{file} encoding should be UTF-8."
23
+ end
24
+ contents = RDiscount.new(raw_contents).to_html
25
+ return contents
26
+ end
27
+
28
+ def to_html
29
+ erb = ERB.new(@template)
30
+ @argv.each do |file|
31
+ @doc[:contents] = md2html(file)
32
+ html = erb.result(binding)
33
+ out_path = File.expand_path(file) + ".html"
34
+ File.open(out_path, "w:utf-8") {|f| f.write html}
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,58 @@
1
+ # coding: utf-8
2
+
3
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
4
+ require "mdpage"
5
+ require "rspec"
6
+
7
+ describe MdPage do
8
+ before(:each) do
9
+ files = [
10
+ "#{File.dirname(__FILE__)}/sample.md"
11
+ ]
12
+ files2 = [
13
+ "#{File.dirname(__FILE__)}/sample.md",
14
+ "#{File.dirname(__FILE__)}/sample2.md"
15
+ ]
16
+ @mdp = MdPage.new(files)
17
+ @mdp_invalid_enc = MdPage.new(files2)
18
+ end
19
+
20
+ context 'Initializing MdPage' do
21
+ it "init with file path as Array" do
22
+ argv = ["#{File.dirname(__FILE__)}/sample.md"]
23
+ mdp = MdPage.new(argv)
24
+ mdp.path_check.should == true
25
+ end
26
+
27
+ it "init with invalid file path" do
28
+ argv = ["#{File.dirname(__FILE__)}/faff/sample.md"]
29
+ mdp = MdPage.new(argv)
30
+ mdp.path_check.should == false
31
+ end
32
+
33
+ it "init with one or more valid file path" do
34
+ argv = [
35
+ "#{File.dirname(__FILE__)}/sample.md",
36
+ "#{File.dirname(__FILE__)}/sample2.md"
37
+ ]
38
+ mdp = MdPage.new(argv)
39
+ mdp.path_check.should == true
40
+ end
41
+
42
+ it "init with one or more invalid file path" do
43
+ argv = [
44
+ "#{File.dirname(__FILE__)}/sample.md",
45
+ "#{File.dirname(__FILE__)}/rere/sample2.md"
46
+ ]
47
+ mdp = MdPage.new(argv)
48
+ mdp.path_check.should == false
49
+ end
50
+ end
51
+
52
+ context 'Generate html' do
53
+ it "MdPage should be return true if generated htmlfile" do
54
+ @mdp.to_html.should_not == nil
55
+ end
56
+ end
57
+ end
58
+
data/spec/sample.md ADDED
@@ -0,0 +1,33 @@
1
+ # mdpage
2
+ mdpage is a styled html generator for markdown files.
3
+
4
+ ## \*.md files should be encoded "utf-8"
5
+ mdpage processes to assume utf-8 character encoding of the file.
6
+
7
+ ## customize
8
+ if exist ~/.mdpage/default.html.erb , mdpage use your template.
9
+
10
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
11
+
12
+ ## Default css style
13
+
14
+ # h1
15
+ ## h2
16
+ ### h3
17
+ #### h4
18
+ ##### h5
19
+ ###### h6
20
+
21
+ p
22
+
23
+ table style|header|
24
+ -----------|------|
25
+ td |td
26
+
27
+ >block quote
28
+ >block quote
29
+
30
+ def code_tag
31
+ puts "Hello mdpage"
32
+ end
33
+
data/spec/sample2.md ADDED
@@ -0,0 +1,6 @@
1
+ # this is sample.md
2
+ hgoehgoehgoeg
3
+
4
+ ## encoded cp932
5
+ ���{��
6
+
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mdpage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Masahiro Arakane
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdiscount
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.8
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: 1.6.8
30
+ description:
31
+ email: arafine@me.com
32
+ executables:
33
+ - mdpage
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - bin/mdpage
38
+ - lib/mdpage.rb
39
+ - lib/default.html.erb
40
+ - spec/mdpage_spec.rb
41
+ - spec/sample.md
42
+ - spec/sample2.md
43
+ homepage: https://github.com/arafine/mdpage.git
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.24
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Simple style document generator
67
+ test_files: []
68
+ has_rdoc: