tocmd 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ead8d26564457484d965c84920ea6cc1849c5a96
4
- data.tar.gz: d726c0ea146046332ede4d050010badc8f08f0f4
3
+ metadata.gz: 4742aa16c227b9e8d339d3b1a1cf75c4c75ee0ba
4
+ data.tar.gz: c4879204dc0e2b7acf74e0057682ac302cf94e1a
5
5
  SHA512:
6
- metadata.gz: 28a066a9a866653240fe2190cdc17854f8bd7823b905874c8abd9589335f982e45d780115c59b840bd1a87dcb49fdaee0e3ab84c9ed70bba0712e0a8d0f5e382
7
- data.tar.gz: aa738fc2f1b49a53567c8324d71d74c59229f7988ee1871d060a57e22d989a380e35e74e578bc0ff352c15cdf8b1c9b79d0eae2e317c196e2ef9cac4e831dac1
6
+ metadata.gz: 0cadc5bed77b8fab06e6d2da41c7a491add78dea316d725a5b02e7dbfdd51567969a469741378a8b932c1e0a708fe5f7a075e61dbebf5f6588ac05ad94be8422
7
+ data.tar.gz: f356897c0fb02a40336e5f46f23ec3a87486d9144f69e06113a57398c19d674f257d881a95bbb3e04d2d53cf312da3012b3c19000b71cc7608cb09263cc6a95d
data/bin/tocmd CHANGED
File without changes
data/bin/tocmd_conf ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'tocmd'
5
+ require 'pathname'
6
+ # p ARGV
7
+ # puts Mdpreview.hi(ARGV)
8
+ #
9
+ # puts __FILE__
10
+ # puts File.dirname(__FILE__)
11
+
12
+
13
+ # File.join(File.dirname(__FILE__), '..', 'lib')
14
+ path = Pathname.new(File.join(File.dirname(__FILE__), '..')).realpath.to_s
15
+
16
+ # p "开始#{Dir.pwd}"
17
+ # p "path = #{path}"
18
+
19
+ require 'optparse'
20
+
21
+ options = {}
22
+ option_parser = OptionParser.new do |opts|
23
+ # 这里是这个命令行工具的帮助信息
24
+ opts.banner = 'preview markdown file with toc of the command line tool.'
25
+
26
+ # # Option 作为switch,不带argument,用于将switch设置成true或false
27
+ # options[:switch] = false
28
+ # # 下面第一项是Short option(没有可以直接在引号间留空),第二项是Long option,第三项是对Option的描述
29
+ # opts.on('-s', '--switch', 'Set options as switch') do
30
+ # # 这个部分就是使用这个Option后执行的代码
31
+ # options[:switch] = true
32
+ # end
33
+ vendor_path = Pathname.new(File.expand_path('../../vendor/', __FILE__)).realpath.to_s
34
+
35
+ src_p = Dir.pwd
36
+
37
+ begin
38
+ Dir.delelte(File.join(src_p ,"preview"))
39
+ Dir.mkdir(File.join(src_p ,"preview"), 0700) #=> 0
40
+ rescue
41
+ ensure
42
+ end
43
+
44
+ begin
45
+ Dir.mkdir(File.join(src_p ,"preview"), 0700) #=> 0
46
+ rescue
47
+ ensure
48
+ end
49
+
50
+ # begin
51
+ # Dir.mkdir(File.join(src_p ,".toc"), 0700) #=> 0
52
+ # rescue
53
+ # ensure
54
+ # f = File.new(File.join(vendor_path ,"template.html"), "w+").path
55
+ # FileUtils.cp(f ,File.join(src_p ,".toc"))
56
+ # end
57
+ #
58
+
59
+ # Option 作为flag,带argument,用于将argument作为数值解析,比如"name"信息
60
+ #下面的“value”就是用户使用时输入的argument
61
+ opts.on('-f sourceFileNAME', '--name sourceFileNAME', 'Pass-in source file name') do |value|
62
+ # options[:fname] = value
63
+
64
+ Tocmd.hi_local(src_p + '/' + value)
65
+ end
66
+
67
+ opts.on('-d dirNAME', '--name sourceFileNAME', 'Pass-in source file name') do |value|
68
+ # options[:fname] = value
69
+ p value
70
+
71
+ if value == '.'
72
+ Tocmd.hi_dir_local(src_p + '/' )
73
+ else
74
+ Tocmd.hi_dir_local(src_p + '/' + value)
75
+ end
76
+
77
+ end
78
+
79
+ # Option 作为flag,带一组用逗号分割的arguments,用于将arguments作为数组解析
80
+ # opts.on('-a A,B', '--array A,B', Array, 'List of arguments') do |value|
81
+ # options[:array] = value
82
+ # end
83
+ end.parse!
84
+
85
+ # puts options.inspect
@@ -0,0 +1,333 @@
1
+ require 'pathname'
2
+ # require 'FileUtils'
3
+ require 'fileutils'
4
+
5
+ class Tocmd::TranslatorConf
6
+ def initialize(source_file_path)
7
+ #源文件路径
8
+ @source_file_path = source_file_path
9
+ #gem跟目录
10
+ @gem_root_path = File.expand_path('../', @source_file_path)
11
+ #editor path
12
+ @editor_path = Pathname.new(File.expand_path('../../../vendor', __FILE__)).realpath.to_s
13
+
14
+ end
15
+
16
+ def hi
17
+ generate_meta_js
18
+ # cp_source_file_to_cur_file
19
+
20
+ ar = @source_file_path.split('/')
21
+ ar.pop()
22
+
23
+ puts "src path = #{ar.join('/').to_s}"
24
+ src_path = ar.join('/').to_s
25
+
26
+ ar.push('preview');
27
+ dest_dir = ar.join('/').to_s
28
+
29
+ puts "desc path = #{ar.join('/').to_s}"
30
+
31
+ # copy vendor/toc to dest directory
32
+ `cp -rf #{@editor_path}/toc #{dest_dir}`
33
+
34
+ _toc_config(dest_dir)
35
+
36
+ # build now
37
+ build_with_dir(@source_file_path ,dest_dir)
38
+
39
+ # if mac open in browser
40
+ open_in_browser
41
+ end
42
+
43
+ def hi_dir
44
+ generate_meta_js
45
+ # cp_source_file_to_cur_file
46
+
47
+ ar = @source_file_path.split('/')
48
+ # ar.pop()
49
+
50
+ puts "hi_dir src path = #{ar.join('/').to_s}"
51
+ src_path = ar.join('/').to_s
52
+
53
+ ar.push('preview');
54
+ dest_dir = ar.join('/').to_s
55
+
56
+ puts "hi_dir desc path = #{ar.join('/').to_s}"
57
+
58
+ # copy vendor/toc to dest directory
59
+ `cp -rf #{@editor_path}/toc #{dest_dir}/toc`
60
+
61
+ _toc_config(dest_dir)
62
+
63
+ build_with_dir(src_path ,dest_dir)
64
+
65
+ open_in_browser
66
+ end
67
+
68
+ def _toc_config(dest_dir)
69
+ if File.exist?("#{dest_dir}/toc_conf.js")
70
+ puts 'toc_conf file exist'
71
+ else
72
+ # if file not exit,create toc_conf
73
+ `touch #{dest_dir}/toc_conf.js if ! -f #{dest_dir}/toc_conf.js`
74
+
75
+ # fill data to toc_conf.js
76
+ `echo 'var jquery_ztree_toc_opts = {'>#{dest_dir}/toc_conf.js`
77
+ `echo 'is_auto_number:true,' >> #{dest_dir}/toc_conf.js`
78
+ `echo "documment_selector:'.markdown-body'" >> #{dest_dir}/toc_conf.js`
79
+
80
+ `echo '};' >> #{dest_dir}/toc_conf.js`
81
+
82
+ `echo "var markdown_panel_style = {'width':'70%','margin-left':'20%'};">> #{dest_dir}/toc_conf.js`
83
+ end
84
+ end
85
+
86
+ def open_in_browser
87
+ ar = @source_file_path.split('/')
88
+
89
+ if File.directory?(@source_file_path) == false #普通文件
90
+ file_name = ar.pop().split('.')[0]
91
+ src_path = ar.join('/').to_s
92
+
93
+ ar.push('preview');
94
+ dest_dir = ar.join('/').to_s
95
+
96
+ `open #{ar.join('/').to_s}/#{file_name}.html`
97
+
98
+ else
99
+ # 目录
100
+ src_path = ar.join('/').to_s
101
+
102
+ Dir.foreach(src_path) do |ff|
103
+ file_name = ff.split('.')[0]
104
+ end
105
+
106
+ ar.push('preview');
107
+ dest_dir = ar.join('/').to_s
108
+ `open #{ar.join('/').to_s}/#{file_name}.html`
109
+ end
110
+ end
111
+
112
+ def generate_meta_js
113
+ f = File.new(File.join(@editor_path,"meta.js"), "w+")
114
+ f.puts("#{@source_file_path}")
115
+ end
116
+
117
+ def cp_source_file_to_cur_file
118
+ # cp param must be string
119
+ f = File.new(File.join(@editor_path,"cur.file"), "w+").path
120
+ FileUtils.cp(@source_file_path,f)
121
+ end
122
+
123
+ def build_with_dir(destiny_dir,dest_dir)
124
+ p "start building......"
125
+
126
+ if File.directory?(destiny_dir) == false
127
+ p 'process_with_one'
128
+ ar = @source_file_path.split('/')
129
+ file_name = ar.pop().split('.')[0]
130
+
131
+ src_path = ar.join('/').to_s
132
+
133
+ ar.push('preview');
134
+ dest_dir = ar.join('/').to_s
135
+
136
+ # return;
137
+ process_with_one(src_path,dest_dir,destiny_dir.split('/').pop().to_s)
138
+
139
+ return;
140
+ end
141
+
142
+ p "src_dir = #{destiny_dir}"
143
+ p "dest_dir = #{dest_dir}"
144
+ Dir.foreach(destiny_dir) do |ff|
145
+ # puts ff
146
+ unless /^\./ =~ ff ||/^images/ =~ ff ||/^css/ =~ ff || File.directory?(ff) || File.extname(ff) != '.md'
147
+
148
+ process_with_one(destiny_dir,dest_dir,ff)
149
+
150
+ end
151
+ end
152
+ end
153
+
154
+ def process_with_one(destiny_dir ,dest_dir ,ff)
155
+ # get markdown text
156
+ text = IO.read(destiny_dir + '/' + ff)
157
+
158
+ # options = [:fenced_code,:generate_toc,:hard_wrap,:no_intraemphasis,:strikethrough,:gh_blockcode,:autolink,:xhtml,:tables]
159
+
160
+ # convert to html
161
+ markdown = Redcarpet::Markdown.new(HTMLwithPygments,:gh_blockcode=>true,:no_intra_emphasis=>true,:filter_html => true,:hard_wrap => true,:autolink => false, :space_after_headers => true,:fenced_code_blocks => true,:tables => true)
162
+ parse_markdown = markdown.render(text)
163
+ # parse_markdown = syntax_highlighter(parse_markdown)
164
+
165
+ css_link = ''
166
+ if destiny_dir.to_s.index('/')
167
+ css_link = %Q{
168
+ <link href="toc/style/github-bf51422f4bb36427d391e4b75a1daa083c2d840e.css" media="all" rel="stylesheet" type="text/css"/>
169
+ <link href="toc/style/github2-d731afd4f624c99a4b19ad69f3083cd6d02b81d5.css" media="all" rel="stylesheet" type="text/css"/>
170
+ <link href="toc/css/zTreeStyle/zTreeStyle.css" media="all" rel="stylesheet" type="text/css"/>
171
+ }
172
+ else
173
+ css_link = %Q{
174
+ <link href="toc/style/github-bf51422f4bb36427d391e4b75a1daa083c2d840e.css" media="all" rel="stylesheet" type="text/css"/>
175
+ <link href="toc/style/github2-d731afd4f624c99a4b19ad69f3083cd6d02b81d5.css" media="all" rel="stylesheet" type="text/css"/>
176
+ <link href="toc/css/zTreeStyle/zTreeStyle.css" media="all" rel="stylesheet" type="text/css"/>
177
+ }
178
+ end
179
+
180
+ t = %Q{
181
+ <html>
182
+ <head>
183
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
184
+ <title>#{ff.gsub('.md','')}</title>
185
+ #{css_link}
186
+ <style>
187
+ pre {
188
+ counter-reset: line-numbering;
189
+ border: solid 1px #d9d9d9;
190
+ border-radius: 0;
191
+ background: #fff;
192
+ padding: 0;
193
+ line-height: 23px;
194
+ margin-bottom: 30px;
195
+ white-space: pre;
196
+ overflow-x: auto;
197
+ word-break: inherit;
198
+ word-wrap: inherit;
199
+ }
200
+
201
+ pre a::before {
202
+ content: counter(line-numbering);
203
+ counter-increment: line-numbering;
204
+ padding-right: 1em; /* space after numbers */
205
+ width: 25px;
206
+ text-align: right;
207
+ opacity: 0.7;
208
+ display: inline-block;
209
+ color: #aaa;
210
+ background: #eee;
211
+ margin-right: 16px;
212
+ padding: 2px 10px;
213
+ font-size: 13px;
214
+ -webkit-touch-callout: none;
215
+ -webkit-user-select: none;
216
+ -khtml-user-select: none;
217
+ -moz-user-select: none;
218
+ -ms-user-select: none;
219
+ user-select: none;
220
+ }
221
+
222
+ pre a:first-of-type::before {
223
+ padding-top: 10px;
224
+ }
225
+
226
+ pre a:last-of-type::before {
227
+ padding-bottom: 10px;
228
+ }
229
+
230
+ pre a:only-of-type::before {
231
+ padding: 10px;
232
+ }
233
+
234
+ .highlight { background-color: #ffffcc } /* RIGHT */
235
+ </style>
236
+ </head>
237
+ <body>
238
+ <div>
239
+ <div style='width:25%;'>
240
+ <ul id="tree" class="ztree" style='width:100%'>
241
+
242
+ </ul>
243
+ </div>
244
+ <div id='readme' style='width:70%;margin-left:20%;'>
245
+ <article class='markdown-body'>
246
+ #{parse_markdown}
247
+ </article>
248
+ </div>
249
+ </div>
250
+ </body>
251
+ </html>
252
+ <script type="text/javascript" src="toc/js/jquery-1.4.4.min.js"></script>
253
+ <script type="text/javascript" src="toc/js/jquery.ztree.all-3.5.min.js"></script>
254
+ <script type="text/javascript" src="toc/js/ztree_toc.js"></script>
255
+ <script type="text/javascript" src="toc_conf.js"></script>
256
+
257
+ <SCRIPT type="text/javascript" >
258
+ <!--
259
+ $(document).ready(function(){
260
+ var css_conf = eval(markdown_panel_style);
261
+ $('#readme').css(css_conf)
262
+
263
+ var conf = eval(jquery_ztree_toc_opts);
264
+ $('#tree').ztree_toc(conf);
265
+
266
+
267
+ });
268
+ //-->
269
+ </SCRIPT>
270
+ }
271
+
272
+ if destiny_dir.to_s.index('/')
273
+ # p '1build src/' + destiny_dir.to_s.split('/')[1] + '/' + ff.gsub('.md','') + '.html'
274
+ build_dir = 'preview/'
275
+
276
+ p 'build = ' + dest_dir + '/' + ff.gsub('.md','') + '.html'
277
+ IO.write(dest_dir + '/' + ff.gsub('.md','') + '.html',t) # => 10
278
+ else
279
+ # p '2build src/' + ff.gsub('.md','') + '.html'
280
+ build_dir = 'preview/'
281
+ # write to html file
282
+ IO.write(build_dir + '/' + ff.gsub('.md','') + '.html',t) # => 10
283
+ end
284
+ end
285
+
286
+ end
287
+
288
+
289
+ require 'redcarpet'
290
+ require 'pygments'
291
+
292
+ require 'redcarpet'
293
+ require 'rexml/document'
294
+ module Redcarpet::Render
295
+ class Custom < Base
296
+ def header(title, level)
297
+ @headers ||= []
298
+
299
+ title_elements = REXML::Document.new(title)
300
+ flattened_title = title_elements.inject('') do |flattened, element|
301
+ flattened += if element.respond_to?(:text)
302
+ element.text
303
+ else
304
+ element.to_s
305
+ end
306
+ end
307
+ permalink = flattened_title.downcase.gsub(/[^a-z\s]/, '').gsub(/\W+/, "-")
308
+
309
+ # for extra credit: implement this as its own method
310
+ if @headers.include?(permalink)
311
+ permalink += "_1"
312
+ # my brain hurts
313
+ loop do
314
+ break if !@headers.include?(permalink)
315
+ # generate titles like foo-bar_1, foo-bar_2
316
+ permalink.gsub!(/\_(\d+)$/, "_#{$1.to_i + 1}")
317
+ end
318
+ end
319
+ @headers << permalink
320
+ %(\n<h#{level}><a name="#{permalink}" class="anchor" href="##{permalink}"><span class="anchor-icon"></span></a>#{title}</h#{level}>\n)
321
+ end
322
+ end
323
+ end
324
+
325
+ class HTMLwithPygments < Redcarpet::Render::HTML
326
+ def doc_header()
327
+ '<style>' + Pygments.css('.highlight',:style => 'friendly') + '</style>'
328
+ end
329
+
330
+ def block_code(code, language)
331
+ Pygments.highlight(code, :lexer => language, :options => {:encoding => 'utf-8'})
332
+ end
333
+ end
data/lib/tocmd/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tocmd
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/tocmd.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "tocmd/version"
2
2
  require "tocmd/translator"
3
3
  require "tocmd/translator_local"
4
+ require "tocmd/translator_conf"
4
5
 
5
6
  module Tocmd
6
7
  # ---------------- tocmd command -----------------
@@ -32,4 +33,20 @@ module Tocmd
32
33
  # puts version info
33
34
  p VERSION
34
35
  end
36
+
37
+ # ---------------- tocmd_conf command -----------------
38
+ def self.hi_local(a)
39
+ translator = TranslatorConf.new(a)
40
+ translator.hi
41
+ # puts version info
42
+ p VERSION
43
+ end
44
+
45
+ def self.hi_dir_local(a)
46
+ translator = TranslatorConf.new(a)
47
+ translator.hi_dir
48
+ # puts version info
49
+ p VERSION
50
+ end
51
+
35
52
  end
metadata CHANGED
@@ -1,29 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tocmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shiren1118
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-01 00:00:00.000000000 Z
11
+ date: 2014-08-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: convert markdown to html with toc plugin
13
+ description: convert markdown to html with i5ting_ztree_toc plugin
14
14
  email:
15
15
  - shiren1118@126.com
16
16
  executables:
17
17
  - tocmd
18
+ - tocmd_conf
18
19
  - tocmd_local
19
20
  extensions: []
20
21
  extra_rdoc_files: []
21
22
  files:
22
23
  - bin/tocmd
24
+ - bin/tocmd_conf
23
25
  - bin/tocmd_local
24
26
  - lib/tocmd.rb
25
27
  - lib/tocmd/markdown_render.rb
26
28
  - lib/tocmd/translator.rb
29
+ - lib/tocmd/translator_conf.rb
27
30
  - lib/tocmd/translator_local.rb
28
31
  - lib/tocmd/version.rb
29
32
  - test/mdptest.rb
@@ -78,6 +81,6 @@ rubyforge_project:
78
81
  rubygems_version: 2.2.2
79
82
  signing_key:
80
83
  specification_version: 4
81
- summary: convert markdown to html with toc plugin
84
+ summary: convert markdown to html with i5ting_ztree_toc plugin
82
85
  test_files:
83
86
  - test/mdptest.rb