# -*- coding: utf-8 -*- require 'systemu' require 'tempfile' # -*- coding: utf-8 -*- begin $hiki_dir = File.readlines('./.hikirc')[0].chomp rescue => e puts e exit end dirnames=Dir.pwd.split('/') $basename = (dirnames[-1]=='hikis')? dirnames[-2] : dirnames[-1] $latex_dir= 'latex_dir' $section_layer = {} task :default do system 'rake -T' end desc "Githubのdirをsafariでopen" task :github do status, stdout, stderr = systemu %q( git remote -v |grep origin ) p github_dir=stdout.match(/(.+):(.+) \(push\)/)[2] system "open https://github.com/#{github_dir}" end desc "hikiシステムにあるゴミファイルを掃除する" task :reset_hiki do status, stdout, stderr = systemu "hiki -l #{$basename}" print stdout files=[] stdout.split("\n").each{|line| files << line.split(/\s+/)[-1] } p r_files = files.reverse[0..-4] r_files.each{|file| print "remove #{file}[ynqlA]?" input=STDIN.gets.chomp case input when 'y' p command="hiki --remove #{file}" system command when 'n' when 'l' p command="hiki -l #{$basename}" system command when 'q' exit when 'A' print "\nAre you sure?[Yn]" input2 = STDIN.gets.chomp case input2 when 'Y' r_files.each{|file| system "hiki --remove #{file}"} p target = File.join($hiki_dir,'cache','attach',$basename) exit when 'n' exit end end } end desc "latex_dirのゴミを掃除" task :reset_latex_dir do system "mv latex_dir/head.tex ." system "rm -rf latex_dir" system "mkdir latex_dir" system "mv head.tex latex_dir/" system "rake latex_all" end desc "toc.hikiの作成" task :mk_toc do p target = $basename+'.toc' cont="" File.readlines(File.join('latex_dir',target)).each{|line| if m=line.match(/\\contentsline \{(.+)\}\{\\numberline \{([\d|\.]+)\}(.+)\}\{\d+\}/) layer,sec_no,title=m[1],m[2],m[3] d = case m[1] when 'section' 1 when 'subsection' 2 when 'subsubsection' 3 else 4 end layer = "!"*d title.gsub!('\_','_') cont << "#{layer}#{sec_no}:#{title}\n" end } print cont File.open("toc.hiki",'w'){|file| file.print cont} end desc "FILE.hikiあるいはhikiファイルすべてを最新状態に更新" task :touch do if file=ARGV[1] p target = $basename+"_"+file.split('.')[0] system "hiki -u #{target}" else p target = File.join($hiki_dir,'cache','attach',$basename) system "touch #{target}/*" Dir.entries('.').each{|file| file_split=file.split(".") if file_split[1]=='hiki' next if file_split[0]==$basename p target = $basename+"_"+file_split[0] system "hiki -u #{target}" end } end end desc "FILE.hikiあるいはhikiファイルをedtiorで開く" task :open do if file=ARGV[1] system "open -a mi #{file}" else Dir.entries('.').each{|file| file_split=file.split(".") if file_split[1]=='hiki' p target = file_split[0] system "open -a mi #{target}" end } end end desc "FILE1をlatexに変換" task :latex => [:latex_base] do exit end desc "FILE1をwrap formatでlatexに変換" task :latex_wrap => [:latex_base, :change_wrap] do exit end def latex_loop(entries,opts={}) main_file="" entries.each{|file| options={:latex_all=>true} next unless file.split('.')[1]=='hiki' next if file=='toc.hiki' next if file.include?('.hikirc') f_name =File.basename(file,'.hiki') if opts[:main] if f_name==$basename options[:latex]="--listings --head latex_dir/head.tex -p " options[:main]=true main_file=f_name convert_to_latex(file, options) end else if f_name!=$basename p level = $section_layer[f_name] || 1 options[:latex]="-l #{level} --listings -b " convert_to_latex(file, options) end end } return main_file end def check_section_layers(lines) p reg_exp = Regexp.new("#{$basename}_(.+)") lines.each{|line| p line if m=line.match(/(!+)\[\[(.+)\]\]/) p count = m[1].count('!') p file_name=m[2].match(reg_exp)[1] $section_layer[file_name]=count end } p $section_layer end def check_bounding_box # $bounding_box="0 0 442 432" $bounding_box=" 0 0 737 453" end desc "すべてのhikiファイルをlatex変換" task :latex_all do p entries=Dir.entries('.') check_bounding_box main_file = latex_loop(entries,opts={:main=>true}) latex_loop(entries) cont="" p reg_exp = Regexp.new("#{$basename}_(.+)") toc = false File.readlines(File.join($latex_dir,"#{main_file}.tex")).each{|line| line, toc = "", true if line.match(/\\tableofcontents/) and toc == true #only one line ="\\usepackage{listings,jlisting}" if line.match(/\\usepackage{listings}/) if line.match(/section{(.+)}/) if m=line.match(/\\verb\|(.+)\((.+)\)\|/) p m p m_t=m[2].match(reg_exp)[1] line = "\\include{#{m_t}}\n" elsif m=line.match(/section{\\verb\|(.+)\|}/) p m p m_t=m[1].match(reg_exp)[1] line = "\\include{#{m_t}}\n" else # line = "" end end cont << line } File.open(File.join($latex_dir,"#{main_file}.tex"),'w'){|file| file.print cont} system "open latex_dir/#{main_file}.tex" exit end task :latex_base do Dir.mkdir($latex_dir) unless Dir.exist?($latex_dir) convert_to_latex(ARGV[1]) system("open #{$save_name}") end def convert_to_latex(file_name, options={}) p file_name = file_name.include?('.hiki')? file_name : file_name+'.hiki' p tex_name = File.basename(file_name,'.hiki')+'.tex' p $save_name = ARGV[2] || File.join($latex_dir,tex_name) pre_name = File.join($latex_dir,'pre.tex') p pre_command = File.exist?(pre_name) ? '--pre '+pre_name : nil lines = File.readlines(file_name) check_section_layers(lines) if options[:main] cont = "" lines.each_with_index{|line,i| if m=line.match(/\{\{attach_view\((.*),(.*)\)\}\}/) p line="\{\{attach_view\(#{m[1]}\)\}\}\n" elsif options[:latex_all] and i<5 line="" if line.match(/^!title:|^!author:|^!date:/) end cont << line } tf1,tf2='tmp1.txt','tmp2.txt' File.open(tf1,'w'){|file| file.print cont} system "hiki2latex #{pre_command} #{options[:latex]} #{tf1} > #{tf2}" lines = File.readlines(tf2) system "rm #{tf1} #{tf2}" cont = "" lines.each{|line| if m=line.match(/\\includegraphics\[width=6cm\]\{(.+)\}/) p line="\\includegraphics\[width=10cm,bb=#{$bounding_box}\]\{../figs/#{m[1]}\}\n" cont << line else cont << line end } File.open($save_name,'w'){|file| file.print cont } end task :change_wrap do lines = File.readlines($save_name) cont = "" lines.each{|line| if line.include?('\begin{figure}[htbp]\begin{center}') p line cont << '\begin{wrapfigure}{r}{8cm}'+"\n" cont << '\vspace{-2\baselineskip}'+"\n" cont << '\begin{center}'+"\n" elsif line.include?('\label{default}\end{center}\end{figure}') p line cont << '\end{center}'+"\n" cont << '\vspace{2\baselineskip}'+"\n" cont << '\end{wrapfigure}'+"\n" else cont << line end } File.open($save_name,'w'){|file| file.print cont } system("open #{$save_name}") exit end desc "increment fig NUBERS in FILE" task :increment do number=ARGV[1] file = ARGV[2] dir = ARGV[3] || nil lines = File.readlines(file) cont = "" lines.each{|line| if m=line.match(/\{\{attach_view\((\w+|_).(\d+).jpeg\)\}\}/) new_num=sprintf("%03d",m[2].to_i+number.to_i) line="\{\{attach_view\(#{m[1]}.#{new_num}.jpeg,#{dir}\)\}\}\n" cont << line else cont << line end } print cont exit end desc "numbering figs from the NUBER in FILE" task :number do number=ARGV[1].to_i file = ARGV[2] dir = ARGV[3] || nil lines = File.readlines(file) cont = "" lines.each{|line| if m=line.match(/\{\{attach_view\((\w+|_).(\d+).jpeg,(\w+)\)\}\}/) new_num=sprintf("%03d",number) line="\{\{attach_view\(#{m[1]}.#{new_num}.jpeg,#{m[3]}\)\}\}\n" cont << line number += 1 else cont << line end } print cont exit end desc "convert fig size SCALE TARGET_DIR" task :convert do scale = ARGV[1] target_dir=ARGV[2] Dir.entries(target_dir)[2..-1].each{|file| p file source = File.join(target_dir,file) target = File.join('figs',file) p command = "convert #{source} -resize #{scale}\% #{target}" system command } exit end desc "hikiの同期" task :sync => [:check_previous,:sync0] desc "hikiの強制同期" task :force_sync => [:sync0] task :sync0 do entries=Dir.entries('.') entries[2..-1].each{|source| #cp *.hiki next unless source.split('.')[1]=='hiki' next if source.include?('.hikirc') p base = source.split('.')[0] name = (base==$basename)? base : $basename+'_'+base p target = File.join($hiki_dir,'text',name) FileUtils.cp(source,target,:verbose=>true) } p entries=Dir.entries('./figs') #cp files in figs p target = File.join($hiki_dir,'cache','attach',$basename) FileUtils.mkdir_p(target,:verbose=>true) unless File.exists?(target) entries[2..-1].each{|file| p source = File.join('./figs',file) FileUtils.cp(source,target,:verbose=>true) } File.open('./.hikirc','w'){|file| status, stdout, stderr =systemu "hiki -l #{$basename}*" file.print($hiki_dir+"\n") stdout.split("\n")[3..-1].each{|line| file.print line+"\n" } } exit end desc "For hiki Errno::ENOENT, Errno::EACCES" task :chenv do p user = ENV['USER'] system("sudo chmod -R a+w #{$hiki_dir}") # system("sudo chown -R #{user} #{$hiki_dir}") end desc "self copy to hikiutils template directory" task :self_copy do p cp_files=[[File.join(Dir.pwd,'Rakefile'),'Rakefile_hiki_sync'], [File.join(ENV['HOME'],'.my_help','hiki_help.yml'),'hiki_help.yml']] cp_files.each{|files| p source = files[0] p target = File.join('/Users/bob/Github/hikiutils/lib/templates/',files[1]) FileUtils.cp(source,target,:verbose=>true) } end task :check_previous do current={} print "current hiki dir\n" status, stdout, stderr = systemu("hiki -l #{$basename}*") stdout.split("\n")[3..-1].each{|line| p line file= line.split(/\s+/)[-1] current[file]=line } previous={} print "previous sync\n" File.readlines('.hikirc')[1..-1].each{|line| p line.chomp file= line.split(/\s+/)[-1] previous[file]=line.chomp } changed = [] current.each_pair{|key,val| if !previous.include?(key) or previous[key]!=val changed << val+"\n" end } if changed.size != 0 print("hiki dirで変更が発生しています.\n") changed.each{|line| print line+"\n"} exit end end