generate readme pages through a plugin
authorRalf Jung <post@ralfj.de>
Wed, 7 Oct 2015 16:15:17 +0000 (18:15 +0200)
committerRalf Jung <post@ralfj.de>
Wed, 7 Oct 2015 16:15:17 +0000 (18:15 +0200)
_config.yml
_includes/menu-level.html
_plugins/menu.rb
_plugins/readmes.rb [new file with mode: 0644]
build.sh [deleted file]

index 83ead92..9ec865f 100644 (file)
@@ -5,11 +5,19 @@ blog:
     title: "Ralf's Ramblings"
     description: "Ralf rambling on things"
 
+readmes:
+    src_base: "/home/r/src"
+    out_base: "projects"
+    projects:
+      - name: "lilass"
+      - name: "dyn-nsupdate"
+      - name: "schsh"
+
 defaults:
   - scope:
-      path: "" # all files in the project
+        path: "" # all files in the project
     values:
-      layout: "default"
+        layout: "default"
 
 # Build settings
 markdown: kramdown
index 5f5ac46..df0c6ea 100644 (file)
@@ -1,5 +1,5 @@
 <ul><!--{% for item in include.menu %}
-    --><li class="{{ item.class }}"><a href="{{ item.url }}">{{ item.url }}: {{ item.title }}</a></li><!--
+    --><li class="{{ item.class }}"><a href="{{ item.url }}">{{ item.title }}</a></li><!--
     {% if item.sub %}
         -->{% include menu-level.html menu=item.sub %}<!--
     {% endif %}
index 317db0c..98614b7 100644 (file)
@@ -17,7 +17,7 @@ module Jekyll
                             css_class = 'sibling'
                         end
                         # create menu node
-                        menu_node = { 'url' => page['url'], 'title' => page['title'], 'class' => css_class }
+                        menu_node = { 'url' => page['url'], 'title' => page['slug'] ? page['slug'] : (page['title'] ? page['title'] : page['url']), 'class' => css_class, 'sort' => page['sort'] }
                         # potentially recurse
                         if (css_class == 'parent' or css_class == 'current')
                             sub_nodes = create_menu(pages, cururl + "/", displayurl)
@@ -29,7 +29,8 @@ module Jekyll
                         result.push(menu_node)
                     end
                 end
-                result
+                # sort the result before returning it
+                result.sort_by { |a| a['url'] }
             end
             
             create_menu(input, "", displayurl.sub('/index.html', ''))
diff --git a/_plugins/readmes.rb b/_plugins/readmes.rb
new file mode 100644 (file)
index 0000000..272e849
--- /dev/null
@@ -0,0 +1,35 @@
+module Jekyll
+
+  class ReadmePage < Page
+    def initialize(site, base, dir, src)
+      @site = site
+      @base = base
+      @dir = dir
+      @name = 'index.md'
+
+      self.process(@name)
+      self.data ||= {}
+      
+      content = File.read(src, self.merged_file_read_opts({})).each_line.to_a
+
+      self.data['layout'] = 'default'
+      self.data['title'] = content[0].match(/^# (.*)\n$/)[1]
+      self.data['slug'] = self.data['title'].match(/^(.*):.*$/)[1]
+      self.content = content[1..content.size].join
+    end
+  end
+
+  class CategoryPageGenerator < Generator
+    safe true
+
+    def generate(site)
+      readmes = site.config['readmes']
+      base = readmes['src_base']
+      dir = readmes['out_base']
+      for project in readmes['projects']
+          site.pages << ReadmePage.new(site, site.source, File.join(dir, project['name']), File.join(base, project['name'], 'README.md'))
+      end
+    end
+  end
+
+end
diff --git a/build.sh b/build.sh
deleted file mode 100755 (executable)
index 56157d3..0000000
--- a/build.sh
+++ /dev/null
@@ -1,42 +0,0 @@
-#!/bin/bash
-
-PROJECTS_BASE="$HOME/src"
-TMPFILE=$(mktemp)
-
-while getopts "ds" opt; do
-  case $opt in
-    d)
-      DEVEL=1
-      ;;
-    s)
-      SYNC=1
-      ;;
-  esac
-done
-
-# update READMEs
-for PROJECT in lilass schsh; do
-    # get the file
-    if [ -n "$DEVEL" ]; then
-        INFILE="$PROJECTS_BASE/$PROJECT/README.md"
-    else
-        cd "$PROJECTS_BASE/$PROJECT"
-        git show master:README.rst > "$TMPFILE"
-        INFILE="$TMPFILE"
-        cd - > /dev/null
-    fi
-    OUTFILE="projects/$PROJECT/index.md"
-    # process it
-    echo "---" > "$OUTFILE"
-    head -n 1 "$INFILE" | sed 's/^# \(.*\)$/title: "\1"/' >> "$OUTFILE"
-    echo "---" >> "$OUTFILE"
-    echo >> "$OUTFILE"
-    tail -n +3 "$INFILE" >> "$OUTFILE"
-done
-
-# call jekyll
-jekyll build
-
-if [ -n "$SYNC" ]; then
-    echo "TODO: sync to server"
-fi