Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changed data: field to allow data to be specified directly.

data:
   deadline:  11/20/2009
   tasks: from tasks.yaml order by date desc

In this example, deadline is given a value directly,
while tasks takes its value from tasks.yaml.
  • Loading branch information...
commit 6d9bb30d672634576c0210845f6b1ea2bf66ba2b 1 parent aed5ad1
@jgm authored
View
28 README.markdown
@@ -51,7 +51,7 @@ what went into `index.html`. The file is a YAML list of YAML hashes
template : index.st
requires : event.st
data :
- recentevents : events.yaml ORDER BY date DESC LIMIT 2
+ recentevents : FROM events.yaml ORDER BY date DESC LIMIT 2
This says: build the page `index.html` from the string template `index.st`
(and subtemplate `event.st`) and data from `events.yaml`. Sort this data
@@ -220,8 +220,8 @@ although they may, and the query doesn't have to end with a semicolon,
though it may):
data:
- events: events.yaml order by date desc group by title then location
- people: people.csv order by birthday then lastname where
+ events: from events.yaml order by date desc group by title then location
+ people: from people.csv order by birthday then lastname where
birthstate = 'CA' limit 5
First we have the name of the stringtemplate attribute to be populated
@@ -269,6 +269,14 @@ Note that the order of transformations is significant. You can get
different results if you use `LIMIT` before or after `ORDER BY`,
for example.
+If you want to specify an attribute's value directly, rather than
+reading it from a file, just omit the "FROM":
+
+ date:
+ deadline: 11/20/2009
+
+Any YAML value can be given to an attribute in this way.
+
### Static files
Any file or subdirectory in the `files` directory (or whatever is
@@ -404,7 +412,7 @@ If you like, you can use a CSV file instead of YAML for your data source.
Just give it the extension `.csv`. In `index.yaml`, you'd have:
data:
- events: events.csv order by date desc
+ events: from events.csv order by date desc
This can be handy if you're using existing data, because spreadsheets
and databases can easily be dumped to CSV. In the case of a SQL
@@ -440,17 +448,7 @@ date in a list of dates: not what you want.
The solution is to set up your text editor so that it doesn't
add the newline at the end of the file. Using [vim][], you can do this
-with the commands `set binary` and `set noeol`. To make this the
-default for `.st` files, add this line to `augroup filetypedetect`
-in `~\.vim\filetype.vim`:
-
- au! BufRead,BufNewFile *.st setfiletype stringtemplate
-
-Then add a file `stringtemplate.st` in `~/.vim/ftplugin` with
-the following contents:
-
- set binary
- set noeol
+with the commands `set binary` and `set noeol`.
### Layout templates
View
6 Yst/Build.hs
@@ -22,7 +22,7 @@ import Yst.Types
import Yst.Util
import Yst.Render
import qualified Data.Map as M
-import Data.Maybe (fromMaybe)
+import Data.Maybe (fromMaybe, mapMaybe)
import Data.List
import System.FilePath
import System.Directory
@@ -44,7 +44,9 @@ dependencies site url =
case sourceFile page of
TemplateFile f -> stripStExt f <.> "st"
SourceFile f -> f
- dataFiles = map (\(_,(f,_)) -> dataDir site </> f) $ pageData page
+ fileFromSpec (DataFromFile f _) = Just f
+ fileFromSpec _ = Nothing
+ dataFiles = map (dataDir site </>) $ mapMaybe (\(_,s) -> fileFromSpec s) $ pageData page
in indexFile site : layout : srcdir : (requires ++ dataFiles)
buildSite :: Site -> IO ()
View
22 Yst/Data.hs
@@ -26,15 +26,16 @@ import Yst.CSV
import Control.Monad
import Data.Char
import Data.Maybe (fromMaybe)
-import Data.List (sortBy, nub)
+import Data.List (sortBy, nub, isPrefixOf)
import Text.ParserCombinators.Parsec
import System.FilePath (takeExtension)
-getData :: (FilePath, [DataOption]) -> IO Node
-getData (file, opts) = do
+getData :: DataSpec -> IO Node
+getData (DataFromFile file opts) = do
raw <- catch (readDataFile file)
(\e -> errorExit 15 ("Error reading data from " ++ file ++ ": " ++ show e) >> return undefined)
return $ foldl applyDataOption raw opts
+getData (DataConstant n) = return n
readDataFile :: FilePath -> IO Node
readDataFile f =
@@ -97,14 +98,19 @@ reverseIfDescending Descending EQ = EQ
reverseIfDescending Descending LT = GT
reverseIfDescending Descending GT = LT
-parseDataField :: Node -> (FilePath, [DataOption])
-parseDataField (NString s) = case parse pDataField s s of
- Right res -> res
- Left err -> error $ show err
-parseDataField x = error $ "Expected string node, got " ++ show x
+parseDataField :: Node -> DataSpec
+parseDataField n@(NString s) = case parse pDataField s s of
+ Right (f,opts) -> DataFromFile f opts
+ Left err -> if "from" `isPrefixOf` (dropWhile isSpace $ map toLower s)
+ then error $ "Error parsing data field: " ++ show err
+ else DataConstant n
+parseDataField n = DataConstant n
pDataField :: GenParser Char st ([Char], [DataOption])
pDataField = do
+ spaces
+ pString "from"
+ pSpace
fname <- pIdentifier <?> "name of YAML or CSV file"
opts <- many $ (pOptWhere <?> "where [CONDITION]")
<|> (pOptLimit <?> "limit [NUMBER]")
View
6 Yst/Types.hs
@@ -42,7 +42,7 @@ data Source = TemplateFile FilePath
deriving (Show, Read, Eq)
data Page = Page {
- pageData :: [(String, (FilePath, [DataOption]))]
+ pageData :: [(String, DataSpec)]
, layoutFile :: Maybe FilePath
, sourceFile :: Source
, requiresFiles :: [FilePath]
@@ -74,6 +74,10 @@ instance Ord Node where
compare (NDate _) _ = GT
compare _ _ = GT
+data DataSpec = DataConstant Node
+ | DataFromFile FilePath [DataOption]
+ deriving (Show, Read, Eq)
+
data DataOption = OrderBy [(String, SortDirection)]
| GroupBy [String]
| Where FilterCond
View
6 demo/index.yaml
@@ -3,21 +3,21 @@
template : index.st
requires : event.st
data :
- recentevents : events.yaml ORDER BY date DESC LIMIT 2
+ recentevents : FROM events.yaml ORDER BY date DESC LIMIT 2
- url : events.html
title : Events
template : events.st
requires : [event.st, eventgroup.st, date.st]
data :
- eventsbydate : events.yaml ORDER BY date DESC GROUP BY date
+ eventsbydate : FROM events.yaml ORDER BY date DESC GROUP BY date
- url : april_events.tex
title : April Events
template : april_events.st
requires : event.st
data :
- april : events.yaml WHERE date >= '2009-04-01' AND date < '2009-05-01' ORDER BY date
+ april : FROM events.yaml WHERE date >= '2009-04-01' AND date < '2009-05-01' ORDER BY date
layout : layout.tex.st
inmenu : no
Please sign in to comment.
Something went wrong with that request. Please try again.