lib/kramdown/converter/man.rb in kramdown-1.17.0 vs lib/kramdown/converter/man.rb in kramdown-2.0.0.beta1

- old
+ new

@@ -1,9 +1,9 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8; frozen_string_literal: true -*- # #-- -# Copyright (C) 2009-2016 Thomas Leitner <t_leitner@gmx.at> +# Copyright (C) 2009-2019 Thomas Leitner <t_leitner@gmx.at> # # This file is part of kramdown which is licensed under the MIT. #++ # @@ -15,18 +15,18 @@ # Converts a Kramdown::Document to a manpage in groff format. See man(7), groff_man(7) and # man-pages(7) for information regarding the output. class Man < Base - def convert(el, opts = {:indent => 0, :result => ''}) #:nodoc: + def convert(el, opts = {indent: 0, result: +''}) #:nodoc: send("convert_#{el.type}", el, opts) end private def inner(el, opts, use = :all) - arr = el.children.reject {|e| e.type == :blank} + arr = el.children.reject {|e| e.type == :blank } arr.each_with_index do |inner_el, index| next if use == :rest && index == 0 break if use == :first && index > 0 options = opts.dup options[:parent] = el @@ -37,19 +37,19 @@ end end def convert_root(el, opts) @title_done = false - opts[:result] = ".\\\" generated by kramdown\n" + opts[:result] = +".\\\" generated by kramdown\n" inner(el, opts) opts[:result] end def convert_blank(*) end - alias :convert_hr :convert_blank - alias :convert_xml_pi :convert_blank + alias convert_hr convert_blank + alias convert_xml_pi convert_blank def convert_p(el, opts) if (opts[:index] != 0 && opts[:prev].type != :header) || (opts[:parent].type == :blockquote && opts[:index] == 0) opts[:result] << macro("P") @@ -102,12 +102,12 @@ compact = (el.attr['class'] =~ /\bcompact\b/) opts[:result] << macro("sp") << macro("PD", 0) if compact inner(el, opts) opts[:result] << macro("PD") if compact end - alias :convert_dl :convert_ul - alias :convert_ol :convert_ul + alias convert_dl convert_ul + alias convert_ol convert_ul def convert_li(el, opts) sym = (opts[:parent].type == :ul ? '\(bu' : "#{opts[:index] + 1}.") opts[:result] << macro("IP", sym, 4) inner(el, opts, :first) @@ -132,23 +132,23 @@ opts[:result] << macro("RE") end opts[:result] << macro("sp") if opts[:next] && opts[:next].type == :dd end - TABLE_CELL_ALIGNMENT = {:left => 'l', :center => 'c', :right => 'r', :default => 'l'} + TABLE_CELL_ALIGNMENT = {left: 'l', center: 'c', right: 'r', default: 'l'} def convert_table(el, opts) - opts[:alignment] = el.options[:alignment].map {|a| TABLE_CELL_ALIGNMENT[a]} + opts[:alignment] = el.options[:alignment].map {|a| TABLE_CELL_ALIGNMENT[a] } table_options = ["box"] table_options << "center" if el.attr['class'] =~ /\bcenter\b/ - opts[:result] << macro("TS") << "#{table_options.join(" ")} ;\n" + opts[:result] << macro("TS") << "#{table_options.join(' ')} ;\n" inner(el, opts) opts[:result] << macro("TE") << macro("sp") end def convert_thead(el, opts) - opts[:result] << opts[:alignment].map {|a| "#{a}b"}.join(' ') << " .\n" + opts[:result] << opts[:alignment].map {|a| "#{a}b" }.join(' ') << " .\n" inner(el, opts) opts[:result] << "=\n" end def convert_tbody(el, opts) @@ -167,11 +167,11 @@ opts[:result] << "\n" end def convert_td(el, opts) result = opts[:result] - opts[:result] = '' + opts[:result] = +'' inner(el, opts) if opts[:result] =~ /\n/ warning("Table cells using links are not supported") result << "\t" else @@ -184,13 +184,12 @@ end def convert_xml_comment(el, opts) newline(opts[:result]) << ".\"#{escape(el.value, true).rstrip.gsub(/\n/, "\n.\"")}\n" end - alias :convert_comment :convert_xml_comment + alias convert_comment convert_xml_comment - def convert_a(el, opts) if el.children.size == 1 && el.children[0].type == :text && el.attr['href'] == el.children[0].value newline(opts[:result]) << macro("UR", escape(el.attr['href'])) << macro("UE") elsif el.attr['href'].start_with?('mailto:') @@ -201,11 +200,11 @@ inner(el, opts) newline(opts[:result]) << macro("UE") end end - def convert_img(el, opts) + def convert_img(_el, _opts) warning("Images are not supported") end def convert_em(el, opts) opts[:result] << '\fI' @@ -221,11 +220,11 @@ def convert_codespan(el, opts) opts[:result] << "\\fB#{escape(el.value)}\\fP" end - def convert_br(el, opts) + def convert_br(_el, opts) newline(opts[:result]) << macro("br") end def convert_abbreviation(el, opts) opts[:result] << escape(el.value) @@ -245,15 +244,13 @@ def convert_raw(*) warning("Raw content is not supported") end - - def convert_text(el, opts) text = escape(el.value) - text.lstrip! if opts[:result][-1] == ?\n + text.lstrip! if opts[:result][-1] == "\n" opts[:result] << text end def convert_entity(el, opts) opts[:result] << unicode_char(el.value.code_point) @@ -262,12 +259,12 @@ def convert_smart_quote(el, opts) opts[:result] << unicode_char(::Kramdown::Utils::Entities.entity(el.value.to_s).code_point) end TYPOGRAPHIC_SYMS_MAP = { - :mdash => '\(em', :ndash => '\(em', :hellip => '\.\.\.', - :laquo_space => '\[Fo]', :raquo_space => '\[Fc]', :laquo => '\[Fo]', :raquo => '\[Fc]' + mdash: '\(em', ndash: '\(em', hellip: '\.\.\.', + laquo_space: '\[Fo]', raquo_space: '\[Fc]', laquo: '\[Fo]', raquo: '\[Fc]' } def convert_typographic_sym(el, opts) opts[:result] << TYPOGRAPHIC_SYMS_MAP[el.value] end @@ -275,11 +272,11 @@ def macro(name, *args) ".#{[name, *args].compact.join(' ')}\n" end def newline(text) - text << "\n" unless text[-1] == ?\n + text << "\n" unless text[-1] == "\n" text end def quote(text) "\"#{text.gsub(/"/, '\\"')}\"" @@ -287,10 +284,10 @@ def escape(text, preserve_whitespace = false) text = (preserve_whitespace ? text.dup : text.gsub(/\s+/, ' ')) text.gsub!('\\', "\\e") text.gsub!(/^\./, '\\\\&.') - text.gsub!(/[.'-]/) {|m| "\\#{m}"} + text.gsub!(/[.'-]/) {|m| "\\#{m}" } text end def unicode_char(codepoint) "\\[u#{codepoint.to_s(16).rjust(4, '0')}]"