Skip to content

Commit

Permalink
Allow pages to specify their own template.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgerdes authored and valderman committed Mar 17, 2017
1 parent 97830f7 commit 9420dcb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
15 changes: 13 additions & 2 deletions CCWF/Impl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Data.Char
import Data.List
import Data.Time
import Data.Function (on)
import Data.Maybe (isJust)
import Data.Maybe (isJust, fromMaybe)
import System.Directory (doesFileExist)
import System.FilePath (takeBaseName)
import Text.Read
Expand Down Expand Up @@ -205,15 +205,26 @@ mkMenuCtx self = mconcat
-- template (i.e. it may include substitutions), and render it as markdown
-- again. Finally, apply the default template to the end result and
-- relativize all URLs.
-- Pages may set a different template by setting the @template@ metadata
-- variable.
applyMeAsTemplate :: Context String -> Context String -> Compiler (Item String)
applyMeAsTemplate pageCtx topCtx = do
template <- getTemplate
getResourceBody
>>= renderPandoc
>>= applyAsTemplate pageCtx
>>= renderPandoc
>>= loadAndApplyTemplate "templates/default.html" topCtx
>>= loadAndApplyTemplate template topCtx
>>= relativizeUrls

-- | Get the template to use for the current page.
-- If no @template@ metadata is given, @default@ is assumed.
getTemplate :: Compiler Identifier
getTemplate = do
self <- getUnderlying
mtemp <- getMetadataField self "template"
return $ fromFilePath $ "templates/" ++ (fromMaybe "default" mtemp) ++ ".html"

-- | Turn a path @p@ (stripped of prefix and extension) into @p/index.html@.
mkPath :: FilePath -> FilePath
mkPath "index" = "index.html"
Expand Down
42 changes: 42 additions & 0 deletions templates/wide.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/css/highlighting.css">
<script src="/js/scripts.js"></script>
<title>$title$ | $coursename$ $year$</title>
<body>
<div id="widecontent" class="widecontent">
$body$
</div>
<div id="sidebar" class="sidebar">
<ul id="sidebarheading">
<h2>$coursename$ $year$</h2>
<p>$coursecode$</p>
</ul>
<ul id="menu">
$for(menuitems)$
<a href="$url$" class="$selected$">
<li class="$selected$">
$body$
$if(submenuitems)$
<ul class="submenu">
$for(submenuitems)$
<a class="submenu" class="unselected" href="$path$"><li>$body$</li></a>
$endfor$
</ul>
$endif$
</li>
</a>
$endfor$
</ul>
<img src="/images/chalmers.png" width="140px" style="margin-top: 40px;">
</div>
<div id="topbar" class="topbar" onclick="toggleSidebar('sidebar');">
<div>Menu</div> <img src="/images/menubutton.png"></img>
</div>
</body>
</head>
</html>

0 comments on commit 9420dcb

Please sign in to comment.