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.



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

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.



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

def lang
  @lang
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Class Method Details

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

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



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

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



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

def self.read( path )
  text = File.open( path, 'r:utf-8' ).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



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

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)



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

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



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

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?



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

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

#textObject



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

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