Class: Wikiscript::Page

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/wikiscript/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil, title: nil, lang: Wikiscript.lang) ⇒ Page

Returns a new instance of Page.



24
25
26
27
28
29
30
31
32
# File 'lib/wikiscript/page.rb', line 24

def initialize( text=nil, title: nil, lang: Wikiscript.lang )
  ## todo: check title
  ## replace title spaces w/ _ ????
  ##  to allow "pretty" titles - why? why not??

  @text  = text
  @title = title
  @lang  = lang
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



8
9
10
# File 'lib/wikiscript/page.rb', line 8

def lang
  @lang
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/wikiscript/page.rb', line 8

def title
  @title
end

Class Method Details

.get(title, lang: Wikiscript.lang) ⇒ Object

todo/check: add a fetch/download alias - why? why not?



11
12
13
14
15
# File 'lib/wikiscript/page.rb', line 11

def self.get( title, lang: Wikiscript.lang )    ## todo/check: add a fetch/download alias - why? why not?
  o = new( title: title, lang: lang )
  o.get  ## "force" refresh text (get/fetch/download)
  o
end

.read(path) ⇒ Object



17
18
19
20
21
# File 'lib/wikiscript/page.rb', line 17

def self.read( path )
  text = File.open( path, 'r:utf-8' ) { |f| f.read }
  o = new( text, title: "File:#{path}" )   ## use auto-generated File:<path> title path - why? why not?
  o
end

Instance Method Details

#eachObject

loop over all nodes / elements -note: nodes is a (flat) list (array) for now



42
43
44
45
46
# File 'lib/wikiscript/page.rb', line 42

def each   ## loop over all nodes / elements -note: nodes is a (flat) list (array) for now
  nodes.each do |node|
    yield( node )
  end
end

#getObject Also known as: fetch, download

“force” refresh text (get/fetch/download)



49
50
51
52
53
54
# File 'lib/wikiscript/page.rb', line 49

def get    ## "force" refresh text (get/fetch/download)
  @nodes = nil  ## note: reset cached parsed nodes too

  @text = Client.new.text( @title, lang: @lang )
  @text
end

#nodesObject



38
39
40
# File 'lib/wikiscript/page.rb', line 38

def nodes
  @nodes ||= parse     # cache text (from parse)
end

#parseObject

todo/change: use/find a different name e.g. doc/elements/etc. - why? why not?



59
60
61
62
# File 'lib/wikiscript/page.rb', line 59

def parse   ## todo/change: use/find a different name e.g. doc/elements/etc. - why? why not?
  @nodes = PageReader.parse( text )
  @nodes
end

#textObject



34
35
36
# File 'lib/wikiscript/page.rb', line 34

def text
  @text ||= get        # cache text (from request)
end