md2cnblog 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem "redcarpet", ">= 2.3.0"
9
+
10
+ group :development do
11
+ gem "rspec", "~> 2.13.0"
12
+ gem "rdoc", "~> 3.9.5"
13
+ gem "bundler"
14
+ gem "jeweler", "~> 1.8.4"
15
+ gem "redcarpet", ">= 2.3.0"
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.2.4)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ rake (10.0.4)
12
+ rdoc (3.9.5)
13
+ redcarpet (2.3.0)
14
+ rspec (2.13.0)
15
+ rspec-core (~> 2.13.0)
16
+ rspec-expectations (~> 2.13.0)
17
+ rspec-mocks (~> 2.13.0)
18
+ rspec-core (2.13.1)
19
+ rspec-expectations (2.13.0)
20
+ diff-lcs (>= 1.1.3, < 2.0)
21
+ rspec-mocks (2.13.1)
22
+
23
+ PLATFORMS
24
+ ruby
25
+ x86-mingw32
26
+
27
+ DEPENDENCIES
28
+ bundler
29
+ jeweler (~> 1.8.4)
30
+ rdoc (~> 3.9.5)
31
+ redcarpet (>= 2.3.0)
32
+ rspec (~> 2.13.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 easonhan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ md2cnblog
2
+ ---------
3
+
4
+ md2cnblog可以轻松的将markdown格式的文本转换成相应blog提供商所支持的html代码,并自动代码高亮之。
5
+
6
+ 使用方法
7
+ ```
8
+ mdb mypost.txt -t cnblog > mypost.html
9
+ ```
10
+ Contributing to md2cnblog
11
+ =========================
12
+
13
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
14
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
15
+ * Fork the project.
16
+ * Start a feature/bugfix branch.
17
+ * Commit and push until you are happy with your contribution.
18
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
19
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
20
+
21
+ Copyright
22
+ =========
23
+
24
+ Copyright (c) 2013 easonhan. See LICENSE.txt for
25
+ further details.
26
+
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = md2cnblog
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to md2cnblog
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 easonhan. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "md2cnblog"
18
+ gem.homepage = "http://github.com/easonhan007/md2cnblog"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{An easy way to convert markdown file to cnblogs and itetye html}
21
+ gem.description = %Q{An command line tool to convert markdown file to cnblogs and itetye html}
22
+ gem.email = "nbkhic@qq.com"
23
+ gem.authors = ["easonhan"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "md2cnblog #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/mdt ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib_path = File.expand_path('../../lib', __FILE__)
4
+ $:.unshift(lib_path)
5
+
6
+ require 'md2cnblog/cli'
data/lib/md2cnblog.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'redcarpet'
2
+ require 'md2cnblog/errors'
3
+ require 'md2cnblog/base_translator'
4
+ require 'md2cnblog/iteye_translator'
5
+ require 'md2cnblog/cnblog_translator'
@@ -0,0 +1,48 @@
1
+ module Md2Cnblog
2
+ class BaseTranslator
3
+ attr_reader :markdown, :options, :output
4
+ attr_accessor :render
5
+
6
+ def initialize(str, options={}, render=nil)
7
+ @str ||= str if valide_str?(str)
8
+ @options = default_options.merge(options) if valide_option?(options)
9
+ @render = render || default_render
10
+ @markdown = ::Redcarpet::Markdown.new(@render, @options)
11
+ end
12
+
13
+ def valide_str?(str)
14
+ raise InvalidInputError unless str.is_a?(String)
15
+ true
16
+ end
17
+
18
+ def valide_option?(options)
19
+ raise InvalidOptionsError unless options.is_a?(Hash)
20
+ true
21
+ end
22
+
23
+ def default_options
24
+ { no_intra_emphasis: true,
25
+ autolink: true,
26
+ space_after_headers: true,
27
+ fenced_code_blocks: true }
28
+ end
29
+
30
+ def default_render
31
+ Redcarpet::Render::HTML.new()
32
+ end
33
+
34
+ def start
35
+ start_mute
36
+ echo
37
+ end
38
+
39
+ def start_mute
40
+ @output = @markdown.render(@str)
41
+ end
42
+
43
+ def echo
44
+ print @output
45
+ end
46
+
47
+ end #BaseTranslator
48
+ end #Md2Cnblog
@@ -0,0 +1,51 @@
1
+ require 'optparse'
2
+ require 'ostruct'
3
+ require 'md2cnblog'
4
+
5
+ module Md2Cnblog
6
+ class OptParser
7
+
8
+ def initialize()
9
+ @option = OpenStruct.new
10
+ end
11
+
12
+ def do(args)
13
+ parser = OptionParser.new do |opts|
14
+ opts.banner = 'Usage: mdt [type] filename'
15
+
16
+ opts.separator ""
17
+ opts.separator "Specific options:"
18
+
19
+ opts.on('-t', '--type [BLOGTYPE]',
20
+ 'the blog type to translate') do |t|
21
+ @option.type = t
22
+ end
23
+
24
+ opts.separator ''
25
+ opts.separator 'Common options:'
26
+
27
+ opts.on_tail('-h', '--help', 'show this message') do
28
+ puts opts
29
+ exit
30
+ end
31
+ end # new
32
+ parser.parse!(args)
33
+ @option
34
+ end # do
35
+
36
+ end # OptParser
37
+
38
+ o = OptParser.new.do(ARGV)
39
+ type = o.type || 'base'
40
+
41
+ raise 'your should specify a file' if ARGV.empty?
42
+ file = ARGV.first
43
+ str = ''
44
+ File.open(file) do |f|
45
+ str = f.read
46
+ end
47
+
48
+ kls = "#{type.capitalize}Translator".to_sym
49
+ Md2Cnblog.const_get(kls).new(str).start
50
+
51
+ end #Md2Cnblog
@@ -0,0 +1,20 @@
1
+ class CnblogRender < ::Redcarpet::Render::HTML
2
+
3
+ def block_code(code, lang)
4
+ tail = 'gutter:false;'
5
+ class_name = (lang.nil?)? '' : "brush:#{lang};"
6
+ %Q[<pre class="#{class_name}#{tail}">#{code}</pre>]
7
+ end
8
+
9
+ end #CnblogRender
10
+
11
+ module Md2Cnblog
12
+ class CnblogTranslator < BaseTranslator
13
+
14
+ def initialize(str)
15
+ super(str, {}, ::CnblogRender)
16
+ end
17
+
18
+ end #IteyeTranslator
19
+
20
+ end #Md2Cnblog
@@ -0,0 +1,5 @@
1
+ module Md2Cnblog
2
+ class Md2CnblogError < StandardError; end
3
+ class InvalidInputError < Md2CnblogError; end
4
+ class InvalidOptionsError < Md2CnblogError; end
5
+ end
@@ -0,0 +1,20 @@
1
+ class IteyeRender < ::Redcarpet::Render::HTML
2
+
3
+ def block_code(code, lang)
4
+ class_name = (lang.nil?)? '' : %Q[ class="#{lang}" ]
5
+ %Q[<pre name="code" #{class_name}>#{code}</pre>]
6
+ end
7
+
8
+ end #IteyeRender
9
+
10
+ module Md2Cnblog
11
+
12
+ class IteyeTranslator < BaseTranslator
13
+
14
+ def initialize(str)
15
+ super(str, {}, ::IteyeRender)
16
+ end
17
+
18
+ end #IteyeTranslator
19
+
20
+ end #Md2Cnblog
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Md2Cnblog::BaseTranslator do
4
+ before(:all) do
5
+ @options = {no_intra_emphasis: true, autolink: true, space_after_headers: true}
6
+ end
7
+
8
+ it "should raise InvalidInputError" do
9
+ lambda { Md2Cnblog::BaseTranslator.new(Object.new) }.should raise_error(Md2Cnblog::InvalidInputError)
10
+ lambda { Md2Cnblog::BaseTranslator.new('string') }.should_not raise_error
11
+ end
12
+
13
+ it 'should init markdown' do
14
+ translator = Md2Cnblog::BaseTranslator.new('string')
15
+ translator.markdown.should be_instance_of(Redcarpet::Markdown)
16
+ end
17
+
18
+ it 'should using default options' do
19
+ options = {no_intra_emphasis: true, autolink: true, space_after_headers: true}
20
+ translator = Md2Cnblog::BaseTranslator.new('string')
21
+ translator.options.should eql(@options)
22
+ end
23
+
24
+ it 'should merge options to default options' do
25
+ new_options = { space_after_headers: true, fenced_code_blocks: true }
26
+ expected = @options.merge(new_options)
27
+ translator = Md2Cnblog::BaseTranslator.new('string', new_options)
28
+ translator.options.should eql(expected)
29
+ end
30
+
31
+ it 'should raise InvalidOptionsError when option is not hash' do
32
+ lambda { Md2Cnblog::BaseTranslator.new('string', Object.new) }.should raise_error(Md2Cnblog::InvalidOptionsError)
33
+ end
34
+
35
+ it 'should use default render' do
36
+ translator = Md2Cnblog::BaseTranslator.new('string')
37
+ translator.render.should be_instance_of(Redcarpet::Render::HTML)
38
+ end
39
+
40
+ it 'should render when calling start method' do
41
+ translator = Md2Cnblog::BaseTranslator.new('string')
42
+ translator.instance_variable_set(:@markdown, double(Redcarpet::Markdown))
43
+ translator.instance_variable_get(:@markdown).should_receive(:render).with('string')
44
+ translator.start()
45
+ end
46
+
47
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'md2cnblog'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: md2cnblog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - easonhan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redcarpet
16
+ requirement: &16771356 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *16771356
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &16770012 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.13.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *16770012
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &16768608 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 3.9.5
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *16768608
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &16789668 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *16789668
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &16788492 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.8.4
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *16788492
69
+ - !ruby/object:Gem::Dependency
70
+ name: redcarpet
71
+ requirement: &16786272 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: 2.3.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *16786272
80
+ description: An command line tool to convert markdown file to cnblogs and itetye html
81
+ email: nbkhic@qq.com
82
+ executables:
83
+ - mdt
84
+ extensions: []
85
+ extra_rdoc_files:
86
+ - LICENSE.txt
87
+ - README.md
88
+ - README.rdoc
89
+ files:
90
+ - .document
91
+ - .rspec
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - README.rdoc
97
+ - Rakefile
98
+ - VERSION
99
+ - bin/mdt
100
+ - lib/md2cnblog.rb
101
+ - lib/md2cnblog/base_translator.rb
102
+ - lib/md2cnblog/cli.rb
103
+ - lib/md2cnblog/cnblog_translator.rb
104
+ - lib/md2cnblog/errors.rb
105
+ - lib/md2cnblog/iteye_translator.rb
106
+ - spec/md2cnblog_spec.rb
107
+ - spec/spec_helper.rb
108
+ homepage: http://github.com/easonhan007/md2cnblog
109
+ licenses:
110
+ - MIT
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ segments:
122
+ - 0
123
+ hash: -871412783
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 1.8.16
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: An easy way to convert markdown file to cnblogs and itetye html
136
+ test_files: []