aboutsummaryrefslogtreecommitdiff
path: root/hyde.rb
blob: 1914754f6883285b81098964a00249da7b9f9a63 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
123
124
125
126
#!/usr/local/bin/ruby

require "erb"
require "optparse"
require "yaml"
require 'date'

options = {}
OptionParser.new do |opt|
  opt.on("-f", "--file-name FILENAME", "File name of the page you want to render") {
    |o| options[:fileName] = o
  }
  opt.on("-t", "--title TITLE", "Title of the rendered page") {
    |o| options[:title] = o
  }
  opt.on("-m", "--master MASTER", "Master file page") {
    |o| options[:master] = o
  }
  opt.on("-n", "--page-name NAME", "page name") {
    |o| options[:pageNames] = o
  }
end.parse!

#puts options

class Page
  attr_reader :title
  attr_reader :pageFileName
  attr_reader :description
  attr_reader :date
  attr_reader :classes
  attr_reader :category

  def initialize title, pageFileName, pageNames, description, date, classes, category
    @title = title
    @pageFileName = pageFileName
    @pageNames = pageNames
    @description = description
    @date = date
    @classes = classes
    @category = category
  end

  def render path
    content = File.read(File.expand_path(path))
    t = ERB.new(content)
    t.result(binding)
  end
end

def listarticles(path, max)
  pages = Array.new
  Dir.glob(path).each { |file|
    if (file.end_with?(".rhtml") && File.file?(file))
      config = YAML.load_file(file + '.config')
      file["rhtml"] = "html"
      date = Date.parse(file.gsub(/.*\/(.*)_.*/, '\1'))
      pages.append(Page.new(config['title'], file, nil, config['description'], date, config['classes'], nil))
      max = max - 1
      if (max == 0)
        break
      end
    end
  }

  return pages
end

if(!File.exist?(options[:fileName] + '.config') && !options[:fileName].end_with?("tmpl"))
  puts 'Cannot find: ' + options[:fileName] + '.config'
  exit -1
end

if(File.exist?(options[:fileName] + '.config'))
  config = YAML.load_file(options[:fileName] + '.config')

  if(config != nil)
    title = config['title']
    pageNames = config['pageNames']
    description = config['description']
    classes = config['classes']
    master = config['master']
    category = config['category']
  end
end

page = Page.new(title, options[:fileName], pageNames, description, nil, classes, category)

if (options[:master] != nil)
  master = options[:master]
end

if (options[:master] == nil && master == nil)
  master = "master.rhtml"
end

if (master.kind_of?(Array))
  i = 0
  filesToDelete = Array.new
  fileName = nil
  master.each { |m|
    fileName = options[:fileName] + "." + i.to_s + ".tmp";
    filesToDelete.append(fileName)
    output = File.open(fileName, "w")
    output << page.render(m)
    page = Page.new(title, fileName, pageNames, description, nil, classes, category)
    output.close
    i = i + 1
  }

  if (fileName != nil)
    output = File.open(fileName)
    puts output.read
    output.close
  end

  filesToDelete.each { |f|
    File.delete(f) if File.exist?(f)
  }

else
  puts page.render(master)
end