latex2hiki 0.1.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.
@@ -0,0 +1,3 @@
1
+ module Latex2hiki
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,222 @@
1
+ #!/usr/local/bin/ruby
2
+ # -*- coding: utf-8 -*-
3
+ require "stringio"
4
+ require 'kconv'
5
+ require "strscan"
6
+ require "uri"
7
+
8
+ class Latex < String
9
+
10
+ def initialize(output)
11
+ @text = NKF.nkf("-w",output)
12
+ @text.gsub!(/\r\n?/,'\n')
13
+ @text.gsub!(/\\%/,'%')
14
+ @text.gsub!("。",".")
15
+ @text.gsub!("、",",")
16
+ end
17
+
18
+ def strip_hiki
19
+ ifHIKI = true
20
+ output = ''
21
+ @text.each_line {|line1|
22
+ case line1
23
+ when /\\ifHIKI/
24
+ ifHIKI = true
25
+ when /\\else/
26
+ ifHIKI = false
27
+ when /\\fi/
28
+ ifHIKI = true
29
+ else
30
+ if ifHIKI==true then
31
+ output << line1
32
+ end
33
+ end
34
+ }
35
+ @text = output
36
+ end
37
+
38
+ def strip_document
39
+ is_document = false
40
+ is_include_document = false
41
+ output = ''
42
+ @text.each_line{|line|
43
+ case line
44
+ when /\\begin\{document\}/
45
+ is_document = true
46
+ is_include_document = true
47
+ when /\\end\{document\}/
48
+ is_document = false
49
+ else
50
+ if is_document==true then
51
+ output << line
52
+ end
53
+ end
54
+ }
55
+ if is_include_document==true then
56
+ @text = output
57
+ end
58
+ end
59
+
60
+ def to_hiki
61
+ roman_num=["","i)","ii)","iii)","iv)","v)","vi)","vii)","viii)","ix)","x)"]
62
+ alpha_num=["","a)","b)","c)","d)","e)","f)","g)","h)","i)","j)"]
63
+ strip_document()
64
+ strip_hiki()
65
+ output=''
66
+ item_number=Array.new(3,1)
67
+ is_enumerate=0
68
+ is_quote_eq, is_quote, is_itemize, is_table = false, false, false, false
69
+ @text.each_line {|nline|
70
+ line=nline.gsub(/\\verb\|(.+?)\|/){|matched|
71
+ $&[6..-2]
72
+ }
73
+
74
+ line.gsub!(/\\fbox\{(.+?)\}/,"[ XXX ]")
75
+ line.gsub!(/\\displaystyle/,'')
76
+
77
+ case line
78
+ when /\\label\{(.*)\}/
79
+
80
+ when /\\section\{(.*)\}/
81
+ output << "\n!#{$1} \n"
82
+ when /\\section\*\{(.*)\}/
83
+ output << "\n!#{$1} \n"
84
+ when /\\subsection\{(.*)\}/
85
+ output << "\n!!#{$1} \n"
86
+ when /\\subsubsection\{(.*)\}/
87
+ output << "\n!!!#{$1} \n"
88
+ when /\\paragraph\{(.*)\}/
89
+ output << "\n!!!!#{$1} \n"
90
+ when /\\item\[(.*)\](.*)/
91
+ output << ":"+$1+":"+$2.chomp+"\n"
92
+ when /\\item(.*)/
93
+ if is_enumerate==1 then
94
+ output << "# "+$1.chomp+"\n"
95
+ elsif is_enumerate==2 then
96
+ output << "# "+$1.chomp+"\n"
97
+ elsif is_enumerate==3 then
98
+ output << "# "+$1.chomp+"\n"
99
+ elsif is_itemize then
100
+ output << "* "+$1+"\n"
101
+ end
102
+
103
+ when /\\begin\{MapleInput\}/
104
+ output << "<<<maple\n"
105
+ when /\\end\{MapleInput\}/
106
+ output << ">>>\n"
107
+ when /\\begin\{MapleError\}/
108
+ output << "<<<maple\n"
109
+ when /\\end\{MapleError\}/
110
+ output << ">>>\n"
111
+
112
+
113
+ when /\\begin\{quote\}/
114
+ is_quote = true
115
+ when /\\end\{quote\}/
116
+ is_quote = false
117
+
118
+ when /\\begin\{description\}/
119
+ when /\\end\{description\}/
120
+ when /\\begin\{verbatim\}/
121
+ output << "<<<\n"
122
+ when /\\end\{verbatim\}/
123
+ output << ">>>\n"
124
+ when /\\begin\{enumerate\}/
125
+ is_enumerate += 1
126
+ when /\\begin\{itemize\}/
127
+ is_itemize = true
128
+ when /\\end\{enumerate\}/
129
+ item_number[is_enumerate]=1
130
+ is_enumerate -= 1
131
+ when /\\end\{itemize\}/
132
+ is_itemize = false
133
+
134
+
135
+
136
+ when /\\begin\{MapleOutput\}/,/\\begin\{MapleOutputGather\}/,/\\begin\{gather\}/ then
137
+ output << "\$\$\n"
138
+ is_quote_eq = true
139
+ when /\\end\{MapleOutput\}/,/\\end\{MapleOutputGather\}/,/\\end\{gather\}/ then
140
+ output << "\$\$\n"
141
+ is_quote_eq = false
142
+ # when /\\notag/ then
143
+ # output << $`+"\n"
144
+ # output <<
145
+
146
+ when /\\MaplePlot\{(.*)\}\{(.*)\}/
147
+ target=File::basename($2,".eps")+".png"
148
+ output << "||{{attach_view(#{target},#{$target_dir})}}||\n"
149
+
150
+ when /\\begin\{tabular\}/
151
+ is_table = true
152
+ when /\\end\{tabular\}/
153
+ is_table = false
154
+ when /\\begin\{table\}/
155
+ when /\\end\{table\}/
156
+ when /\\begin\{center\}/
157
+ when /\\end\{center\}/
158
+ when /\\caption\{(.*)\}/
159
+ # output << "'''"+$1+"'''\n"
160
+ output << "!!!caption:"+$1+"\n"
161
+
162
+ when /\\begin\{equation\*\}/
163
+ is_quote_eq = true
164
+ if is_quote then
165
+ output << "\"\"\$\$\n"
166
+ else
167
+ output << "\n\$\$\n"
168
+ end
169
+ when /\\end\{equation\*\}/
170
+ is_quote_eq = false
171
+ output << "\$\$\n"
172
+ when /\\begin\{equation\}/
173
+ is_quote_eq = true
174
+ if is_quote then
175
+ output << "\"\"\$\$\n"
176
+ else
177
+ output << "\$\$\n"
178
+ end
179
+ when /\\end\{equation\}/
180
+ is_quote_eq = false
181
+ output << "\$\$\n"
182
+ when /\\pagebreak/
183
+ when /\\begin\{(.+)\}/
184
+ if is_quote_eq then
185
+ output << line.lstrip
186
+ end
187
+ when /\\end\{(.+)\}/
188
+ if is_quote_eq then
189
+ output << line.lstrip
190
+ end
191
+ else
192
+ if is_table then
193
+ next if ((line==nil) or (line=~/^\\hline/))
194
+ line.gsub!(/\&/,"||")
195
+ line.gsub!(/\\\\/,"")
196
+ line.gsub!(/\\hline/,"")
197
+ output << "||"+line
198
+
199
+ elsif is_quote then
200
+ if is_quote_eq then
201
+ output << line.lstrip
202
+ else
203
+ output << "\"\""+line
204
+ end
205
+ else
206
+ if is_quote_eq then
207
+ output << line.lstrip
208
+ else
209
+ output << line
210
+ end
211
+ end
212
+ end
213
+ }
214
+ return output
215
+ end
216
+ end
217
+
218
+ if __FILE__ == $0
219
+ $target_dir=File::dirname(ARGV[0])
220
+ # puts NKF.nkf("-e",Latex.new(File.read(ARGV[0])).to_hiki)
221
+ puts NKF.nkf("-w",Latex.new(File.read(ARGV[0])).to_hiki)
222
+ end
@@ -0,0 +1,50 @@
1
+ task :default do
2
+ system 'rake -T'
3
+ end
4
+
5
+ desc "make hiki documents by mk_maple_hiki"
6
+ task :hiki do
7
+ dirs=Dir.glob('./*')
8
+ dirs.each{|directory|
9
+ next unless FileTest.directory?(directory)
10
+ p directory
11
+ FileUtils.cp('./.latex2hiki_rc',directory)
12
+ system "mk_maple_hiki --hiki #{directory}"
13
+ system "mk_maple_hiki --figures #{directory}"
14
+ }
15
+
16
+ end
17
+
18
+ desc "strip multiple braces"
19
+ task :strip do
20
+ lines = File.readlines(ARGV[1])
21
+ text = ""
22
+ lines.each{|line|
23
+ # if md=line.match(/\{\{(.+?)\}\}/) then
24
+ if md=line.match(/\{\{.+?\}\}/) then
25
+ line.gsub!(/\{\{\{(.+?)\}\}\}/){|text|
26
+ "\{#{$1}\}"
27
+ }
28
+ line.gsub!(/\{\{(.+?)\}\}/){|text|
29
+ "\{#{$1}\}"
30
+ }
31
+ end
32
+ text << line
33
+ }
34
+ print text
35
+ end
36
+
37
+ desc "replace multiple word pairs at once commands"
38
+ task :replace do
39
+ lines = File.readlines(ARGV[1])
40
+ text = ""
41
+ change_pair = [["ChartElementTwo","subsection"]]
42
+ lines.each{|line|
43
+ if md=line.match(/\A\\ChartElementTwo/)
44
+ line.gsub!(change_pair[0][0],change_pair[0][1])
45
+ end
46
+ text << line
47
+ }
48
+ print text
49
+ end
50
+
@@ -0,0 +1,178 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'optparse'
3
+ require "latex2hiki/version"
4
+ require "maple/latex2hiki"
5
+ #require "latex2hiki/latex2hiki_new"
6
+ require 'fileutils'
7
+ require 'yaml'
8
+
9
+ module MkMapleHiki
10
+
11
+ class Command
12
+ def self.run(argv=[])
13
+ new(argv).execute
14
+ end
15
+
16
+ def initialize(argv=[])
17
+ @argv = argv
18
+ @scale = 80
19
+ end
20
+
21
+ def execute
22
+ @argv << '--help' if @argv.size==0
23
+ banner = 'For making maple hiki'
24
+ command_parser = OptionParser.new(banner) do |opt|
25
+ opt.on('-v', '--version','show program Version.') { |v|
26
+ opt.version = Latex2hiki::VERSION
27
+ puts opt.ver
28
+ }
29
+ opt.on('--init [NAME]','initialize NAME directory.') { |name|
30
+ name = name || './'
31
+ init(name)
32
+ exit
33
+ }
34
+ opt.on('--figures [NAME]','gather and convert figures in NAME dir.') { |name|
35
+ name = name || './'
36
+ gather_figures(name)
37
+ convert_figures(name)
38
+ exit
39
+ }
40
+ opt.on('--scale [VAL]%','set scale for convert figures.') { |val|
41
+ @scale = val.to_i
42
+ }
43
+ opt.on('--level [VAL]','set level for head start level .') { |val|
44
+ @level = val.to_i
45
+ }
46
+ opt.on('--hiki [NAME]','make hiki contents from NAME directory.') { |name|
47
+ name = name || './'
48
+ @src=read_src(name)
49
+ make_hiki(name)
50
+ exit
51
+ }
52
+ end
53
+ command_parser.parse!(@argv)
54
+ puts NKF.nkf("-w",Latex.new(File.read(ARGV[0])).to_hiki)
55
+ exit
56
+ end
57
+
58
+ def read_src(name)
59
+ file=File.read(File.join(name,'.latex2hiki_rc'))
60
+ YAML.load(file)
61
+ end
62
+
63
+ def make_hiki(name)
64
+ if name=='./'
65
+ toc_file = './'+File.basename(Dir.pwd())+'.tex'
66
+ hiki_file = File.join(@src[:local_site],'text',File.basename(Dir.pwd))
67
+ else
68
+ toc_file = File.join(name,"#{File.basename(name)}.tex")
69
+ hiki_file = File.join(@src[:local_site],'text',File.basename(name))
70
+ end
71
+
72
+ if File.exists?(toc_file) then
73
+ toc=toc_file_base_proc(toc_file)
74
+ else
75
+ # dir_base_proc
76
+ end
77
+
78
+ File.write(hiki_file, toc)
79
+ FileUtils.chmod(0666,hiki_file,:verbose=>true)
80
+ end
81
+
82
+ def toc_file_base_proc(toc_file)
83
+ lines = File.readlines(toc_file)
84
+ parser=[]
85
+ str_started=false
86
+ lines.each{|line|
87
+ elements=line.match(/^\\(.+){(.+)}/u)
88
+ next unless elements
89
+ if !str_started
90
+ if elements[2]!='document'
91
+ next
92
+ else
93
+ str_started=!str_started
94
+ end
95
+ end
96
+ parser << [elements[1],elements[2]]
97
+ }
98
+ p parser
99
+ converts = @src[:convert_specific_def]
100
+ @level = @level || @src[:level]
101
+ text="{{toc}}\n"
102
+ parser.each{|elements|
103
+ tt=""
104
+ case elements[0]
105
+ when 'chapter'
106
+ next if @level<0
107
+ @level.times{ tt << '!' }
108
+ p [tt,elements[1]]
109
+ text << "\n#{tt}#{elements[1]}\n"
110
+ when 'section'
111
+ next if (@level+1)<0
112
+ (@level+1).times{ tt << '!' }
113
+ p [tt,elements[1]]
114
+ text << "\n#{tt}#{elements[1]}\n"
115
+ when 'subsection'#,'ChartElement','ChartElementTwo'
116
+ (@level+2).times{ tt << '!' }
117
+ text << "\n#{tt}#{elements[1]}\n"
118
+ when 'input'
119
+ p elements[1]
120
+ latex_txt= File.read(File.join(File.dirname(toc_file),elements[1]))
121
+ hiki_txt = NKF.nkf("-w",Latex.new(latex_txt).to_hiki)
122
+ text << "\n#{hiki_txt}\n"
123
+ end
124
+ }
125
+ return text
126
+ end
127
+
128
+ def gather_figures(name)
129
+ @src=read_src(name)
130
+ extension = @src[:fig_extension] || '.eps'
131
+ p file_names =File.join(name,'**',"\*#{extension}")
132
+ files = Dir.glob(file_names)
133
+ target = File.join(name,'figures')
134
+ files.each{|file|
135
+ next if File.dirname(file)==target
136
+ FileUtils.cp(file,target,:verbose=>true)
137
+ }
138
+ end
139
+
140
+ def convert_figures(name)
141
+ @src=read_src(name)
142
+ extension = @src[:fig_extension] || '.eps'
143
+ p file_dir =File.join(name,'figures')
144
+ files = Dir.entries(file_dir)
145
+ p target_dir = File.join(@src[:local_site],'cache','attach',File.basename(name))
146
+ FileUtils.mkdir_p(target_dir,:verbose=>true)
147
+ files.each{|file|
148
+ next if File.extname(file) != extension
149
+ p source = File.join(file_dir,file)
150
+ p name = File.basename(file,extension)
151
+ p target = File.join(target_dir,name+'.png')
152
+ p command = "convert #{source} -resize #{@scale}\% #{target}"
153
+ system command
154
+ }
155
+ end
156
+
157
+ def init(name)
158
+ FileUtils.mkdir_p(name,:verbose=>true)
159
+ FileUtils.mkdir_p(File.join(name,'figures'),:verbose=>true)
160
+
161
+ @src = {:fig_extension => '.eps',:level => 0,
162
+ :local_site => '/Users/bob/Sites/new_ist_data/maple_hiki_data'}
163
+ unless File.exists?(File.join(name,'.latex2hiki_rc'))
164
+ file=File.open(File.join(name,'.latex2hiki_rc'),'w')
165
+ YAML.dump(@src,file)
166
+ file.close
167
+ end
168
+
169
+ if File.exists?(File.join(name,'Rakefile')) then
170
+ print "Remove Rakefile from targetdir."
171
+ else
172
+ @source_path = File.expand_path('..', __FILE__)
173
+ FileUtils.cp(File.join(@source_path,'maple','new_rakefile'),
174
+ File.join(name,'Rakefile'),:verbose=>true)
175
+ end
176
+ end
177
+ end
178
+ end