Skip to content

Commit

Permalink
Adds numRssEntries option to limit rss feed size.
Browse files Browse the repository at this point in the history
  • Loading branch information
gradha committed Apr 6, 2014
1 parent 221d1c6 commit 7791370
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ipsum.nim
Expand Up @@ -247,6 +247,11 @@ proc generateTagPages(meta: seq[TArticleMetadata], cfg: TConfig) =
tag.addFileExt("html"), output)

proc generateAtomFeed(meta: seq[TArticleMetadata], cfg: TConfig) =
# Prunes the article sequence according to the configuration limit.
assert cfg.numRssEntries >= 0
var meta = meta
if meta.len > cfg.numRssEntries:
meta.delete(cfg.numRssEntries + 1, <meta.len)
let feed = renderAtom(meta, cfg.title, cfg.url, joinUrl(cfg.url, "feed.xml"),
cfg.author)
writeFile(getCurrentDir() / outputDir / "feed.xml", feed)
Expand Down
7 changes: 6 additions & 1 deletion readme.md
Expand Up @@ -91,7 +91,12 @@ author = "Your Name"
```

The information from this config is also available in your templates, the key
names match the names in the config file.
names match the names in the config file. Additional options you can add to the
``ipsum.ini`` file:

* ``numRssEntries`` - Integer with the maximum number of generated entries in
the rss feed. If you don't specify a value for this, the value ``10`` will be
used by default.

Once you're done with the setup, simply execute ``ipsum`` and your blog should
be generated before you can even blink!
Expand Down
9 changes: 8 additions & 1 deletion src/config.nim
Expand Up @@ -5,11 +5,13 @@ type
title*: string
url*: string
author*: string
numRssEntries*: int

proc initConfig(): TConfig =
result.title = ""
result.url = ""
result.author = ""
result.numRssEntries = 10

proc validateConfig(config: TConfig) =
template ra(field: string) =
Expand All @@ -21,6 +23,9 @@ proc validateConfig(config: TConfig) =
ra("author")
if config.url == "":
ra("url")
if config.numRssEntries < 0:
raise newException(EInvalidValue,
"The numRssEntries value can't be negative.")

proc parseConfig*(filename: string): TConfig =
if not filename.existsFile:
Expand All @@ -35,13 +40,15 @@ proc parseConfig*(filename: string): TConfig =
of cfgSectionStart:
raise newException(EInvalidValue, "No sections supported.")
of cfgKeyValuePair, cfgOption:
case ev.key
case ev.key.normalize
of "title":
result.title = ev.value
of "url":
result.url = ev.value
of "author":
result.author = ev.value
of "numrssentries":
result.numRssEntries = ev.value.parseInt
of cfgError:
raise newException(EInvalidValue, ev.msg)
of cfgEof:
Expand Down

0 comments on commit 7791370

Please sign in to comment.