class PseudoHiki::OptionManager
Constants
- BOM
- ENCODING_REGEXP
- ENCODING_TO_CHARSET
- FILE_HEADER_PAT
- Formatter
- GFMFormat
- MDFormat
- PlainVerboseFormat
- VERSIONS
Attributes
default_title[RW]
input_file_basename[R]
need_output_file[RW]
Public Class Methods
default_options()
click to toggle source
# File lib/pseudohiki/converter.rb, line 285 def self.default_options @default_options.dup end
new(options=nil)
click to toggle source
# File lib/pseudohiki/converter.rb, line 289 def initialize(options=nil) @options = options || self.class.default_options @written_option_pat = {} @options.keys.each do |opt| @written_option_pat[opt] = /^\/\/#{opt}:\s*(.*)$/ end end
remove_bom(input=ARGF)
click to toggle source
# File lib/pseudohiki/converter.rb, line 279 def self.remove_bom(input=ARGF) return if input == ARGF and input.filename == "-" bom = input.read(3) input.rewind unless BOM == bom end
Public Instance Methods
[](key)
click to toggle source
# File lib/pseudohiki/converter.rb, line 297 def [](key) @options[key] end
[]=(key, value)
click to toggle source
# File lib/pseudohiki/converter.rb, line 301 def[]=(key, value) @options[key] = value end
assign_opt_value(opt, key, short, long, description)
click to toggle source
# File lib/pseudohiki/converter.rb, line 381 def assign_opt_value(opt, key, short, long, description) opt.on(short, long, description) {|val| self[key] = val } end
base()
click to toggle source
# File lib/pseudohiki/converter.rb, line 325 def base base_dir = self[:base] if base_dir and not /[\/\\]\.*$/o.match? base_dir base_dir = File.join(base_dir, ".") base_dir = "file:///" + base_dir if not /^\./o.match? base_dir and win32? end base_dir end
charset()
click to toggle source
# File lib/pseudohiki/converter.rb, line 321 def charset ENCODING_TO_CHARSET[self[:encoding]] end
check_argv()
click to toggle source
# File lib/pseudohiki/converter.rb, line 485 def check_argv case ARGV.length when 0 if @need_output_file and not self[:output] raise "You must specify a file name for output" end when 1 read_input_filename(ARGV[0]) end end
create_html_template_with_current_options()
click to toggle source
# File lib/pseudohiki/converter.rb, line 515 def create_html_template_with_current_options return [] unless html_template html = html_template.new html.charset = charset html.language = self[:lang] html.default_css = self[:css] if self[:css] html.base = base if self[:base] html.title = title html end
formatter()
click to toggle source
# File lib/pseudohiki/converter.rb, line 317 def formatter self[:html_version].formatter end
html_template()
click to toggle source
# File lib/pseudohiki/converter.rb, line 313 def html_template self[:html_version].template end
open_output() { |f| ... }
click to toggle source
# File lib/pseudohiki/converter.rb, line 541 def open_output if output_filename open(output_filename, "w") {|f| yield f } else yield STDOUT end end
output_filename()
click to toggle source
# File lib/pseudohiki/converter.rb, line 531 def output_filename return nil unless @need_output_file if self[:output] File.expand_path(self[:output]) else ext = self[:html_version].ext File.join(@input_file_dir, @input_file_basename + ext) end end
parse_command_line_options() { |opt| ... }
click to toggle source
# File lib/pseudohiki/converter.rb, line 496 def parse_command_line_options opt = setup_command_line_options yield opt if block_given? opt.parse! check_argv @default_title = @input_file_basename end
parse_opt_setup_ruby_encoding(opt)
click to toggle source
# File lib/pseudohiki/converter.rb, line 373 def parse_opt_setup_ruby_encoding(opt) opt.on("-E [ex[:in]]", "--encoding [=ex[:in]]", "Specify the default external and internal character encodings \ (same as the option of MRI)") do |given_opt| setup_ruby_encoding(given_opt) end end
read_input_filename(filename)
click to toggle source
# File lib/pseudohiki/converter.rb, line 526 def read_input_filename(filename) @input_file_dir, @input_file_name = File.split(File.expand_path(filename)) @input_file_basename = File.basename(@input_file_name, ".*") end
read_template_file()
click to toggle source
# File lib/pseudohiki/converter.rb, line 338 def read_template_file File.read(File.expand_path(self[:template]), :encoding => charset) end
set_html_encoding(given_opt)
click to toggle source
# File lib/pseudohiki/converter.rb, line 354 def set_html_encoding(given_opt) if ENCODING_REGEXP.values.include? given_opt self[:encoding] = given_opt else ENCODING_REGEXP.each do |pat, encoding| self[:encoding] = encoding if pat.match? given_opt end STDERR.puts "\"#{self[:encoding]}\" is chosen as an encoding system, \ instead of \"#{given_opt}\"." end end
set_html_version(version)
click to toggle source
# File lib/pseudohiki/converter.rb, line 342 def set_html_version(version) VERSIONS.each do |v| if v.version == version return self[:html_version] = v else self[:html_version] = v if v.opt_pat.match? version end end STDERR.puts "\"#{version}\" is an invalid option for --format-version. \ \"#{self[:html_version].version}\" is chosen instead." end
set_options_from_input_file(input_lines)
click to toggle source
# File lib/pseudohiki/converter.rb, line 504 def set_options_from_input_file(input_lines) input_lines.each do |line| break unless FILE_HEADER_PAT.match? line line = line.chomp @options.keys.each do |opt| next if self[opt] and self[:force] self[opt] = $1 if @written_option_pat[opt] =~ line end end end
setup_command_line_options()
click to toggle source
# File lib/pseudohiki/converter.rb, line 385 def setup_command_line_options OptionParser.new("USAGE: #{File.basename($0)} [OPTION]... [FILE]... Convert texts written in a Hiki-like notation into another format.") do |opt| opt.version = PseudoHiki::VERSION parse_opt_setup_ruby_encoding(opt) opt.on("-f [format_version]", "--format-version [=format_version]", "Choose a formart for the output. Available options: \ html4, xhtml1, html5, plain, plain_verbose, markdown or gfm \ (default: #{self[:html_version].version})") do |version| set_html_version(version) end opt.on("-l [lang]", "--lang [=lang]", "Set the value of charset attributes \ (default: #{self[:lang]})") do |lang| self[:lang] = lang if value_given?(lang) end opt.on("-e [encoding]", "--format-encoding [=encoding]", "Available options: utf8, euc-jp, sjis, latin1 \ (default: #{self[:encoding]})") do |given_opt| set_html_encoding(given_opt) end # use '-w' to avoid the conflict with the short option for '[-t]emplate' opt.on("-w [(window) title]", "--title [=title]", "Set the value of the <title> element \ (default: the basename of the input file)") do |title| self[:title] = title if value_given?(title) end assign_opt_value(opt, :css, "-c [css]", "--css [=css]", "Set the path to a css file to be used \ (default: #{self[:css]})") assign_opt_value(opt, :embed_css, "-C [path_to_css_file]", "--embed-css [=path_to_css_file]", "Set the path to a css file to embed \ (default: not to embed)") opt.on("-b [base]", "--base [=base]", "Specify the value of href attribute of the <base> element \ (default: not specified)") do |base_dir| self[:base] = base_dir if value_given?(base_dir) end opt.on("-t [template]", "--template [=template]", "Specify a template file in eruby format with \"<%= body %>\" \ inside (default: not specified)") do |template| self[:template] = template if value_given?(template) end opt.on("-o [output]", "--output [=output]", "Output to the specified file. If no file is given, \ \"[input_file_basename].html\" will be used.(default: STDOUT)") do |output| self[:output] = File.expand_path(output) if value_given?(output) @need_output_file = true end assign_opt_value(opt, :force, "-F", "--force", "Force to apply command line options. \ (default: false)") assign_opt_value(opt, :toc, "-m [contents-title]", "--table-of-contents [=contents-title]", "Include the list of h2 and/or h3 headings with ids. \ (default: nil)") assign_opt_value(opt, :split_main_heading, "-s", "--split-main-heading", "Split the first h1 element") opt.on("-W", "--with-wikiname", "Use WikiNames") do |with_wikiname| if with_wikiname auto_linker = PseudoHiki::AutoLink::WikiName.new PseudoHiki::BlockParser.auto_linker = auto_linker end end opt.on("-d [domain_name(s)]", "--domain-name [=domain_name(s)]", "Specify domain name(s)") do |domain_name| names = domain_name.split(/;\s*/) self[:domain_name] = names.shift self[:alternative_domain_names] = names end assign_opt_value(opt, :relative_link, "-r", "--relative-links-in-html", "Replace absolute paths with relative ones. \ *** THIS OPTION IS EXPERIMENTAL ***") assign_opt_value(opt, :accessibility, "-a", "--accessibility", "A bit improved accessibility") opt end end
setup_ruby_encoding(given_opt)
click to toggle source
# File lib/pseudohiki/converter.rb, line 366 def setup_ruby_encoding(given_opt) return nil unless String.new.respond_to? :encoding external, internal = given_opt.split(/:/o, 2) Encoding.default_external = external if external and not external.empty? Encoding.default_internal = internal if internal and not internal.empty? end
title()
click to toggle source
# File lib/pseudohiki/converter.rb, line 334 def title self[:title] || @default_title || "-" end
value_given?(value)
click to toggle source
# File lib/pseudohiki/converter.rb, line 309 def value_given?(value) value and not value.empty? end
win32?()
click to toggle source
# File lib/pseudohiki/converter.rb, line 305 def win32? true if RUBY_PLATFORM.match? /win/i end