class MathJaxYard::Command

Constants

MJX_FILE_EXT

Public Class Methods

new(argv=[]) click to toggle source
# File lib/mathjax-yard.rb, line 15
def initialize(argv=[])
  @argv = argv
  @eq_data=Hash.new
  @eq_number =0
end
run(argv=[]) click to toggle source
# File lib/mathjax-yard.rb, line 11
def self.run(argv=[])
  new(argv).execute
end

Public Instance Methods

convert(directory) click to toggle source
# File lib/mathjax-yard.rb, line 109
def convert(directory)
  files = Dir.glob(File.join(directory,'*.md'))
  files.each{|base_file|
    m_file = mk_mjx_file_name(base_file)
    @eq_data[m_file] = Hash.new
    lines = File.readlines(base_file)
    output = mk_tags(lines,m_file)
    if @eq_data[m_file].size ==0
      @eq_data.delete(m_file)
    else
      write_output_on_target(base_file,output)
    end
  }
  File.write("mathjax.yml",YAML.dump(@eq_data))
end
execute() click to toggle source
# File lib/mathjax-yard.rb, line 21
    def execute
      command_parser = OptionParser.new do |opt|
        opt.on('-v', '--version','show program Version.') { |v|
          opt.version = Yardmath::VERSION
          puts opt.ver
          exit
        }
        opt.on('-r', '--revert','revert mjx file to orig file.') {
          directory = @argv[0]==nil ? 'lib/../*' : @argv[0]
          revert(directory)
          exit
        }
        opt.on('-p', '--pre','pre operation.') {
          directory = @argv[0]==nil ? 'lib/../*' : @argv[0]
          convert(directory)
          exit
        }
        opt.on('--post','post operation.') {
          post_operation
          directory = @argv[0]==nil ? 'lib/../*' : @argv[0]
          revert(directory)
          exit
        }
        opt.on('-i', '--init','init for mathjax on yard layout.') {
          init_yard()
          init_mathjax_yard()
          exit
        }
      end
      command_parser.banner = <<EOF 
Usage: mathjax-yard [options] [DIRECTORY]
with no extention: mathjax-yard -p lib/../*/*.md

EOF
      command_parser.parse!(@argv)
      directory = @argv[0]==nil ? 'lib/../*' : @argv[0]
      convert(directory)
      exit
    end
get_yard_layout_dir() click to toggle source
# File lib/mathjax-yard/init.rb, line 18
def get_yard_layout_dir()
  status, stdout, stderr  = systemu('gem env | grep INSTALLATION ')
  p inst_dir= stdout.split("\n")[0].split(': ')[1]
  status, stdout, stderr  = systemu('yard -v')
  p yard_num= stdout.split(' ')[0]+'-'+stdout.split(' ')[1]
  p target_dir = File.join(inst_dir,'gems',yard_num,"templates")
  return target_dir
end
init_mathjax_yard() click to toggle source
# File lib/mathjax-yard.rb, line 61
    def init_mathjax_yard
      text = <<EOS

Following hand operations are necessary.

Add on ./yardopts

-t mathjax -p templates

Add on Rakefile

desc "arrange yard target by mathjax-yard"
task :pre_math do
  system('mathjax-yard')
end

desc "make yard documents with yardmath"
task :myard => [:hiki2md, :pre_math,:yard] do
  system('mathjax-yard --post')
end

EOS
      puts text
    end
init_yard() click to toggle source
# File lib/mathjax-yard/init.rb, line 3
def init_yard()
  target_dir=get_yard_layout_dir()
  FileUtils.cd(target_dir){
    tmp_dir='mathjax' # 'math2'
    full_path="#{target_dir}/#{tmp_dir}/layout/html/layout.erb"
    if File.exist?("#{tmp_dir}/layout/html/layout.erb")
      print("file #{full_path} exists.\nDelete them first.\n")
      return
    end
    FileUtils.cp_r('default',tmp_dir)
    modify_layout("#{tmp_dir}/layout/html/layout.erb")
    modify_layout("#{tmp_dir}/onefile/html/layout.erb")
  }
end
mk_mjx_file_name(file) click to toggle source
# File lib/mathjax-yard.rb, line 125
def mk_mjx_file_name(file)
  dir=File.dirname(file)
  File.basename(file).scan(/(.*).md/)
  basename=$1
  return File.join(dir,"#{basename}.mjx.md")
end
mk_tags(lines,file_name) click to toggle source
# File lib/mathjax-yard.rb, line 137
def mk_tags(lines,file_name)
  @in_eq_block = false
  output,stored_eq="",""
  lines.each{|line|
    if @in_eq_block #inside in eq block
      if line =~/^\$\$/ #closing
        stored_eq << "$"
        output << store_eq_data("$#{stored_eq}$",file_name)
        stored_eq=""
        @in_eq_block = !@in_eq_block
      else #normal op. in eq block
        p stored_eq << line
      end
    else #outside eq block
      case line
      when /\\\$(.*?)\\\$/
        p line
        output << line # tryed to change $$ but failed.
      when /\$(.+?)\$/
        p line
        line.gsub!(/\$(.+?)\$/){|equation|
          store_eq_data(equation,file_name)
        }
        output << line
      when /^\$\$/ # opening in eq block
        @in_eq_block = !@in_eq_block
        stored_eq << "$"
      else  #normal op (no eq)
        output << line
      end
    end
  }
  return output
end
modify_layout(file_name) click to toggle source
# File lib/mathjax-yard/init.rb, line 27
def modify_layout(file_name)
  p file_name
  src=File.read(file_name)
  src.gsub!(ORIGINAL,MATH_SCRIPT+ORIGINAL)
  File.write(file_name,src)
end
post_operation() click to toggle source
# File lib/mathjax-yard.rb, line 86
def post_operation
  src = File.read("./mathjax.yml")
  p data = YAML.load(src)
  data.each_pair{|file, tags|
    File.basename(file).scan(/(.+)\.md/)
    p basename = $1
    target = "./doc/file.#{basename}.html"
    src = File.read(target)
    tags.each_pair{|tag,eq|
      src.gsub!(tag){eq}  # fail (tag,eq) at '/////n'
    }
    File.write(target,src)
  }
end
revert(directory) click to toggle source
# File lib/mathjax-yard.rb, line 102
def revert(directory)
  files = Dir.glob(File.join(directory,MJX_FILE_EXT))
  files.each{|m_file|
    FileUtils.rm(m_file)
  }
end
store_eq_data(equation,file_name) click to toggle source
# File lib/mathjax-yard.rb, line 172
def store_eq_data(equation,file_name)
  @eq_number+=1
  tag="$MATHJAX#{@eq_number}$"
  @eq_data[file_name][tag] = equation
  return tag
end
write_output_on_target(file,output) click to toggle source
# File lib/mathjax-yard.rb, line 132
def write_output_on_target(file,output)
  m_file = mk_mjx_file_name(file)
  File.write(m_file,output)
end