Skip to content

Commit

Permalink
watch only for relevant files, some doc
Browse files Browse the repository at this point in the history
  • Loading branch information
mna committed Jul 15, 2013
1 parent 4141bc9 commit 5139630
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 89 deletions.
48 changes: 48 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// trofaf is a super-simple *live* static blog engine.
//
// Install using: `go get github.com/PuerkitoBio/trofaf`
//
// ## Description
//
// It generates a static website from *markdown* files and front matter, and requires only a simple
// 3-directories structure to get going. It favors simplicity over features.
//
// To get started, create the 3 subdirectories (you can look at the `example/` subdirectory
// for... an example):
// * posts
// * public
// * templates
//
// trofaf only cares about `*.md` files in the posts directory, and about `*.amber` (Amber templates)
// or `*.html` (native Go templates) files in the templates directory. It will watch for changes,
// creates or deletes on those files in these directories, and will re-generate automatically
// the website when required. This is the *live* part.
//
// All files in the public directory are exposed by the web server. Posts in markdown format get
// translated to static html files at the root of the public directory. The html file name is
// an URL-friendly slug generated from the original markdown file name. There is no extension, so
// the URL looks clean and, uh, *modern*?
//
// An RSS feed is automatically generated from a number of recent posts, and saved as a static
// XML file in the public directory.
//
// There is no special template for an index page, the most recent post (based on the publication
// date found in the front matter of the markdown files) is saved twice - once under its own
// html file, once under the index.html file, so that this is the page displayed when the root
// of the web server is requested.
//
// When the site is (re-)generated, the public directory must be cleaned, because some posts may
// have been deleted. Subdirectories are left untouched (so that `css/` or `js/` directories can
// coexist peacefully), as well as hidden (dot) files, and some special files are also graced
// from the destruction (robots.txt, favicon.ico, etc., see gen.go).
//
// ## Command-line Options
//
// The following options can be set at the command-line:
// * Port (-p) : the port number for the web server, defaults to 9000.
// * NoGen (-G) : prevents watching and live-generating the site. This is equivalent to running the static public directory.
// * SiteName (-n) : the name of the web site, passed to the template.
// * TagLine (-t) : a tag line for the web site, passed to the template.
// * RecentPostsCount (-r) : the number of posts in the recent posts list, passed to the template and used for the RSS feed.
// * BaseURL (-b) : the base URL of the web site, most likely the host name (i.e. http://www.mysite.com).
package main
2 changes: 1 addition & 1 deletion example/posts/003-dependances.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Date: 2012-03-05
Date: 2012-03-01
Title: npm: la base essentielle pour débuter avec node.js
Author: Martin Angers
Category: technologie
Expand Down
2 changes: 1 addition & 1 deletion example/posts/004-npm-packages.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Date: 2012-03-12
Date: 2012-03-01
Title: npm shrinkwrap: Comment contrôler ses dépendances
Author: Martin Angers
Category: technologie
Expand Down
1 change: 1 addition & 0 deletions example/posts/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test2!
6 changes: 3 additions & 3 deletions example/public/002-meta-billet
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/003-dependances
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/004-npm-packages
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/005-structurer-express
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/006-dependency-injection
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/007-implement-js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/008-backbone-computed-properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/009-je-men-vais
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/010-demenagement
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
6 changes: 3 additions & 3 deletions example/public/j-42-preparation
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/fontello.css" />
<link href="http://fonts.googleapis.com/css?family=Telex" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="/rss" />
<link rel="alternate" type="application/rss&#43;xml" title="RSS" href="http://localhost/rss" />
</head>
<body>

<div class="header">
<div class="title">
<a href="/"></a>
<a href="/">Ø value</a>
<br />
<span class="tagline"></span>
<span class="tagline">a wysiwyg hypertext cybersite</span>
</div>
</div>
<nav>
Expand Down
2 changes: 1 addition & 1 deletion example/public/rss
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><title></title><description></description><link></link><lastBuildDate>14 Jul 13 19:43 -0400</lastBuildDate><generator>trofaf</generator><item><title>Ce blogue a déménagé sur GitHub</title><link>/010-demenagement</link><description>Suite à l&#39;annonce de la fermeture de Calepin.co, j&#39;ai déménagé ce blogue sur http://hypermegatop.github.com. Veuillez mettre à jour vos favoris!</description><author>Martin Angers</author><category></category><pubDate>30 Apr 12 00:00 +0000</pubDate></item><item><title>Je suis venu te dire que je m&#39;en vais</title><link>/009-je-men-vais</link><description>Ou plutôt, &#34;je suis venu te dire qu&#39;on m&#39;évince&#34;, mais ça &#34;punch&#34; moins... En effet, mon coup de foudre pour le blogiciel Calepin aura finalement été son baiser de la mort. Sur Twitter la semaine dernière, via le canal officiel du site, on apprenait que c&#39;était la fin des haricots.</description><author>Martin Angers</author><category></category><pubDate>23 Apr 12 00:00 +0000</pubDate></item><item><title>Propriétés calculées avec Backbone</title><link>/008-backbone-computed-properties</link><description>La beauté de l&#39;univers du code libre est que lorsqu&#39;il manque une fonctionnalité, on peut se retrousser les manches, ouvrir le code et l&#39;ajouter. Voici ma petite histoire des propriétés calculées avec Backbone.</description><author>Martin Angers</author><category></category><pubDate>13 Apr 12 00:00 +0000</pubDate></item><item><title>implement.js: typage fort et Javascript</title><link>/007-implement-js</link><description>L&#39;injection de dépendance avec Javascript a comme conséquence de ne pouvoir assumer que les fonctionnalités offertes par l&#39;instance reçue seront celles attendues.</description><author>Martin Angers</author><category></category><pubDate>02 Apr 12 00:00 +0000</pubDate></item><item><title>Expérimentations sur l&#39;injection de dépendance avec node.js</title><link>/006-dependency-injection</link><description>L&#39;injection de dépendance permet de découpler les différentes composantes d&#39;une application pour en faciliter l&#39;entretien, la testabilité, circonscrire l&#39;impact des changements, mais aussi pour imposer une façon d&#39;aborder la création de l&#39;application en une aggrégation de pièces simples, à la responsabilité ciblée, et à l&#39;API bien défini.</description><author>Martin Angers</author><category></category><pubDate>27 Mar 12 00:00 +0000</pubDate></item></channel></rss>
<rss version="2.0"><channel><title>Ø value</title><description>a wysiwyg hypertext cybersite</description><link>http://localhost</link><lastBuildDate>14 Jul 13 20:36 -0400</lastBuildDate><generator>trofaf</generator><item><title>Ce blogue a déménagé sur GitHub</title><link>http://localhost/010-demenagement</link><description>Suite à l&#39;annonce de la fermeture de Calepin.co, j&#39;ai déménagé ce blogue sur http://hypermegatop.github.com. Veuillez mettre à jour vos favoris!</description><author>Martin Angers</author><category></category><pubDate>30 Apr 12 00:00 +0000</pubDate></item><item><title>Je suis venu te dire que je m&#39;en vais</title><link>http://localhost/009-je-men-vais</link><description>Ou plutôt, &#34;je suis venu te dire qu&#39;on m&#39;évince&#34;, mais ça &#34;punch&#34; moins... En effet, mon coup de foudre pour le blogiciel Calepin aura finalement été son baiser de la mort. Sur Twitter la semaine dernière, via le canal officiel du site, on apprenait que c&#39;était la fin des haricots.</description><author>Martin Angers</author><category></category><pubDate>23 Apr 12 00:00 +0000</pubDate></item><item><title>Propriétés calculées avec Backbone</title><link>http://localhost/008-backbone-computed-properties</link><description>La beauté de l&#39;univers du code libre est que lorsqu&#39;il manque une fonctionnalité, on peut se retrousser les manches, ouvrir le code et l&#39;ajouter. Voici ma petite histoire des propriétés calculées avec Backbone.</description><author>Martin Angers</author><category></category><pubDate>13 Apr 12 00:00 +0000</pubDate></item><item><title>implement.js: typage fort et Javascript</title><link>http://localhost/007-implement-js</link><description>L&#39;injection de dépendance avec Javascript a comme conséquence de ne pouvoir assumer que les fonctionnalités offertes par l&#39;instance reçue seront celles attendues.</description><author>Martin Angers</author><category></category><pubDate>02 Apr 12 00:00 +0000</pubDate></item><item><title>Expérimentations sur l&#39;injection de dépendance avec node.js</title><link>http://localhost/006-dependency-injection</link><description>L&#39;injection de dépendance permet de découpler les différentes composantes d&#39;une application pour en faciliter l&#39;entretien, la testabilité, circonscrire l&#39;impact des changements, mais aussi pour imposer une façon d&#39;aborder la création de l&#39;application en une aggrégation de pièces simples, à la responsabilité ciblée, et à l&#39;API bien défini.</description><author>Martin Angers</author><category></category><pubDate>27 Mar 12 00:00 +0000</pubDate></item></channel></rss>
24 changes: 18 additions & 6 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
Expand All @@ -15,9 +16,8 @@ import (
)

var (
postTpl *template.Template
postTplNm = "post.amber"
rssTplNm = "rss.amber"
postTpl *template.Template // The one and only compiled post template
postTplNm = "post.amber" // The amber post template file name (native Go are compiled using ParseGlob)

// Special files in the public directory, that must not be deleted
// If value is true, this must match the prefix of the file (HasPrefix())
Expand All @@ -35,12 +35,14 @@ var (
}
)

// This type is a slice of *LongPost that implements the sort.Interface, to sort in PubTime order.
type sortableLongPost []*LongPost

func (s sortableLongPost) Len() int { return len(s) }
func (s sortableLongPost) Less(i, j int) bool { return s[i].PubTime.Before(s[j].PubTime) }
func (s sortableLongPost) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// Filter cleans the slice of FileInfo to leave only `.md` files (markdown)
func Filter(fi []os.FileInfo) []os.FileInfo {
for i := 0; i < len(fi); {
if fi[i].IsDir() || filepath.Ext(fi[i].Name()) != ".md" {
Expand All @@ -52,6 +54,7 @@ func Filter(fi []os.FileInfo) []os.FileInfo {
return fi
}

// Compile the Post template.
func compileTemplate() error {
ap := filepath.Join(TemplatesDir, postTplNm)
if _, err := os.Stat(ap); os.IsNotExist(err) {
Expand All @@ -73,6 +76,7 @@ func compileTemplate() error {
return nil
}

// Clear the public directory, ignoring special files, subdirectories, and hidden (dot) files.
func clearPublicDir() error {
// Clear the public directory, except subdirs and special files (favicon.ico)
fis, err := ioutil.ReadDir(PublicDir)
Expand All @@ -93,6 +97,7 @@ func clearPublicDir() error {
return nil
}

// Generate the whole site.
func generateSite() error {
// First compile the template(s)
if err := compileTemplate(); err != nil {
Expand All @@ -107,9 +112,14 @@ func generateSite() error {
fis = Filter(fis)

// Get all posts.
all := make(sortableLongPost, len(fis))
for i, fi := range fis {
all[i] = newLongPost(fi)
all := make(sortableLongPost, 0, len(fis))
for _, fi := range fis {
lp, err := newLongPost(fi)
if err == nil {
all = append(all, lp)
} else {
log.Printf("post skipped: %s; error: %s\n", fi.Name(), err)
}
}
// Then sort in reverse order (newer first)
sort.Sort(sort.Reverse(all))
Expand All @@ -131,6 +141,7 @@ func generateSite() error {
return generateRss(td)
}

// Creates the rss feed from the recent posts.
func generateRss(td *TemplateData) error {
r := NewRss(td.SiteName, td.TagLine, Options.BaseURL)
base, err := url.Parse(Options.BaseURL)
Expand All @@ -147,6 +158,7 @@ func generateRss(td *TemplateData) error {
return r.WriteToFile(filepath.Join(PublicDir, "rss"))
}

// Generate the static HTML file for the post identified by the index.
func generateFile(td *TemplateData, idx bool) error {
var w io.Writer

Expand Down
Loading

0 comments on commit 5139630

Please sign in to comment.