922d3a896566d0ff2441fc15e0f180b20d7011f1
[web.git] / personal / _plugins / readmes.rb
1 module Jekyll
2
3   class ReadmePage < Page
4     def initialize(site, base, dir, src, idx)
5       @site = site
6       @base = base
7       @dir = dir
8       @name = 'index.md'
9
10       self.process(@name)
11       self.data ||= {}
12       
13       content = File.read(src, self.merged_file_read_opts({})).each_line.to_a
14
15       self.data['layout'] = 'default'
16       self.data['title'] = content[0].match(/^#* ?(.*)\n$/)[1]
17       slug = self.data['title'].match(/^([^:]*):.*$/)
18       if slug
19         self.data['slug'] = slug[1]
20       end
21       self.data['sort'] = idx
22       self.content = content[1..content.size].join
23     end
24   end
25
26   class ReadmePageGenerator < Generator
27     safe true
28
29     def generate(site)
30       readmes = site.config['readmes']
31       return if readmes.nil?
32       base = readmes['src_base']
33       dir = readmes['out_base']
34       idx = 0
35       for project in readmes['projects']
36           site.pages << ReadmePage.new(site, site.source, File.join(dir, project['name']), File.join(base, project['src'] ? project['src'] : project['name'], 'README.md'), idx)
37           idx += 1
38       end
39     end
40   end
41
42 end