rib: Static site generator using Shake

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Haskell library for writing your own static site generator


[Skip to Readme]

Properties

Versions 0.2.0.0, 0.3.0.0, 0.4.1.0, 0.5.0.0, 0.6.0.0, 0.6.0.0, 0.7.0.0, 0.8.0.0, 0.10.0.0, 0.12.0.0, 1.0.0.0
Change log CHANGELOG.md
Dependencies aeson (>=1.4.2 && <1.5), async, base-noprelude (>=4.7 && <5), binary (>=0.8.6 && <0.9), clay (>=0.13.1 && <0.14), cmdargs (>=0.10.20 && <0.11), containers (>=0.6.0 && <0.7), directory (>=1.0 && <2.0), exceptions, foldl, fsnotify (>=0.3.0 && <0.4), lucid (>=2.9.11 && <2.10), megaparsec, mmark, mmark-ext, modern-uri, mtl (>=2.2.2 && <2.3), pandoc (>=2.7 && <3), pandoc-include-code (>=1.4.0 && <1.5), pandoc-types (>=1.17.5 && <1.18), path (>=0.7.0), path-io, relude (>=0.6 && <0.7), shake (>=0.18.4), text (>=1.2.3 && <1.3), wai (>=3.2.2 && <3.3), wai-app-static (>=3.1.6 && <3.2), warp [details]
License BSD-3-Clause
Copyright 2019 Sridhar Ratnakumar
Author Sridhar Ratnakumar
Maintainer srid@srid.ca
Category Web
Home page https://github.com/srid/rib#readme
Bug tracker https://github.com/srid/rib/issues
Source repo head: git clone https://github.com/srid/rib
Uploaded by sridca at 2020-01-08T19:52:28Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for rib-0.6.0.0

[back to package description]

Logo

rib

BSD3 Hackage built with nix Zulip chat

Rib is a Haskell library for writing your own static site generator.

How does it compare to Hakyll?

Rib prioritizes the use of existing tools over reinventing them, and enables the user to compose them as they wish instead of having to write code to fit a custom framework.

Table of Contents

Quick Preview

Here is how your code may look like if you were to generate your static site using Rib:

-- | A generated page corresponds to either an index of sources, or an
-- individual source.
--
-- Each `Source` specifies the parser type to use. Rib provides `MMark` and
-- `Pandoc`; but you may define your own as well.
data Page
  = Page_Index [Source M.MMark]
  | Page_Single (Source M.MMark)

-- | Metadata in our markdown sources. Parsed as JSON.
data SrcMeta
  = SrcMeta
      { title :: Text,
        -- | Description is optional, hence it is a `Maybe`
        description :: Maybe Text
      }
  deriving (Show, Eq, Generic, FromJSON)

-- | Main entry point to our generator.
--
-- `Rib.run` handles CLI arguments, and takes three parameters here.
--
-- 1. Directory `a`, from which static files will be read.
-- 2. Directory `b`, under which target files will be generated.
-- 3. Shake build action to run.
--
-- In the shake build action you would expect to use the utility functions
-- provided by Rib to do the actual generation of your static site.
main :: IO ()
main = Rib.run [reldir|a|] [reldir|b|] generateSite
  where
    -- Shake Action for generating the static site
    generateSite :: Action ()
    generateSite = do
      -- Copy over the static files
      Rib.buildStaticFiles [[relfile|static/**|]]
      -- Build individual sources, generating .html for each.
      -- The function `buildHtmlMulti` takes the following arguments:
      -- - File patterns to build
      -- - Function that will parse the file (here we use mmark)
      -- - Function that will generate the HTML (see below)
      srcs <-
        Rib.buildHtmlMulti M.parse [[relfile|*.md|]] $
          renderPage . Page_Single
      -- Write an index.html linking to the aforementioned files.
      Rib.writeHtml [relfile|index.html|] $
        renderPage (Page_Index srcs)
    -- Define your site HTML here
    renderPage :: Page -> Html ()
    renderPage page = with html_ [lang_ "en"] $ do
      head_ $ do
        meta_ [httpEquiv_ "Content-Type", content_ "text/html; charset=utf-8"]
        title_ $ case page of
          Page_Index _ -> "My website!"
          Page_Single src -> toHtml $ title $ getMeta src
        style_ [type_ "text/css"] $ Clay.render pageStyle
      body_
        $ with div_ [id_ "thesite"]
        $ do
          with a_ [href_ "/"] "Back to Home"
          hr_ []
          case page of
            Page_Index srcs ->
              div_ $ forM_ srcs $ \src -> with li_ [class_ "links"] $ do
                let meta = getMeta src
                b_ $ with a_ [href_ (Rib.sourceUrl src)] $ toHtml $ title meta
                maybe mempty (M.render . either (error . T.unpack) id . M.parsePure "<desc>") $ description meta
            Page_Single src ->
              with article_ [class_ "post"] $ do
                h1_ $ toHtml $ title $ getMeta src
                M.render $ Rib.sourceVal src
    -- Get metadata from Markdown YAML block
    getMeta :: Source M.MMark -> SrcMeta
    getMeta src = case M.projectYaml (Rib.sourceVal src) of
      Nothing -> error "No YAML metadata"
      Just val -> case fromJSON val of
        Aeson.Error e -> error $ "JSON error: " <> e
        Aeson.Success v -> v
    -- Define your site CSS here
    pageStyle :: Css
    pageStyle = "div#thesite" ? do
      margin (em 4) (pc 20) (em 1) (pc 20)
      "li.links" ? do
        listStyleType none
        marginTop $ em 1
        "b" ? fontSize (em 1.2)
        "p" ? sym margin (px 0)

(View full Main.hs at rib-sample)

Getting Started

The easiest way to get started with Rib is to use the template repository, rib-sample, from Github.

Concepts

Directory structure

Let's look at what's in the template repository:

$ git clone https://github.com/srid/rib-sample.git mysite
...
$ cd mysite
$ ls -F
a/  default.nix  Main.hs  README.md  rib-sample.cabal

The three key items here are:

  1. Main.hs: Haskell source containing the DSL of the HTML/CSS of your site.
  2. a/: The source content (eg: Markdown sources and static files)
  3. b/: The target directory, excluded from the git repository, will contain generated content (i.e., the HTML files, and copied over static content)

The template repository comes with a few sample posts under a/, and a basic HTML layout and CSS style defined in Main.hs.

Run the site

Now let's run them all.

Clone the sample repository locally, install Nix and run your site as follows:

nix-shell --run 'ghcid -T main'

(Note even though the author recommends it Nix is strictly not required; you may simply run ghcid -T main instead of the above command if you do not wish to use Nix.)

Running this command gives you a local HTTP server at http://localhost:8080/ (serving the generated files) that automatically reloads when either the content (a/) or the HTML/CSS/build-actions (Main.hs) changes. Hot reload, in other words.

How Rib works

How does the aforementioned nix-shell command work?

  1. nix-shell will run the given command in a shell environment with all of our dependencies (notably the Haskell ones including the rib library itself) installed.

  2. ghcid will compile your Main.hs and run its main function.

  3. Main.hs:main in turn calls Rib.App.run which takes as argument your custom Shake action that will build the static site.

  4. Rib.App.run: this parses the CLI arguments and runs the rib CLI "app" which can be run in one of a few modes --- generating static files, watching the a/ directory for changes, starting HTTP server for the b/ directory. By default---without any explicit arguments---this will run the Shake build action passed as argument on every file change and spin up a HTTP server.

Run that command, and visit http://localhost:8080 to view your site.

Editing workflow

Now try making some changes to the content, say a/first-post.md. You should see it reflected when you refresh the page. Or change the HTML or CSS of your site in Main.hs; this will trigger ghcid to rebuild the Haskell source and restart the server.

What's next?

Great, by now you should have your static site generator ready and running! What more can you do? Surely you may have specific needs; and this usually translates to running custom Shake actions during the build. Rib provides helper functions in Rib.Shake to make this easier.

Rib recommends writing your Shake actions in the style of being forward-defined which adds to the simplicity of the entire thing.

Examples