Class: MathJaxYard::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/mathjax-yard.rb,
lib/mathjax-yard/init.rb

Constant Summary

MJX_FILE_EXT =
'*.mjx.md'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Command

Returns a new instance of Command



15
16
17
18
19
# File 'lib/mathjax-yard.rb', line 15

def initialize(argv=[])
  @argv = argv
  @eq_data=Hash.new
  @eq_number =0
end

Class Method Details

.run(argv = []) ⇒ Object



11
12
13
# File 'lib/mathjax-yard.rb', line 11

def self.run(argv=[])
  new(argv).execute
end

Instance Method Details

#convert(directory) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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

#executeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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_dirObject



18
19
20
21
22
23
24
25
# 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_yardObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mathjax-yard.rb', line 61

def init_mathjax_yard
  text = <<EOS

Following hand operations are necessary.

Add ./yardopts

-t mathjax -p templates

Add 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_yardObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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) ⇒ Object



125
126
127
128
129
130
# 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) ⇒ Object



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
# 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) ⇒ Object



27
28
29
30
31
32
# 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_operationObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object



102
103
104
105
106
107
# 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) ⇒ Object



172
173
174
175
176
177
# 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) ⇒ Object



132
133
134
135
# 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