Class: PseudoHiki::OptionManager
- Inherits:
-
Object
- Object
- PseudoHiki::OptionManager
- Includes:
- HtmlElement::CHARSET
- Defined in:
- lib/pseudohiki/converter.rb
Defined Under Namespace
Classes: Formatter
Constant Summary collapse
- PlainVerboseFormat =
PlainTextFormat.create(:verbose_mode => true)
- MDFormat =
MarkDownFormat.create
- GFMFormat =
MarkDownFormat.create(:gfm_style => true)
- VERSIONS =
[ ["html4", HtmlFormat, HtmlTemplate, ".html", /^h/io], ["xhtml1", XhtmlFormat, XhtmlTemplate, ".html", /^x/io], ["html5", Xhtml5Format, Xhtml5Template, ".html", /^h5/io], ["plain", PageComposer::PlainFormat, nil, ".plain", /^p/io], ["plain_verbose", PlainVerboseFormat, nil, ".plain", /^pv/io], ["markdown", MDFormat, nil, ".md", /^m/io], ["gfm", GFMFormat, nil, ".md", /^g/io] ].map {|args| Formatter.new(*args) }
- ENCODING_REGEXP =
{ /^u/io => 'utf8', /^e/io => 'euc-jp', /^s/io => 'sjis', /^l[a-zA-Z]*1/io => 'latin1' }
- BOM =
"\xef\xbb\xbf"
- FILE_HEADER_PAT =
/^\/\//
- ENCODING_TO_CHARSET =
{ 'utf8' => UTF8, 'euc-jp' => EUC_JP, 'sjis' => SJIS, 'latin1' => LATIN1 }
Constants included from HtmlElement::CHARSET
HtmlElement::CHARSET::EUC_JP, HtmlElement::CHARSET::LATIN1, HtmlElement::CHARSET::SJIS, HtmlElement::CHARSET::UTF8
Instance Attribute Summary collapse
-
#default_title ⇒ Object
Returns the value of attribute default_title.
-
#input_file_basename ⇒ Object
readonly
Returns the value of attribute input_file_basename.
-
#need_output_file ⇒ Object
Returns the value of attribute need_output_file.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #assign_opt_value(opt, key, short, long, description) ⇒ Object
- #base ⇒ Object
- #charset ⇒ Object
- #check_argv ⇒ Object
- #create_html_template_with_current_options ⇒ Object
- #formatter ⇒ Object
- #html_template ⇒ Object
-
#initialize(options = nil) ⇒ OptionManager
constructor
A new instance of OptionManager.
- #open_output ⇒ Object
- #output_filename ⇒ Object
- #parse_command_line_options {|opt| ... } ⇒ Object
- #parse_opt_setup_ruby_encoding(opt) ⇒ Object
- #read_input_filename(filename) ⇒ Object
- #read_template_file ⇒ Object
- #set_html_encoding(given_opt) ⇒ Object
- #set_html_version(version) ⇒ Object
- #set_options_from_input_file(input_lines) ⇒ Object
- #setup_command_line_options ⇒ Object
- #setup_ruby_encoding(given_opt) ⇒ Object
- #title ⇒ Object
- #value_given?(value) ⇒ Boolean
- #win32? ⇒ Boolean
Constructor Details
#initialize(options = nil) ⇒ OptionManager
Returns a new instance of OptionManager.
289 290 291 292 293 294 295 |
# File 'lib/pseudohiki/converter.rb', line 289 def initialize(=nil) @options = || self.class. @written_option_pat = {} @options.keys.each do |opt| @written_option_pat[opt] = /^\/\/#{opt}:\s*(.*)$/ end end |
Instance Attribute Details
#default_title ⇒ Object
Returns the value of attribute default_title.
276 277 278 |
# File 'lib/pseudohiki/converter.rb', line 276 def default_title @default_title end |
#input_file_basename ⇒ Object (readonly)
Returns the value of attribute input_file_basename.
277 278 279 |
# File 'lib/pseudohiki/converter.rb', line 277 def input_file_basename @input_file_basename end |
#need_output_file ⇒ Object
Returns the value of attribute need_output_file.
276 277 278 |
# File 'lib/pseudohiki/converter.rb', line 276 def need_output_file @need_output_file end |
Class Method Details
.default_options ⇒ Object
285 286 287 |
# File 'lib/pseudohiki/converter.rb', line 285 def self. @default_options.dup end |
.remove_bom(input = ARGF) ⇒ Object
279 280 281 282 283 |
# 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 |
Instance Method Details
#[](key) ⇒ Object
297 298 299 |
# File 'lib/pseudohiki/converter.rb', line 297 def [](key) @options[key] end |
#[]=(key, value) ⇒ Object
301 302 303 |
# File 'lib/pseudohiki/converter.rb', line 301 def[]=(key, value) @options[key] = value end |
#assign_opt_value(opt, key, short, long, description) ⇒ Object
381 382 383 |
# 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 ⇒ Object
325 326 327 328 329 330 331 332 |
# 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 ⇒ Object
321 322 323 |
# File 'lib/pseudohiki/converter.rb', line 321 def charset ENCODING_TO_CHARSET[self[:encoding]] end |
#check_argv ⇒ Object
485 486 487 488 489 490 491 492 493 494 |
# 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 ⇒ Object
515 516 517 518 519 520 521 522 523 524 |
# File 'lib/pseudohiki/converter.rb', line 515 def 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 ⇒ Object
317 318 319 |
# File 'lib/pseudohiki/converter.rb', line 317 def formatter self[:html_version].formatter end |
#html_template ⇒ Object
313 314 315 |
# File 'lib/pseudohiki/converter.rb', line 313 def html_template self[:html_version].template end |
#open_output ⇒ Object
541 542 543 544 545 546 547 |
# 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 ⇒ Object
531 532 533 534 535 536 537 538 539 |
# File 'lib/pseudohiki/converter.rb', line 531 def output_filename return nil unless @need_output_file if self[:output] File.(self[:output]) else ext = self[:html_version].ext File.join(@input_file_dir, @input_file_basename + ext) end end |
#parse_command_line_options {|opt| ... } ⇒ Object
496 497 498 499 500 501 502 |
# File 'lib/pseudohiki/converter.rb', line 496 def opt = yield opt if block_given? opt.parse! check_argv @default_title = @input_file_basename end |
#parse_opt_setup_ruby_encoding(opt) ⇒ Object
373 374 375 376 377 378 379 |
# 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) ⇒ Object
526 527 528 529 |
# File 'lib/pseudohiki/converter.rb', line 526 def read_input_filename(filename) @input_file_dir, @input_file_name = File.split(File.(filename)) @input_file_basename = File.basename(@input_file_name, ".*") end |
#read_template_file ⇒ Object
338 339 340 |
# File 'lib/pseudohiki/converter.rb', line 338 def read_template_file File.read(File.(self[:template]), :encoding => charset) end |
#set_html_encoding(given_opt) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 |
# 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) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 |
# 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) ⇒ Object
504 505 506 507 508 509 510 511 512 513 |
# File 'lib/pseudohiki/converter.rb', line 504 def (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 ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/pseudohiki/converter.rb', line 385 def 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.(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) ⇒ Object
366 367 368 369 370 371 |
# 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 ⇒ Object
334 335 336 |
# File 'lib/pseudohiki/converter.rb', line 334 def title self[:title] || @default_title || "-" end |
#value_given?(value) ⇒ Boolean
309 310 311 |
# File 'lib/pseudohiki/converter.rb', line 309 def value_given?(value) value and not value.empty? end |
#win32? ⇒ Boolean
305 306 307 |
# File 'lib/pseudohiki/converter.rb', line 305 def win32? true if RUBY_PLATFORM.match? /win/i end |