Class: Wikiscript::PageReader

Inherits:
Object
  • Object
show all
Defined in:
lib/wikiscript/page_reader.rb

Class Method Summary collapse

Class Method Details

.parse(txt) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wikiscript/page_reader.rb', line 12

def self.parse( txt )
  page = []   ## page structure

  inside_table = false
  table_txt    = nil

  txt.each_line do |line|
    line = line.strip

    break if line == '__END__'

    ## note:  allow/add comments
    ##   note: CANNOT allow inline (end-of-line) comments
    ##     would strip/break css colors eg.  bgcolor=#ffff44
    next if line.start_with?( '#' )   ## skip comments too
    next if line.empty?               ## skip empty lines for now

    ## note: like in wikimedia markup (and markdown) all optional trailing ==== too
    ##  todo/check:  allow ===  Text  =-=-=-=-=-=   too - why? why not?
    if line =~ /^(={1,})       ## leading ======
                ([^=]+?)       ##  text   (note: for now no "inline" = allowed)
                =*             ## (optional) trailing ====
                $/x
      heading_marker = $1
      heading_level  = $1.length   ## count number of = for heading level
      heading        = $2.strip

      puts "heading #{heading_level} >#{heading}<"
      page << [:"h#{heading_level}", heading]
    elsif line.start_with?( '{|' )     ## start table
      inside_table = true
      table_txt = String.new   ## collect table source text
      table_txt << line << "\n"    ## note: do NOT forget to add back newline!!
    elsif inside_table && line.start_with?( '|}' )  ## end table
      table_txt << line << "\n"
      table = TableReader.parse_table( table_txt )
      page << [:table, table]
      ## reset table variables
      inside_table = false
      table_txt    = nil
    elsif inside_table
       table_txt << line << "\n"
    else
      ## note: skip unknown line types for now

      ## puts "** !!! ERROR !!! unknown line type in wiki page:"
      ## pp line
      ## exit 1
    end
  end
  page
end

.read(path) ⇒ Object



6
7
8
9
# File 'lib/wikiscript/page_reader.rb', line 6

def self.read( path )
  txt = File.open( path, 'r:utf-8' ) { |f| f.read }
  parse( txt )
end