Class: Hiki2latex::Command

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

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/hiki2latex.rb', line 15

def initialize(argv=[])
  @argv = argv
  @pre=@head=@post=nil
  @listings=false
end

Class Method Details

.run(argv = []) ⇒ Object



11
12
13
# File 'lib/hiki2latex.rb', line 11

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

Instance Method Details

#bare_doc(file) ⇒ Object



60
61
62
63
# File 'lib/hiki2latex.rb', line 60

def bare_doc(file)
  bare_doc = HikiDoc.to_latex(File.read(file),{:level=>@level,:listings=>@listings})
  puts kill_head_tableofcontents(bare_doc)
end

#executeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hiki2latex.rb', line 21

def execute
  @argv << '--help' if @argv.size==0
  command_parser = OptionParser.new do |opt|
    opt.on('-v', '--version','show program Version.') { |v|
      opt.version = Hiki2latex::VERSION
      puts opt.ver
    }
    opt.on('-l VALUE','--level','set Level for output section.'){|level| @level=level.to_i}
    opt.on('-p FILE', '--plain','make Plain latex document.') { |file| plain_doc(file) }
    opt.on('-b FILE', '--bare','make Bare latex document.') { |file| bare_doc(file) }
    opt.on('--head FILE', 'put headers of maketitle file.') { |file| @head=file }
    opt.on('--pre FILE', 'put preamble file.') { |file| @pre=file }
    opt.on('--post FILE', 'put post file.') { |file| @post=file }
    opt.on('--listings', 'use listings.sty for preformat with style.') {@listings=true }
  end
  command_parser.banner = "Usage: hiki2latex [options] FILE"
  command_parser.parse!(@argv)
  plain_doc(@argv[0]) if @argv[0]!=nil
  exit
end

#kill_head_tableofcontents(text) ⇒ Object



65
66
67
68
# File 'lib/hiki2latex.rb', line 65

def kill_head_tableofcontents(text)
  text.gsub!(/^\\tableofcontents/,'')
  return text
end

#listings_preambleObject



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
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hiki2latex.rb', line 100

def listings_preamble
  str = <<"EOS"
\\documentclass[12pt,a4paper]{jsarticle}
\\usepackage[dvipdfmx]{graphicx}
\\usepackage[dvipdfmx]{color}
\\usepackage{listings}
% to use japanese correctly, install jlistings.
\\lstset{
  basicstyle={\\ttfamily},
  identifierstyle={},
  commentstyle={\\color{red}},
  keywordstyle={\\bfseries\\color{cyan}},
  ndkeywordstyle={},
  stringstyle={\\color{blue}},
  frame={tb},
  breaklines=true,
  numbers=left,
  numberstyle={},
  stepnumber=1,
  numbersep=1zw,
  xrightmargin=0zw,
  xleftmargin=3zw,
  lineskip=0.5ex
}
\\lstdefinestyle{customCsh}{
  language={csh},
  numbers=none,
}
\\lstdefinestyle{customRuby}{
  language={ruby},
  numbers=left,
}
\\lstdefinestyle{customTex}{
  language={tex},
  numbers=none,
}
\\lstdefinestyle{customJava}{
  language={java},
  numbers=left,
}
EOS
end

#mod_abstract(text) ⇒ Object

convert section to abstract in the plain text



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hiki2latex.rb', line 71

def mod_abstract(text)
#      return text
  abstract,section = [],[]
  content = ""
  text.split("\n").each do |line|
    case line
    when /^\\section(.+)/
      section.push $1
    end
#        p section[-1]
    case section[-1]
    when /\{abstract\}/, /\{【abstract】\}/
      abstract << line+"\n"
    when /\{概要\}/, /\{【概要】\}/
      abstract << line+"\n"
    else
      content << line+"\n"
    end
  end
#      p abstract
  if abstract.size>1 then
    abstract.delete_at(0)
    content.gsub!(/\\tableofcontents/){|text|
      tt="\n\\abstract\{\n#{abstract.join}\}\n\\tableofcontents"
    }
  end
  return content
end

#plain_doc(file) ⇒ Object

pre, post, listingsなどの拡張を処理



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hiki2latex.rb', line 43

def plain_doc(file)
  if @listings==true then
    puts listings_preamble
  elsif @pre==nil then
    puts "\\documentclass[12pt,a4paper]{jsarticle}"
    puts "\\usepackage[dvipdfmx]{graphicx}"
  else
    puts File.read(@pre)
  end
  puts "\\begin{document}"
  puts File.read(@head) if @head!=nil
  plain_tex = HikiDoc.to_latex(File.read(file),{:listings=>@listings})
  puts mod_abstract(plain_tex)
  puts File.read(@post) if @post!=nil
  puts "\\end{document}"
end