class Infoboxer::Tree::Wikilink

Internal MediaWiki link class.

See [Wikipedia docs](en.wikipedia.org/wiki/Help:Link#Wikilinks) for extensive explanation of Wikilink concept.

Note, that Wikilink is {Linkable}, so you can {Linkable#follow follow} it to obtain linked pages.

Attributes

anchor[R]

Anchor part of hyperlink, like `History` for `[Argentina#History]`

interwiki[R]

Interwiki identifier. For example, `[[wikt:Argentina]]` will have `“Argentina”` as its {#name} and `“wikt”` (wiktionary) as an interwiki. TODO: how to use it.

See [Wikipedia docs](en.wikipedia.org/wiki/Help:Interwiki_linking) for details.

name[R]

“Clean” wikilink name, for ex., `Cities` for `[Category:Cities]`

namespace[R]

Wikilink namespace, `Category` for `[Category:Cities]`, empty string (not `nil`!) for just `[Cities]`

refinement[R]

Refinement part of link name.

See {#topic} for explanation.

topic[R]

Topic part of link name.

There's so-called [“Pipe trick”](en.wikipedia.org/wiki/Help:Pipe_trick) in wikilink markup, which defines that `[Phoenix, Arizona]` link has main part (“Phoenix”) and refinement part (“Arizona”). So, we are splitting it here in `topic` and {#refinement}. The same way, `[Pipe (programming)]` has `topic == 'Pipe'` and `refinement == 'programming'`

Public Class Methods

new(link, label = nil, namespace: nil, interwiki: nil) click to toggle source
Calls superclass method Infoboxer::Tree::Link::new
# File lib/infoboxer/tree/wikilink.rb, line 15
def initialize(link, label = nil, namespace: nil, interwiki: nil)
  super(link, label, namespace: namespace, interwiki: interwiki)
  @namespace = namespace || ''
  @interwiki = interwiki
  parse_name!
end

Private Instance Methods

parse_name!() click to toggle source
# File lib/infoboxer/tree/wikilink.rb, line 58
def parse_name!
  @name = namespace.empty? ? link : link.sub(/^#{namespace}:/, '')
  @name, @anchor = @name.split('#', 2)
  @anchor ||= ''

  parse_topic!
end
parse_topic!() click to toggle source

@see en.wikipedia.org/wiki/Help:Pipe_trick

# File lib/infoboxer/tree/wikilink.rb, line 67
def parse_topic!
  @topic, @refinement =
    case @name
    when /^(.+\S)\s*\((.+)\)$/, /^(.+?),\s*(.+)$/
      [Regexp.last_match(1), Regexp.last_match(2)]
    else
      [@name, '']
    end

  return unless children.count == 1 &&
                children.first.is_a?(Text) && children.first.raw_text.empty?

  children.first.raw_text = @topic
end