Class: RuneBlog::Widget::Pages

Inherits:
Object
  • Object
show all
Defined in:
lib/widgets/pages/pages.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Pages

Returns a new instance of Pages.



9
10
11
12
13
14
# File 'lib/widgets/pages/pages.rb', line 9

def initialize(repo)
  @blog = repo
  @datafile = "list.data"
  @lines = _get_data(@datafile)
  @data = @lines.map {|x| x.chomp.split(/, */, 2) }
end

Instance Method Details

#_html_body(file, css = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/widgets/pages/pages.rb', line 27

def _html_body(file, css = nil)
  file.puts "<html>"
  if css
    file.puts "    <head>"  
    file.puts "        <style>\n#{css}\n          </style>"
    file.puts "    </head>"  
  end
  file.puts "  <body>"
  yield
  file.puts "  </body>\n</html>"
end

#buildObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/widgets/pages/pages.rb', line 16

def build
  # build child pages
  children = Dir["*.lt3"] - ["pages.lt3"]
  children.each do |child|
    dest = child.sub(/.lt3$/, ".html")
    preprocess src: child, dst: dest
  end
  write_main
  write_card
end

#edit_menuObject



124
125
# File 'lib/widgets/pages/pages.rb', line 124

def edit_menu
end

#manageObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/widgets/pages/pages.rb', line 89

def manage
  dir = @blog.view.dir/"widgets/pages"
  # Assume child files already generated (and list.data??)
  data = dir/"list.data"
  lines = _get_data?(data)
  hash = {}
  lines.each do |line|
    url, name = line.chomp.split(",")
    source = url.sub(/.html$/, ".lt3")
    hash[name] = source
  end
  new_item = "[New page]"
  num, fname = STDSCR.menu(title: "Edit page:", items: hash.keys + [new_item])
  return if fname.nil?
  if fname == new_item
    print "Page title:  "
    title = RubyText.gets
    title.chomp!
    print "File name (.lt3): "
    fname = RubyText.gets
    fname << ".lt3" unless fname.end_with?(".lt3")
    fhtml = fname.sub(/.lt3$/, ".html")
    File.open(data, "a") {|f| f.puts "#{fhtml},#{title}" }
    new_file = dir/fname
    File.open(new_file, "w") do |f|
      f.puts "<h1>#{title}</h1>\n\n\n "
      f.puts ".backlink"
    end
    edit_file(new_file)
  else
    target = hash[fname]
    edit_file(dir/target)
  end
end

#refreshObject



127
128
# File 'lib/widgets/pages/pages.rb', line 127

def refresh
end

#write_cardObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/widgets/pages/pages.rb', line 55

def write_card
  tag = Type
  url = :widgets/tag/tag+"-main.html"
  card_title = "Pages"  # FIXME
  cardfile = "#{Type}-card"
  File.open("#{cardfile}.html", "w") do |f|
    f.puts <<-EOS
      <div class="card mb-3">
        <div class="card-body">
          <h5 class="card-title">
            <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="##{tag}">+</button>
            <a href="javascript: void(0)" 
               onclick="javascript:open_main('#{url}')" 
               style="text-decoration: none; color: black">#{card_title}</a>
          </h5>
          <div class="collapse" id="#{tag}">
    EOS
    @data.each do |url2, title|
      f.puts "<!-- #{[url2, title].inspect} -->"
      url3 = :widgets/tag/url2
      f.puts "<!-- url3 = #{url3.inspect} -->"
      url_ref = %[href="javascript: void(0)" onclick="javascript:open_main('#{url3}')"]
      anchor = %[<a #{url_ref}>#{title}</a>]
      wrapper = %[<li class="list-group-item">#{anchor}</li>]
      f.puts wrapper
    end
    f.puts <<-EOS
          </div>
        </div>
      </div>
    EOS
  end
end

#write_mainObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/widgets/pages/pages.rb', line 39

def write_main
  css = "body { font-family: verdana }"
  card_title = Title
  File.open("#{Type}-main.html", "w") do |f|     
    _html_body(f, css) do
      f.puts "<h1>#{card_title}</h1><br><hr>"
      url_ref = nil
      @data.each do |url, title|
        url_ref = "href = '#{url}'"
        css = "color: #8888FF; text-decoration: none; font-size: 21px"
        f.puts %[<a style="#{css}" #{url_ref}>#{title}</a> <br>]
      end
    end
  end
end