Sha256: 1a222abb15e5aa0c753b879881a766003ae18f5af1ff8f9558e4538e5564e9ce

Contents?: true

Size: 1.83 KB

Versions: 23

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true
require 'cgi'

##
# Creates HTML-safe labels suitable for use in id attributes.  Tidylinks are
# converted to their link part and cross-reference links have the suppression
# marks removed (\\SomeClass is converted to SomeClass).

class RDoc::Markup::ToLabel < RDoc::Markup::Formatter

  attr_reader :res # :nodoc:

  ##
  # Creates a new formatter that will output HTML-safe labels

  def initialize markup = nil
    super nil, markup

    @markup.add_regexp_handling RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
    @markup.add_regexp_handling(/(((\{.*?\})|\b\S+?)\[\S+?\])/, :TIDYLINK)

    add_tag :BOLD, '', ''
    add_tag :TT,   '', ''
    add_tag :EM,   '', ''

    @res = []
  end

  ##
  # Converts +text+ to an HTML-safe label

  def convert text
    label = convert_flow @am.flow text

    CGI.escape(label).gsub('%', '-').sub(/^-/, '')
  end

  ##
  # Converts the CROSSREF +target+ to plain text, removing the suppression
  # marker, if any

  def handle_regexp_CROSSREF target
    text = target.text

    text.sub(/^\\/, '')
  end

  ##
  # Converts the TIDYLINK +target+ to just the text part

  def handle_regexp_TIDYLINK target
    text = target.text

    return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/

    $1
  end

  alias accept_blank_line         ignore
  alias accept_block_quote        ignore
  alias accept_heading            ignore
  alias accept_list_end           ignore
  alias accept_list_item_end      ignore
  alias accept_list_item_start    ignore
  alias accept_list_start         ignore
  alias accept_paragraph          ignore
  alias accept_raw                ignore
  alias accept_rule               ignore
  alias accept_verbatim           ignore
  alias end_accepting             ignore
  alias handle_regexp_HARD_BREAK  ignore
  alias start_accepting           ignore

end

Version data entries

23 entries across 23 versions & 4 rubygems

Version Path
rdoc-6.4.1.1 lib/rdoc/markup/to_label.rb
rdoc-6.3.4.1 lib/rdoc/markup/to_label.rb
ric-0.14.2 vendor/bundle/ruby/2.7.0/gems/rdoc-6.4.0/lib/rdoc/markup/to_label.rb
ric-0.14.1 vendor/bundle/ruby/2.7.0/gems/rdoc-6.4.0/lib/rdoc/markup/to_label.rb
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/rdoc-6.4.0/lib/rdoc/markup/to_label.rb
rdoc-6.4.0 lib/rdoc/markup/to_label.rb
rdoc-6.3.3 lib/rdoc/markup/to_label.rb
rdoc-6.1.2.1 lib/rdoc/markup/to_label.rb
rdoc-6.3.2 lib/rdoc/markup/to_label.rb
gitlab-rdoc-6.3.2 lib/rdoc/markup/to_label.rb
rdoc-6.3.1 lib/rdoc/markup/to_label.rb
rdoc-6.3.0 lib/rdoc/markup/to_label.rb
rdoc-6.2.1 lib/rdoc/markup/to_label.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/rdoc-6.1.1/lib/rdoc/markup/to_label.rb
rdoc-6.2.0 lib/rdoc/markup/to_label.rb
rdoc-6.1.2 lib/rdoc/markup/to_label.rb
ric-0.13.0 vendor/bundle/ruby/2.5.0/gems/rdoc-6.1.1/lib/rdoc/markup/to_label.rb
ric-0.12.2 vendor/bundle/ruby/2.5.0/gems/rdoc-6.1.1/lib/rdoc/markup/to_label.rb
rdoc-6.1.1 lib/rdoc/markup/to_label.rb
rdoc-6.1.0 lib/rdoc/markup/to_label.rb