Class: Tocmd::TranslatorLocal

Inherits:
Object
  • Object
show all
Defined in:
lib/tocmd/translator_local.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_file_path) ⇒ TranslatorLocal

Returns a new instance of TranslatorLocal.



6
7
8
9
10
11
12
13
# File 'lib/tocmd/translator_local.rb', line 6

def initialize(source_file_path) 
  #源文件路径
  @source_file_path = source_file_path    
  #gem跟目录                          
  @gem_root_path = File.expand_path('../', @source_file_path)   
  #editor path
  @editor_path =  Pathname.new(File.expand_path('../../../vendor', __FILE__)).realpath.to_s  
end

Instance Method Details

#build_with_dir(destiny_dir, dest_dir) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tocmd/translator_local.rb', line 100

def build_with_dir(destiny_dir,dest_dir)
	p "start building......"
	
	if File.directory?(destiny_dir) == false
		p 'process_with_one'
		ar = @source_file_path.split('/')
		file_name = ar.pop().split('.')[0]
	
		src_path = ar.join('/').to_s

		ar.push('preview');
		dest_dir = ar.join('/').to_s		
		
		# return;
		process_with_one(src_path,dest_dir,destiny_dir.split('/').pop().to_s)
		
		return;
	end
	
	p "src_dir = #{destiny_dir}"
	p "dest_dir = #{dest_dir}"
	Dir.foreach(destiny_dir) do |ff| 
	  # puts ff
		unless /^\./ =~ ff ||/^images/ =~ ff ||/^css/ =~ ff || File.directory?(ff) || File.extname(ff) != '.md'
			
			process_with_one(destiny_dir,dest_dir,ff)
		
		end
	end 
end

#cp_source_file_to_cur_fileObject



94
95
96
97
98
# File 'lib/tocmd/translator_local.rb', line 94

def cp_source_file_to_cur_file
# cp param must be string
  f = File.new(File.join(@editor_path,"cur.file"), "w+").path
  FileUtils.cp(@source_file_path,f)
end

#generate_meta_jsObject



89
90
91
92
# File 'lib/tocmd/translator_local.rb', line 89

def generate_meta_js
  f = File.new(File.join(@editor_path,"meta.js"), "w+")
  f.puts("#{@source_file_path}")
end

#hiObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tocmd/translator_local.rb', line 15

def hi
    generate_meta_js
    # cp_source_file_to_cur_file
    
	ar = @source_file_path.split('/')
	ar.pop()
	
	puts "src path = #{ar.join('/').to_s}"
	src_path = ar.join('/').to_s
	
	ar.push('preview');
	dest_dir = ar.join('/').to_s
	
	puts "desc path = #{ar.join('/').to_s}"
    
    # copy vendor/toc to dest directory
    `cp -rf #{@editor_path}/toc #{dest_dir}`
	
    # build now
	build_with_dir(@source_file_path ,dest_dir)
	
    # if mac open in browser
	open_in_browser
end

#hi_dirObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tocmd/translator_local.rb', line 40

def hi_dir
    generate_meta_js
    # cp_source_file_to_cur_file
  
	ar = @source_file_path.split('/')
	# ar.pop()
	
	puts "hi_dir src path = #{ar.join('/').to_s}"
	src_path = ar.join('/').to_s
	
	ar.push('preview');
	dest_dir = ar.join('/').to_s
	
	puts "hi_dir desc path = #{ar.join('/').to_s}"
    
    # copy vendor/toc to dest directory
    `cp -rf #{@editor_path}/toc #{dest_dir}/toc`
	
	build_with_dir(src_path ,dest_dir)
	
	open_in_browser
end

#open_in_browserObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tocmd/translator_local.rb', line 63

def open_in_browser
ar = @source_file_path.split('/')

if File.directory?(@source_file_path) == false #普通文件
		file_name = ar.pop().split('.')[0]
		src_path = ar.join('/').to_s

		ar.push('preview');
		dest_dir = ar.join('/').to_s		
		
		`open #{ar.join('/').to_s}/#{file_name}.html`		

else  
	# 目录
	src_path = ar.join('/').to_s

	Dir.foreach(src_path) do |ff| 
		file_name = ff.split('.')[0]
	end

	ar.push('preview');
	dest_dir = ar.join('/').to_s		
	`open #{ar.join('/').to_s}/#{file_name}.html`		
end
end

#process_with_one(destiny_dir, dest_dir, ff) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/tocmd/translator_local.rb', line 131

def process_with_one(destiny_dir ,dest_dir ,ff)
	# get markdown text
	text = IO.read(destiny_dir + '/' + ff)

	# options = [:fenced_code,:generate_toc,:hard_wrap,:no_intraemphasis,:strikethrough,:gh_blockcode,:autolink,:xhtml,:tables]

	# convert to html
	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)
	parse_markdown = markdown.render(text)
	# parse_markdown = syntax_highlighter(parse_markdown)

	css_link = ''
	if destiny_dir.to_s.index('/') 
		css_link =  %Q{
				<link href="toc/style/github-bf51422f4bb36427d391e4b75a1daa083c2d840e.css" media="all" rel="stylesheet" type="text/css"/>
				<link href="toc/style/github2-d731afd4f624c99a4b19ad69f3083cd6d02b81d5.css" media="all" rel="stylesheet" type="text/css"/>
				<link href="toc/css/zTreeStyle/zTreeStyle.css" media="all" rel="stylesheet" type="text/css"/>
		}
	else
		css_link =  %Q{
				<link href="toc/style/github-bf51422f4bb36427d391e4b75a1daa083c2d840e.css" media="all" rel="stylesheet" type="text/css"/>
				<link href="toc/style/github2-d731afd4f624c99a4b19ad69f3083cd6d02b81d5.css" media="all" rel="stylesheet" type="text/css"/>
				<link href="toc/css/zTreeStyle/zTreeStyle.css" media="all" rel="stylesheet" type="text/css"/>
		}
	end

  t = %Q{
    <html>
      <head>
			  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>#{ff.gsub('.md','')}</title>
				#{css_link}
			  <style>
				pre {
				    counter-reset: line-numbering;
				    border: solid 1px #d9d9d9;
				    border-radius: 0;
				    background: #fff;
				    padding: 0;
				    line-height: 23px;
				    margin-bottom: 30px;
				    white-space: pre;
				    overflow-x: auto;
				    word-break: inherit;
				    word-wrap: inherit;
				}

				pre a::before {
				  content: counter(line-numbering);
				  counter-increment: line-numbering;
				  padding-right: 1em; /* space after numbers */
				  width: 25px;
				  text-align: right;
				  opacity: 0.7;
				  display: inline-block;
				  color: #aaa;
				  background: #eee;
				  margin-right: 16px;
				  padding: 2px 10px;
				  font-size: 13px;
				  -webkit-touch-callout: none;
				  -webkit-user-select: none;
				  -khtml-user-select: none;
				  -moz-user-select: none;
				  -ms-user-select: none;
				  user-select: none;
				}

				pre a:first-of-type::before {
				  padding-top: 10px;
				}

				pre a:last-of-type::before {
				  padding-bottom: 10px;
				}

				pre a:only-of-type::before {
				  padding: 10px;
				}
		
				.highlight { background-color: #ffffcc } /* RIGHT */
				</style>
      </head>
      <body>
			  <div>
						<div style='width:25%;'>
								<ul id="tree" class="ztree" style='width:100%'>
	
								</ul>
						</div>
		        <div id='readme' style='width:70%;margin-left:20%;'>
		          	<article class='markdown-body'>
		            	#{parse_markdown}
		          	</article>
		        </div>
				</div>
      </body>
    </html>
		<script type="text/javascript" src="toc/js/jquery-1.4.4.min.js"></script>
		<script type="text/javascript" src="toc/js/jquery.ztree.all-3.5.min.js"></script>
		<script type="text/javascript" src="toc/js/ztree_toc.js"></script>
		<SCRIPT type="text/javascript" >
		<!--
		$(document).ready(function(){
			$('#tree').ztree_toc({
				is_auto_number:true,
				documment_selector:'.markdown-body'
			});
		});
		//-->
		</SCRIPT>
  }

	if destiny_dir.to_s.index('/') 
		# p '1build src/' + destiny_dir.to_s.split('/')[1] + '/' + ff.gsub('.md','') + '.html'
		build_dir = 'preview/'
		
		p 'build = ' + dest_dir + '/' + ff.gsub('.md','') + '.html'
		IO.write(dest_dir +  '/'  + ff.gsub('.md','') + '.html',t) # => 10
	else
		# p '2build src/' + ff.gsub('.md','') + '.html'
		build_dir = 'preview/'  
		# write to html file
		IO.write(build_dir + '/' + ff.gsub('.md','') + '.html',t) # => 10
	end
end