Class: Kramdown::Converter::Rfc2629
- Includes:
- Utils::Html
- Defined in:
- lib/kramdown-rfc2629.rb
Overview
Converts a Kramdown::Document to HTML.
Constant Summary collapse
- INDENTATION =
Defines the amount of indentation used when nesting XML tags.
2
- STYLES =
{ul: 'symbols', ol: 'numbers', dl: 'hanging'}
- HTML_TAGS_WITH_BODY =
['div', 'script']
- ALIGNMENTS =
{ default: :left, left: :left, right: :right, center: :center}
- COLS_ALIGN =
{ "l" => :left, "c" => :center, "r" => :right}
- REFCACHEDIR =
ENV["KRAMDOWN_REFCACHEDIR"] || ".refcache"
- KRAMDOWN_OFFLINE =
warn “*** REFCACHEDIR #REFCACHEDIR”
- KRAMDOWN_REFCACHE_REFETCH =
- XML_RESOURCE_ORG_MAP =
- subdirectory name, cache ttl in seconds, does it provide for ?anchor=
{ "RFC" => ["bibxml", 86400*7], # these should change rarely "I-D" => "bibxml3", "W3C" => "bibxml4", "3GPP" => "bibxml5", "ANSI" => "bibxml2", "CCITT" => "bibxml2", "FIPS" => "bibxml2", # "IANA" => "bibxml2", overtaken by bibxml8 "IEEE" => "bibxml2", # now, how about bibxml6? "ISO" => "bibxml2", "ITU" => "bibxml2", "NIST" => "bibxml2", "OASIS" => "bibxml2", "PKCS" => "bibxml2", "DOI" => ["bibxml7", 86400, true], # 24 h cache at source anyway "IANA" => ["bibxml8", 86400, true], # ditto }
- XML_RESOURCE_ORG_HOST =
XML_RESOURCE_ORG_HOST = ENV || “xml.resource.org”
ENV["XML_RESOURCE_ORG_HOST"] || "xml2rfc.tools.ietf.org"
- XML_RESOURCE_ORG_PREFIX =
ENV["XML_RESOURCE_ORG_PREFIX"] || "https://#{XML_RESOURCE_ORG_HOST}/public/rfc"
- KRAMDOWN_REFCACHETTL =
(e = ENV["KRAMDOWN_REFCACHETTL"]) ? e.to_i : 3600
- EMPH =
{ em: "emph", strong: "strong"}
- TYPOGRAPHIC_SYMS =
{ :mdash => [::Kramdown::Utils::Entities.entity('mdash')], :ndash => [::Kramdown::Utils::Entities.entity('ndash')], :hellip => [::Kramdown::Utils::Entities.entity('hellip')], :laquo_space => [::Kramdown::Utils::Entities.entity('laquo'), ::Kramdown::Utils::Entities.entity('nbsp')], :raquo_space => [::Kramdown::Utils::Entities.entity('nbsp'), ::Kramdown::Utils::Entities.entity('raquo')], :laquo => [::Kramdown::Utils::Entities.entity('laquo')], :raquo => [::Kramdown::Utils::Entities.entity('raquo')] }
- ITEM_RE =
'\s*(?:"([^"]*)"|([^,]*?))\s*'
- IREF_RE =
%r{\A(!\s*)?#{ITEM_RE}(?:,#{ITEM_RE})?\z}
Instance Method Summary collapse
-
#clean_pcdata(parts) ⇒ Object
hack, will become unnecessary with XML2RFCv3.
- #convert(el, indent = -INDENTATION,, opts = {}) ⇒ Object
- #convert1(el, indent, opts = {}) ⇒ Object
- #convert_a(el, indent, opts) ⇒ Object
-
#convert_abbreviation(el, indent, opts) ⇒ Object
XXX: This is wrong.
- #convert_blank(el, indent, opts) ⇒ Object
- #convert_blockquote(el, indent, opts) ⇒ Object
- #convert_br(el, indent, opts) ⇒ Object
- #convert_codeblock(el, indent, opts) ⇒ Object
- #convert_codespan(el, indent, opts) ⇒ Object
- #convert_comment(el, indent, opts) ⇒ Object
- #convert_dd(el, indent, opts) ⇒ Object
-
#convert_dt(el, indent, opts) ⇒ Object
SERIOUSLY BAD HACK:.
- #convert_em(el, indent, opts) ⇒ Object (also: #convert_strong)
- #convert_entity(el, indent, opts) ⇒ Object
-
#convert_footnote(el, indent, opts) ⇒ Object
XXX: footnotes into crefs???.
- #convert_header(el, indent, opts) ⇒ Object
-
#convert_hr(el, indent, opts) ⇒ Object
misuse for page break.
- #convert_html_element(el, indent, opts) ⇒ Object
-
#convert_img(el, indent, opts) ⇒ Object
misuse the tag!.
- #convert_iref(el, indent, opts) ⇒ Object
- #convert_li(el, indent, opts) ⇒ Object
-
#convert_math(el, indent, opts) ⇒ Object
XXX: This is wrong.
- #convert_p(el, indent, opts) ⇒ Object
- #convert_raw(el, indent, opts) ⇒ Object
- #convert_root(el, indent, opts) ⇒ Object
- #convert_smart_quote(el, indent, opts) ⇒ Object
-
#convert_table(el, indent, opts) ⇒ Object
This only works for tables with headers.
- #convert_td(el, indent, opts) ⇒ Object (also: #convert_th)
- #convert_text(el, indent, opts) ⇒ Object
- #convert_thead(el, indent, opts) ⇒ Object (also: #convert_tbody, #convert_tfoot, #convert_tr)
- #convert_typographic_sym(el, indent, opts) ⇒ Object
- #convert_ul(el, indent, opts) ⇒ Object (also: #convert_ol, #convert_dl)
- #convert_xml_comment(el, indent, opts) ⇒ Object (also: #convert_xml_pi, #convert_html_doctype)
- #convert_xref(el, indent, opts) ⇒ Object
- #el_html_attributes(el) ⇒ Object
- #el_html_attributes_with(el, defattr) ⇒ Object
- #end_sections(to_level, indent) ⇒ Object
-
#get_and_cache_resource(url, cachefile, tvalid = 7200, tn = Time.now) ⇒ Object
this is now slightly dangerous as multiple urls could map to the same cachefile.
-
#initialize(*doc) ⇒ Rfc2629
constructor
Initialize the XML converter with the given Kramdown document
doc
. - #inner(el, indent, opts) ⇒ Object
- #inner_a(el, indent, opts) ⇒ Object
- #iref_attr(s) ⇒ Object
- #saner_generate_id(value) ⇒ Object
Constructor Details
#initialize(*doc) ⇒ Rfc2629
Initialize the XML converter with the given Kramdown document doc
.
102 103 104 105 106 107 |
# File 'lib/kramdown-rfc2629.rb', line 102 def initialize(*doc) super @sec_level = 1 @in_dt = 0 @footnote_names_in_use = {} end |
Instance Method Details
#clean_pcdata(parts) ⇒ Object
hack, will become unnecessary with XML2RFCv3
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/kramdown-rfc2629.rb', line 218 def clean_pcdata(parts) # hack, will become unnecessary with XML2RFCv3 clean = '' irefs = '' # warn "clean_parts: #{parts.inspect}" parts.each do |p| md = p.match(%r{([^<]*)(.*)}) clean << md[1] irefs << md[2] # breaks for spanx... don't emphasize in headings! end [clean, irefs] end |
#convert(el, indent = -INDENTATION,, opts = {}) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/kramdown-rfc2629.rb', line 109 def convert(el, indent = -INDENTATION, opts = {}) if el.children[-1].type == :raw raw = convert1(el.children.pop, indent, opts) end "#{convert1(el, indent, opts)}#{end_sections(1, indent)}#{raw}" end |
#convert1(el, indent, opts = {}) ⇒ Object
116 117 118 119 |
# File 'lib/kramdown-rfc2629.rb', line 116 def convert1(el, indent, opts = {}) el.rfc2629_fix send("convert_#{el.type}", el, indent, opts) end |
#convert_a(el, indent, opts) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/kramdown-rfc2629.rb', line 385 def convert_a(el, indent, opts) gi = el.attr.delete('gi') res = inner(el, indent, opts) target = el.attr['target'] if target[0] == "#" # handle [](#foo) as xref as in RFC 7328 el.attr['target'] = target = target[1..-1] if target.downcase == res.downcase res = '' # get rid of raw anchors leaking through end gi ||= "xref" else gi ||= "eref" end "<#{gi}#{el_html_attributes(el)}>#{res}</#{gi}>" end |
#convert_abbreviation(el, indent, opts) ⇒ Object
XXX: This is wrong
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/kramdown-rfc2629.rb', line 641 def convert_abbreviation(el, indent, opts) # XXX: This is wrong title = @root.[:abbrev_defs][el.value] title = nil if title.empty? value = el.value if item = title m = title.scan(Parser::RFC2629Kramdown::IREF_START) if m.empty? subitem = value else iref = m.map{|a,| iref_attr(a)}.join('') end else item = value end iref ||= "<iref#{html_attributes(item: item, subitem: subitem)}/>" "#{el.value}#{iref}" end |
#convert_blank(el, indent, opts) ⇒ Object
133 134 135 |
# File 'lib/kramdown-rfc2629.rb', line 133 def convert_blank(el, indent, opts) "\n" end |
#convert_blockquote(el, indent, opts) ⇒ Object
199 200 201 202 203 |
# File 'lib/kramdown-rfc2629.rb', line 199 def convert_blockquote(el, indent, opts) text = inner(el, indent, opts) text = "<t></t>" unless text =~ /</ # empty block quote "#{' '*indent}<t><list style='empty'#{el_html_attributes(el)}>\n#{text}#{' '*indent}</list></t>\n" end |
#convert_br(el, indent, opts) ⇒ Object
381 382 383 |
# File 'lib/kramdown-rfc2629.rb', line 381 def convert_br(el, indent, opts) "<vspace />" end |
#convert_codeblock(el, indent, opts) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/kramdown-rfc2629.rb', line 153 def convert_codeblock(el, indent, opts) # el.attr['anchor'] ||= saner_generate_id(el.value) -- no longer in 1.0.6 result = el.value blockclass = el.attr.delete('class') if blockclass == 'language-tbreak' result = result.lines.map {|line| [line.chomp, 0]} spaceind = 0 result.each_with_index {|pair, index| if pair[0] == '' result[spaceind][1] += 1 pair[0] = nil unless index == spaceind else spaceind = index end } # $stderr.puts(result.inspect) result = result.map {|line, space| "<![CDATA[#{line.gsub(/^\s+/) {|s| "\u00A0" * s.size}}]]><vspace blankLines=\"#{space}\"/>" if line }.compact.join("\n") "#{' '*indent}<t>#{result}</t>\n" else artwork_attr = {} if blockclass classes = blockclass.split(' ') classes.each do |cl| if md = cl.match(/\Alanguage-(.*)/) artwork_attr["type"] = md[1] # XXX overwrite else $stderr.puts "*** Unimplemented block class: #{cl}" end end end # compensate for XML2RFC idiosyncrasy by insisting on a blank line unless el.attr.delete('tight') result[0,0] = "\n" unless result[0,1] == "\n" end el.attr.each do |k, v| if md = k.match(/\Aartwork-(.*)/) el.attr.delete(k) artwork_attr[md[1]] = v end end "#{' '*indent}<figure#{el_html_attributes(el)}><artwork#{html_attributes(artwork_attr)}><![CDATA[#{result}#{result =~ /\n\Z/ ? '' : "\n"}]]></artwork></figure>\n" end end |
#convert_codespan(el, indent, opts) ⇒ Object
542 543 544 545 |
# File 'lib/kramdown-rfc2629.rb', line 542 def convert_codespan(el, indent, opts) attrstring = el_html_attributes_with(el, {"style" => 'verb'}) "<spanx#{attrstring}>#{escape_html(el.value)}</spanx>" end |
#convert_comment(el, indent, opts) ⇒ Object
372 373 374 375 376 377 378 379 |
# File 'lib/kramdown-rfc2629.rb', line 372 def convert_comment(el, indent, opts) ## Don't actually output all those comments into the XML: # if el.options[:category] == :block # "#{' '*indent}<!-- #{el.value} -->\n" # else # "<!-- #{el.value} -->" # end end |
#convert_dd(el, indent, opts) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/kramdown-rfc2629.rb', line 273 def convert_dd(el, indent, opts) output = ' '*indent if @in_dt == 1 @in_dt = 0 else output << "<t#{el_html_attributes(el)}>" end res = inner(el, indent+INDENTATION, opts.merge(unpacked: true)) # if el.children.empty? || el.children.first.options[:category] != :block output << res << (res =~ /\n\Z/ ? ' '*indent : '') # else FIXME: The latter case is needed for more complex cases # output << "\n" << res << ' '*indent # end output << "</t>\n" end |
#convert_dt(el, indent, opts) ⇒ Object
SERIOUSLY BAD HACK:
289 290 291 292 293 294 295 296 |
# File 'lib/kramdown-rfc2629.rb', line 289 def convert_dt(el, indent, opts) # SERIOUSLY BAD HACK: close = "#{' '*indent}</t>\n" * @in_dt @in_dt = 1 vspace = opts[:vspace] vspaceel = "<vspace blankLines='#{vspace}'/>" if vspace ht = escape_html(inner(el, indent, opts), :attribute) # XXX this may leave gunk "#{close}#{' '*indent}<t#{el_html_attributes(el)} hangText='#{ht}'>#{vspaceel}\n" end |
#convert_em(el, indent, opts) ⇒ Object Also known as: convert_strong
572 573 574 575 576 |
# File 'lib/kramdown-rfc2629.rb', line 572 def convert_em(el, indent, opts) attrstring = el_html_attributes_with(el, {"style" => EMPH[el.type]}) span, irefs = clean_pcdata(inner_a(el, indent, opts)) "<spanx#{attrstring}>#{span}</spanx>#{irefs}" end |
#convert_entity(el, indent, opts) ⇒ Object
579 580 581 |
# File 'lib/kramdown-rfc2629.rb', line 579 def convert_entity(el, indent, opts) entity_to_str(el.value) end |
#convert_footnote(el, indent, opts) ⇒ Object
XXX: footnotes into crefs???
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/kramdown-rfc2629.rb', line 547 def convert_footnote(el, indent, opts) # XXX: footnotes into crefs??? # this would be more like xml2rfc v3: # "\n#{' '*indent}<cref>\n#{inner(el.value, indent, opts).rstrip}\n#{' '*indent}</cref>" content = inner(el.value, indent, opts).strip content = escape_html(content.sub(/\A<t>(.*)<\/t>\z/m) {$1}, :text) # text only... name = el.[:name].sub(/\A[0-9]/) {"_" << $&} while @footnote_names_in_use[name] do if name =~ /:\d+\z/ name.succ! else name << ":1" end end @footnote_names_in_use[name] = true attrstring = el_html_attributes_with(el, {"anchor" => name}) "\n#{' '*indent}<cref#{attrstring}>#{content}</cref>" end |
#convert_header(el, indent, opts) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/kramdown-rfc2629.rb', line 230 def convert_header(el, indent, opts) # todo: handle appendix tags el = el.deep_clone = @doc ? @doc. : @options # XXX: 0.11 vs. 0.12 if [:auto_ids] && !el.attr['anchor'] el.attr['anchor'] = saner_generate_id(el.[:raw_text]) end clean, irefs = clean_pcdata(inner_a(el, indent, opts)) el.attr['title'] = clean "#{end_sections(el.[:level], indent)}#{' '*indent}<section#{@sec_level += 1; el_html_attributes(el)}>#{irefs}\n" end |
#convert_hr(el, indent, opts) ⇒ Object
misuse for page break
242 243 244 |
# File 'lib/kramdown-rfc2629.rb', line 242 def convert_hr(el, indent, opts) # misuse for page break "#{' '*indent}<t><vspace blankLines='999' /></t>\n" end |
#convert_html_element(el, indent, opts) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/kramdown-rfc2629.rb', line 300 def convert_html_element(el, indent, opts) res = inner(el, indent, opts) if el.[:category] == :span "<#{el.value}#{el_html_attributes(el)}" << (!res.empty? ? ">#{res}</#{el.value}>" : " />") else output = '' output << ' '*indent if !el.[:parent_is_raw] output << "<#{el.value}#{el_html_attributes(el)}" if !res.empty? && el.[:parse_type] != :block output << ">#{res}</#{el.value}>" elsif !res.empty? output << ">\n#{res}" << ' '*indent << "</#{el.value}>" elsif HTML_TAGS_WITH_BODY.include?(el.value) output << "></#{el.value}>" else output << " />" end output << "\n" if el.[:outer_element] || !el.[:parent_is_raw] output end end |
#convert_img(el, indent, opts) ⇒ Object
misuse the tag!
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/kramdown-rfc2629.rb', line 503 def convert_img(el, indent, opts) # misuse the tag! if a = el.attr alt = a.delete('alt').strip alt = '' if alt == '!' # work around re-wrap uglyness if src = a.delete('src') a['target'] = src end end if alt == ":include:" # Really bad misuse of tag... anchor = el.attr.delete('anchor') || ( # not yet warn "*** missing anchor for '#{src}'" src ) anchor.sub!(/\A[0-9]/) { "_#{$&}" } # can't start an ID with a number anchor.gsub!('/', '_') # should take out all illegals to_insert = "" src.scan(/(W3C|3GPP|[A-Z-]+)[.]?([A-Za-z_0-9.\/\+-]+)/) do |t, n| fn = "reference.#{t}.#{n}.xml" sub, ttl = XML_RESOURCE_ORG_MAP[t] ttl ||= KRAMDOWN_REFCACHETTL # everything but RFCs might change a lot puts "Huh: ${fn}" unless sub url = "#{XML_RESOURCE_ORG_PREFIX}/#{sub}/#{fn}" to_insert = get_and_cache_resource(url, fn.gsub('/', '_'), ttl) to_insert.scrub! rescue nil # only do this for Ruby >= 2.1 # this may be a bit controversial: Don't break the build if reference is broken if KRAMDOWN_OFFLINE to_insert ||= "<reference anchor='#{anchor}'> <front> <title>*** BROKEN REFERENCE ***</title> <author> <organization/> </author> <date/> </front> </reference>" else exit 66 unless to_insert # EX_NOINPUT end end to_insert.sub(/<\?xml version=["']1.0["'] encoding=["']UTF-8["']\?>/, '') .sub(/\banchor=(?:"[^"]+"|'[^']+')/, "anchor=\"#{anchor}\"") else "<xref#{el_html_attributes(el)}>#{alt}</xref>" end end |
#convert_iref(el, indent, opts) ⇒ Object
637 638 639 |
# File 'lib/kramdown-rfc2629.rb', line 637 def convert_iref(el, indent, opts) iref_attr(el.attr['target']) end |
#convert_li(el, indent, opts) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/kramdown-rfc2629.rb', line 260 def convert_li(el, indent, opts) res_a = inner_a(el, indent, opts) if el.children.empty? || el.children.first.[:category] == :span res = res_a.join('') else # merge multiple <t> elements res = res_a.select { |x| x.strip != '' }.map { |x| x.sub(/\A\s*<t>(.*)<\/t>\s*\Z/m) { $1} }.join("#{' '*indent}<vspace blankLines='1'/>\n").gsub(%r{(</list>)\s*<vspace blankLines='1'/>}) { $1 }.gsub(%r{<vspace blankLines='1'/>\s*(<list)}) { $1 } end "#{' '*indent}<t#{el_html_attributes(el)}>#{res}#{(res =~ /\n\Z/ ? ' '*indent : '')}</t>\n" end |
#convert_math(el, indent, opts) ⇒ Object
XXX: This is wrong
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/kramdown-rfc2629.rb', line 600 def convert_math(el, indent, opts) # XXX: This is wrong el = el.deep_clone if el.[:category] == :block el.attr['artwork-type'] ||= '' el.attr['artwork-type'] += (el.attr['artwork-type'].empty? ? '' : ' ') + 'math' artwork_attr = {} el.attr.each do |k, v| if md = k.match(/\Aartwork-(.*)/) el.attr.delete(k) artwork_attr[md[1]] = v end end result, _s = Open3.capture2("tex2mail -noindent -ragged -by_par -linelength=69", stdin_data: el.value); # warn "*** tex2mail not in path?" unless s.success? -- doesn't have useful status "#{' '*indent}<figure#{el_html_attributes(el)}><artwork#{html_attributes(artwork_attr)}><![CDATA[#{result}#{result =~ /\n\Z/ ? '' : "\n"}]]></artwork></figure>\n" else warn "*** no support for inline math in XML2RFCv2" type = 'spanx' attrstring = el_html_attributes_with(el, {"style" => 'verb'}) "<#{type}#{attrstring}>#{escape_html(el.value, :text)}</#{type}>" end end |
#convert_p(el, indent, opts) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/kramdown-rfc2629.rb', line 141 def convert_p(el, indent, opts) if (el.children.size == 1 && el.children[0].type == :img) || opts[:unpacked] inner(el, indent, opts) # Part of the bad reference hack else "#{' '*indent}<t#{el_html_attributes(el)}>#{inner(el, indent, opts)}</t>\n" end end |
#convert_raw(el, indent, opts) ⇒ Object
565 566 567 568 |
# File 'lib/kramdown-rfc2629.rb', line 565 def convert_raw(el, indent, opts) end_sections(1, indent) + el.value + (el.[:category] == :block ? "\n" : '') end |
#convert_root(el, indent, opts) ⇒ Object
659 660 661 |
# File 'lib/kramdown-rfc2629.rb', line 659 def convert_root(el, indent, opts) result = inner(el, indent, opts) end |
#convert_smart_quote(el, indent, opts) ⇒ Object
596 597 598 |
# File 'lib/kramdown-rfc2629.rb', line 596 def convert_smart_quote(el, indent, opts) entity_to_str(smart_quote_entity(el)) end |
#convert_table(el, indent, opts) ⇒ Object
This only works for tables with headers
335 336 337 338 339 |
# File 'lib/kramdown-rfc2629.rb', line 335 def convert_table(el, indent, opts) # This only works for tables with headers alignment = el.[:alignment].map { |al| ALIGNMENTS[al]} cols = (el.attr.delete("cols") || "").split(' ') "#{' '*indent}<texttable#{el_html_attributes(el)}>\n#{inner(el, indent, opts.merge(table_alignment: alignment, table_cols: cols))}#{' '*indent}</texttable>\n" end |
#convert_td(el, indent, opts) ⇒ Object Also known as: convert_th
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/kramdown-rfc2629.rb', line 348 def convert_td(el, indent, opts) if alignment = opts[:table_alignment] alignment = alignment.shift if cols = opts[:table_cols].shift md = cols.match(/(\d*(|em|[%*]))([lrc])/) if md[1].to_i != 0 widthval = md[1] widthval << "em" if md[2].empty? widthopt = "width='#{widthval}' " end alignment = COLS_ALIGN[md[3]] || :left end end if alignment res, irefs = clean_pcdata(inner_a(el, indent, opts)) warn "*** lost markup #{irefs} in table heading" unless irefs.empty? "#{' '*indent}<ttcol #{widthopt}align='#{alignment}'#{el_html_attributes(el)}>#{res.empty? ? " " : res}</ttcol>\n" # XXX need clean_pcdata else res = inner(el, indent, opts) "#{' '*indent}<c#{el_html_attributes(el)}>#{res.empty? ? " " : res}</c>\n" end end |
#convert_text(el, indent, opts) ⇒ Object
137 138 139 |
# File 'lib/kramdown-rfc2629.rb', line 137 def convert_text(el, indent, opts) escape_html(el.value, :text) end |
#convert_thead(el, indent, opts) ⇒ Object Also known as: convert_tbody, convert_tfoot, convert_tr
341 342 343 |
# File 'lib/kramdown-rfc2629.rb', line 341 def convert_thead(el, indent, opts) inner(el, indent, opts) end |
#convert_typographic_sym(el, indent, opts) ⇒ Object
592 593 594 |
# File 'lib/kramdown-rfc2629.rb', line 592 def convert_typographic_sym(el, indent, opts) TYPOGRAPHIC_SYMS[el.value].map {|e| entity_to_str(e)}.join('') end |
#convert_ul(el, indent, opts) ⇒ Object Also known as: convert_ol, convert_dl
248 249 250 251 252 253 254 255 256 |
# File 'lib/kramdown-rfc2629.rb', line 248 def convert_ul(el, indent, opts) opts = opts.merge(vspace: el.attr.delete('vspace')) attrstring = el_html_attributes_with(el, {"style" => STYLES[el.type]}) if opts[:unpacked] "#{' '*indent}<list#{attrstring}>\n#{inner(el, indent, opts)}#{' '*indent}</list>\n" else "#{' '*indent}<t><list#{attrstring}>\n#{inner(el, indent, opts)}#{' '*indent}</list></t>\n" end end |
#convert_xml_comment(el, indent, opts) ⇒ Object Also known as: convert_xml_pi, convert_html_doctype
322 323 324 325 326 327 328 |
# File 'lib/kramdown-rfc2629.rb', line 322 def convert_xml_comment(el, indent, opts) if el.[:category] == :block && !el.[:parent_is_raw] ' '*indent + el.value + "\n" else el.value end end |
#convert_xref(el, indent, opts) ⇒ Object
401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/kramdown-rfc2629.rb', line 401 def convert_xref(el, indent, opts) gi = el.attr.delete('gi') target = el.attr['target'] if target[0] == "&" "#{target};" else if target =~ %r{\A\w+:(?://|.*@)} gi ||= "eref" else gi ||= "xref" end "<#{gi}#{el_html_attributes(el)}/>" end end |
#el_html_attributes(el) ⇒ Object
89 90 91 |
# File 'lib/kramdown-rfc2629.rb', line 89 def el_html_attributes(el) html_attributes(el.attr) end |
#el_html_attributes_with(el, defattr) ⇒ Object
92 93 94 |
# File 'lib/kramdown-rfc2629.rb', line 92 def el_html_attributes_with(el, defattr) html_attributes(defattr.merge(el.attr)) end |
#end_sections(to_level, indent) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/kramdown-rfc2629.rb', line 205 def end_sections(to_level, indent) if indent < 0 indent = 0 end if @sec_level >= to_level delta = (@sec_level - to_level) @sec_level = to_level "#{' '*indent}</section>\n" * delta else $stderr.puts "Incorrect section nesting: Need to start with 1" end end |
#get_and_cache_resource(url, cachefile, tvalid = 7200, tn = Time.now) ⇒ Object
this is now slightly dangerous as multiple urls could map to the same cachefile
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 |
# File 'lib/kramdown-rfc2629.rb', line 424 def get_and_cache_resource(url, cachefile, tvalid = 7200, tn = Time.now) fn = "#{REFCACHEDIR}/#{cachefile}" Dir.mkdir(REFCACHEDIR) unless Dir.exists?(REFCACHEDIR) f = File.stat(fn) rescue nil unless KRAMDOWN_REFCACHE_REFETCH if !KRAMDOWN_OFFLINE && (!f || tn - f.ctime >= tvalid) if f = "renewing (stale by #{"%.1f" % ((tn-f.ctime)/86400)} days)" fetch_timeout = 10 # seconds, give up quickly if just renewing else = "fetching" fetch_timeout = 60 # seconds; long timeout needed for Travis end $stderr.puts "#{fn}: #{}" if ENV["HAVE_WGET"] `cd #{REFCACHEDIR}; wget -t 3 -T #{fetch_timeout} -Nnv "#{url}"` # ignore errors if offline (hack) begin File.utime nil, nil, fn rescue Errno::ENOENT warn "Can't fetch #{url} -- is wget in path?" end else require 'open-uri' require 'socket' require 'openssl' require 'timeout' begin Timeout::timeout(fetch_timeout) do = {} if ENV["KRAMDOWN_DONT_VERIFY_HTTPS"] [:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE end # workaround for OpenSSL on Windows... open(url, ) do |uf| s = uf.read if uf.status[0] != "200" warn "*** Status code #{status} while fetching #{url}" else File.write(fn, s) end end end rescue OpenURI::HTTPError, SocketError, Timeout::Error => e warn "*** #{e} while fetching #{url}" end end end begin File.read(fn) # this blows up if no cache available after fetch attempt rescue Errno::ENOENT => e warn "*** #{e} for ${fn}" end end |
#inner(el, indent, opts) ⇒ Object
129 130 131 |
# File 'lib/kramdown-rfc2629.rb', line 129 def inner(el, indent, opts) inner_a(el, indent, opts).join('') end |
#inner_a(el, indent, opts) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/kramdown-rfc2629.rb', line 121 def inner_a(el, indent, opts) indent += INDENTATION el.children.map do |inner_el| inner_el.rfc2629_fix send("convert_#{inner_el.type}", inner_el, indent, opts) end end |
#iref_attr(s) ⇒ Object
627 628 629 630 631 632 633 634 635 |
# File 'lib/kramdown-rfc2629.rb', line 627 def iref_attr(s) md = s.match(IREF_RE) attr = { item: md[2] || md[3], subitem: md[4] || md[5], primary: md[1] && 'true', } "<iref#{html_attributes(attr)}/>" end |
#saner_generate_id(value) ⇒ Object
149 150 151 |
# File 'lib/kramdown-rfc2629.rb', line 149 def saner_generate_id(value) generate_id(value).gsub(/-+/, '-') end |