tocmd 0.1.6 → 0.2.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: b059101b2d893f4a571cd00782ff0537ec5123c8
4
- data.tar.gz: 26cc39a37fb82e07de9eb8532a44580d10d4f819
3
+ metadata.gz: ead8d26564457484d965c84920ea6cc1849c5a96
4
+ data.tar.gz: d726c0ea146046332ede4d050010badc8f08f0f4
5
5
  SHA512:
6
- metadata.gz: dcbedff24185fcc1cfc9d3489dc7ffb8337e7036e2f1a302a9258a239f41b3f1c9d91ef7cfd3faf044b5e5a5367c28c73520321cc5c72085a7f102d8b53ba28a
7
- data.tar.gz: 32bd249b14aefd51658398332cf5f9d4a697df0328a574a29e0809dd122ec94c538f6881f31365e8d0c6bbd53c2bcb575c5987f3b6a71e34e11df3267450fd89
6
+ metadata.gz: 28a066a9a866653240fe2190cdc17854f8bd7823b905874c8abd9589335f982e45d780115c59b840bd1a87dcb49fdaee0e3ab84c9ed70bba0712e0a8d0f5e382
7
+ data.tar.gz: aa738fc2f1b49a53567c8324d71d74c59229f7988ee1871d060a57e22d989a380e35e74e578bc0ff352c15cdf8b1c9b79d0eae2e317c196e2ef9cac4e831dac1
data/bin/tocmd_local 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
@@ -10,11 +10,6 @@ class Tocmd::Translator
10
10
  @gem_root_path = File.expand_path('../', @source_file_path)
11
11
  #editor path
12
12
  @editor_path = Pathname.new(File.expand_path('../../../vendor', __FILE__)).realpath.to_s
13
-
14
- # p "@gem root_path:::"+@gem_root_path
15
- # p "@src file_path:::"+@source_file_path
16
- # p "@editor path :::"+@editor_path
17
-
18
13
  end
19
14
 
20
15
  def hi
@@ -141,7 +136,7 @@ class Tocmd::Translator
141
136
  if destiny_dir.to_s.index('/')
142
137
  css_link = %Q{
143
138
  <link href="#{@editor_path}/toc/style/github-bf51422f4bb36427d391e4b75a1daa083c2d840e.css" media="all" rel="stylesheet" type="text/css"/>
144
- <link href="#{@editor_path}/toc//style/github2-d731afd4f624c99a4b19ad69f3083cd6d02b81d5.css" media="all" rel="stylesheet" type="text/css"/>
139
+ <link href="#{@editor_path}/toc/style/github2-d731afd4f624c99a4b19ad69f3083cd6d02b81d5.css" media="all" rel="stylesheet" type="text/css"/>
145
140
  <link href="#{@editor_path}/toc/css/zTreeStyle/zTreeStyle.css" media="all" rel="stylesheet" type="text/css"/>
146
141
  }
147
142
  else
@@ -0,0 +1,305 @@
1
+ require 'pathname'
2
+ # require 'FileUtils'
3
+ require 'fileutils'
4
+
5
+ class Tocmd::TranslatorLocal
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
+ end
14
+
15
+ def hi
16
+ generate_meta_js
17
+ # cp_source_file_to_cur_file
18
+
19
+ ar = @source_file_path.split('/')
20
+ ar.pop()
21
+
22
+ puts "src path = #{ar.join('/').to_s}"
23
+ src_path = ar.join('/').to_s
24
+
25
+ ar.push('preview');
26
+ dest_dir = ar.join('/').to_s
27
+
28
+ puts "desc path = #{ar.join('/').to_s}"
29
+
30
+ # copy vendor/toc to dest directory
31
+ `cp -rf #{@editor_path}/toc #{dest_dir}`
32
+
33
+ # build now
34
+ build_with_dir(@source_file_path ,dest_dir)
35
+
36
+ # if mac open in browser
37
+ open_in_browser
38
+ end
39
+
40
+ def hi_dir
41
+ generate_meta_js
42
+ # cp_source_file_to_cur_file
43
+
44
+ ar = @source_file_path.split('/')
45
+ # ar.pop()
46
+
47
+ puts "hi_dir src path = #{ar.join('/').to_s}"
48
+ src_path = ar.join('/').to_s
49
+
50
+ ar.push('preview');
51
+ dest_dir = ar.join('/').to_s
52
+
53
+ puts "hi_dir desc path = #{ar.join('/').to_s}"
54
+
55
+ # copy vendor/toc to dest directory
56
+ `cp -rf #{@editor_path}/toc #{dest_dir}/toc`
57
+
58
+ build_with_dir(src_path ,dest_dir)
59
+
60
+ open_in_browser
61
+ end
62
+
63
+ def open_in_browser
64
+ ar = @source_file_path.split('/')
65
+
66
+ if File.directory?(@source_file_path) == false #普通文件
67
+ file_name = ar.pop().split('.')[0]
68
+ src_path = ar.join('/').to_s
69
+
70
+ ar.push('preview');
71
+ dest_dir = ar.join('/').to_s
72
+
73
+ `open #{ar.join('/').to_s}/#{file_name}.html`
74
+
75
+ else
76
+ # 目录
77
+ src_path = ar.join('/').to_s
78
+
79
+ Dir.foreach(src_path) do |ff|
80
+ file_name = ff.split('.')[0]
81
+ end
82
+
83
+ ar.push('preview');
84
+ dest_dir = ar.join('/').to_s
85
+ `open #{ar.join('/').to_s}/#{file_name}.html`
86
+ end
87
+ end
88
+
89
+ def generate_meta_js
90
+ f = File.new(File.join(@editor_path,"meta.js"), "w+")
91
+ f.puts("#{@source_file_path}")
92
+ end
93
+
94
+ def cp_source_file_to_cur_file
95
+ # cp param must be string
96
+ f = File.new(File.join(@editor_path,"cur.file"), "w+").path
97
+ FileUtils.cp(@source_file_path,f)
98
+ end
99
+
100
+ def build_with_dir(destiny_dir,dest_dir)
101
+ p "start building......"
102
+
103
+ if File.directory?(destiny_dir) == false
104
+ p 'process_with_one'
105
+ ar = @source_file_path.split('/')
106
+ file_name = ar.pop().split('.')[0]
107
+
108
+ src_path = ar.join('/').to_s
109
+
110
+ ar.push('preview');
111
+ dest_dir = ar.join('/').to_s
112
+
113
+ # return;
114
+ process_with_one(src_path,dest_dir,destiny_dir.split('/').pop().to_s)
115
+
116
+ return;
117
+ end
118
+
119
+ p "src_dir = #{destiny_dir}"
120
+ p "dest_dir = #{dest_dir}"
121
+ Dir.foreach(destiny_dir) do |ff|
122
+ # puts ff
123
+ unless /^\./ =~ ff ||/^images/ =~ ff ||/^css/ =~ ff || File.directory?(ff) || File.extname(ff) != '.md'
124
+
125
+ process_with_one(destiny_dir,dest_dir,ff)
126
+
127
+ end
128
+ end
129
+ end
130
+
131
+ def process_with_one(destiny_dir ,dest_dir ,ff)
132
+ # get markdown text
133
+ text = IO.read(destiny_dir + '/' + ff)
134
+
135
+ # options = [:fenced_code,:generate_toc,:hard_wrap,:no_intraemphasis,:strikethrough,:gh_blockcode,:autolink,:xhtml,:tables]
136
+
137
+ # convert to html
138
+ 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)
139
+ parse_markdown = markdown.render(text)
140
+ # parse_markdown = syntax_highlighter(parse_markdown)
141
+
142
+ css_link = ''
143
+ if destiny_dir.to_s.index('/')
144
+ css_link = %Q{
145
+ <link href="toc/style/github-bf51422f4bb36427d391e4b75a1daa083c2d840e.css" media="all" rel="stylesheet" type="text/css"/>
146
+ <link href="toc/style/github2-d731afd4f624c99a4b19ad69f3083cd6d02b81d5.css" media="all" rel="stylesheet" type="text/css"/>
147
+ <link href="toc/css/zTreeStyle/zTreeStyle.css" media="all" rel="stylesheet" type="text/css"/>
148
+ }
149
+ else
150
+ css_link = %Q{
151
+ <link href="toc/style/github-bf51422f4bb36427d391e4b75a1daa083c2d840e.css" media="all" rel="stylesheet" type="text/css"/>
152
+ <link href="toc/style/github2-d731afd4f624c99a4b19ad69f3083cd6d02b81d5.css" media="all" rel="stylesheet" type="text/css"/>
153
+ <link href="toc/css/zTreeStyle/zTreeStyle.css" media="all" rel="stylesheet" type="text/css"/>
154
+ }
155
+ end
156
+
157
+ t = %Q{
158
+ <html>
159
+ <head>
160
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
161
+ <title>#{ff.gsub('.md','')}</title>
162
+ #{css_link}
163
+ <style>
164
+ pre {
165
+ counter-reset: line-numbering;
166
+ border: solid 1px #d9d9d9;
167
+ border-radius: 0;
168
+ background: #fff;
169
+ padding: 0;
170
+ line-height: 23px;
171
+ margin-bottom: 30px;
172
+ white-space: pre;
173
+ overflow-x: auto;
174
+ word-break: inherit;
175
+ word-wrap: inherit;
176
+ }
177
+
178
+ pre a::before {
179
+ content: counter(line-numbering);
180
+ counter-increment: line-numbering;
181
+ padding-right: 1em; /* space after numbers */
182
+ width: 25px;
183
+ text-align: right;
184
+ opacity: 0.7;
185
+ display: inline-block;
186
+ color: #aaa;
187
+ background: #eee;
188
+ margin-right: 16px;
189
+ padding: 2px 10px;
190
+ font-size: 13px;
191
+ -webkit-touch-callout: none;
192
+ -webkit-user-select: none;
193
+ -khtml-user-select: none;
194
+ -moz-user-select: none;
195
+ -ms-user-select: none;
196
+ user-select: none;
197
+ }
198
+
199
+ pre a:first-of-type::before {
200
+ padding-top: 10px;
201
+ }
202
+
203
+ pre a:last-of-type::before {
204
+ padding-bottom: 10px;
205
+ }
206
+
207
+ pre a:only-of-type::before {
208
+ padding: 10px;
209
+ }
210
+
211
+ .highlight { background-color: #ffffcc } /* RIGHT */
212
+ </style>
213
+ </head>
214
+ <body>
215
+ <div>
216
+ <div style='width:25%;'>
217
+ <ul id="tree" class="ztree" style='width:100%'>
218
+
219
+ </ul>
220
+ </div>
221
+ <div id='readme' style='width:70%;margin-left:20%;'>
222
+ <article class='markdown-body'>
223
+ #{parse_markdown}
224
+ </article>
225
+ </div>
226
+ </div>
227
+ </body>
228
+ </html>
229
+ <script type="text/javascript" src="toc/js/jquery-1.4.4.min.js"></script>
230
+ <script type="text/javascript" src="toc/js/jquery.ztree.all-3.5.min.js"></script>
231
+ <script type="text/javascript" src="toc/js/ztree_toc.js"></script>
232
+ <SCRIPT type="text/javascript" >
233
+ <!--
234
+ $(document).ready(function(){
235
+ $('#tree').ztree_toc({
236
+ is_auto_number:true,
237
+ documment_selector:'.markdown-body'
238
+ });
239
+ });
240
+ //-->
241
+ </SCRIPT>
242
+ }
243
+
244
+ if destiny_dir.to_s.index('/')
245
+ # p '1build src/' + destiny_dir.to_s.split('/')[1] + '/' + ff.gsub('.md','') + '.html'
246
+ build_dir = 'preview/'
247
+
248
+ p 'build = ' + dest_dir + '/' + ff.gsub('.md','') + '.html'
249
+ IO.write(dest_dir + '/' + ff.gsub('.md','') + '.html',t) # => 10
250
+ else
251
+ # p '2build src/' + ff.gsub('.md','') + '.html'
252
+ build_dir = 'preview/'
253
+ # write to html file
254
+ IO.write(build_dir + '/' + ff.gsub('.md','') + '.html',t) # => 10
255
+ end
256
+ end
257
+
258
+ end
259
+
260
+
261
+ require 'redcarpet'
262
+ require 'pygments'
263
+
264
+ require 'redcarpet'
265
+ require 'rexml/document'
266
+ module Redcarpet::Render
267
+ class Custom < Base
268
+ def header(title, level)
269
+ @headers ||= []
270
+
271
+ title_elements = REXML::Document.new(title)
272
+ flattened_title = title_elements.inject('') do |flattened, element|
273
+ flattened += if element.respond_to?(:text)
274
+ element.text
275
+ else
276
+ element.to_s
277
+ end
278
+ end
279
+ permalink = flattened_title.downcase.gsub(/[^a-z\s]/, '').gsub(/\W+/, "-")
280
+
281
+ # for extra credit: implement this as its own method
282
+ if @headers.include?(permalink)
283
+ permalink += "_1"
284
+ # my brain hurts
285
+ loop do
286
+ break if !@headers.include?(permalink)
287
+ # generate titles like foo-bar_1, foo-bar_2
288
+ permalink.gsub!(/\_(\d+)$/, "_#{$1.to_i + 1}")
289
+ end
290
+ end
291
+ @headers << permalink
292
+ %(\n<h#{level}><a name="#{permalink}" class="anchor" href="##{permalink}"><span class="anchor-icon"></span></a>#{title}</h#{level}>\n)
293
+ end
294
+ end
295
+ end
296
+
297
+ class HTMLwithPygments < Redcarpet::Render::HTML
298
+ def doc_header()
299
+ '<style>' + Pygments.css('.highlight',:style => 'friendly') + '</style>'
300
+ end
301
+
302
+ def block_code(code, language)
303
+ Pygments.highlight(code, :lexer => language, :options => {:encoding => 'utf-8'})
304
+ end
305
+ end
data/lib/tocmd/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tocmd
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/tocmd.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require "tocmd/version"
2
2
  require "tocmd/translator"
3
+ require "tocmd/translator_local"
3
4
 
4
5
  module Tocmd
5
- # Your code goes here...
6
+ # ---------------- tocmd command -----------------
6
7
  def self.hi(a)
7
8
  translator = Translator.new(a)
8
9
  translator.hi
@@ -16,4 +17,19 @@ module Tocmd
16
17
  # puts version info
17
18
  p VERSION
18
19
  end
20
+
21
+ # ---------------- tocmd_local command -----------------
22
+ def self.hi_local(a)
23
+ translator = TranslatorLocal.new(a)
24
+ translator.hi
25
+ # puts version info
26
+ p VERSION
27
+ end
28
+
29
+ def self.hi_dir_local(a)
30
+ translator = TranslatorLocal.new(a)
31
+ translator.hi_dir
32
+ # puts version info
33
+ p VERSION
34
+ end
19
35
  end
data/vendor/meta.js CHANGED
@@ -1 +1 @@
1
- /Volumes/XP/git/tocmd.gem/sample.md
1
+ /Users/shiren1118/Workspace/github/tocmd.gem/test_data/sample.md
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tocmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shiren1118
@@ -15,13 +15,16 @@ email:
15
15
  - shiren1118@126.com
16
16
  executables:
17
17
  - tocmd
18
+ - tocmd_local
18
19
  extensions: []
19
20
  extra_rdoc_files: []
20
21
  files:
21
22
  - bin/tocmd
23
+ - bin/tocmd_local
22
24
  - lib/tocmd.rb
23
25
  - lib/tocmd/markdown_render.rb
24
26
  - lib/tocmd/translator.rb
27
+ - lib/tocmd/translator_local.rb
25
28
  - lib/tocmd/version.rb
26
29
  - test/mdptest.rb
27
30
  - vendor/meta.js