doctemplates 0.7 → 0.7.1
raw patch · 9 files changed
+141/−17 lines, 9 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Text.DocTemplates.Internal: Chomp :: Filter
- Text.DocTemplates.Internal: Partial :: Template a -> Template a
+ Text.DocTemplates.Internal: Partial :: [Filter] -> Template a -> Template a
Files
- README.md +12/−4
- changelog.md +79/−0
- doctemplates.cabal +1/−1
- src/Text/DocTemplates.hs +10/−5
- src/Text/DocTemplates/Internal.hs +11/−2
- src/Text/DocTemplates/Parser.hs +5/−3
- test/filter-and-partial.test +16/−0
- test/filters.test +7/−1
- test/keyval.txt +0/−1
README.md view
@@ -350,10 +350,9 @@ ## Filters -A filter transforms the value of a variable. Filters are-specified using a slash (`/`) between the variable name and-the filter name. They may go anywhere a variable can go.-Example:+A filter transforms the value of a variable or partial. Filters+are specified using a slash (`/`) between the variable name (or+partial) and the filter name. Example: ``` $for(name)$@@ -365,6 +364,12 @@ $endfor$ ``` +Filters may also be applied to the results of partials:++```+$employee:name()/uppercase$+```+ Filters may be chained: ```@@ -402,6 +407,9 @@ - `reverse`: Reverses a textual value or array, and has no effect on other values.++- `chomp`: Removes trailing newlines (and breakable space)+ from a textual value, and has no effect on other values. - `alpha`: Converts a textual value that can be read as an integer into a lowercase alphabetic
changelog.md view
@@ -1,5 +1,84 @@ # doctemplates +## 0.7.1++ * Add `chomp` filter.++ * Allow filters to be applied to output of partials.++## 0.7++ * Add haddock Makefile target, which regenerates haddocks from README+ and tests the code example.++ * Remove `BreakingSpace` constructor on `Template`.+ Now we use doclayout `BreakingSpace` inside a `Literal`.++ * Add instance for `ToContext a (Doc a)`.++ * Get benchmarks compiling again.++ * Use (doclayout) `Doc` internally and for rendered output.++ + `TemplateTarget` is now a type constraint synonym, not a regular+ typeclass.+ + Constraint on `compileTemplate` and `applyTemplate`+ simplified using TemplateTarget.+ + DocTemplates reexports Text.DocLayout.Doc.+ + The `Literal` costructor of `Template` now takes a `Doc a`+ rather than an `a`.+ + The `SimpleVal` constructor of `Val` now takes a `Doc a`+ rather than an `a`.+ + `renderTemplate` now returns a `Doc a` rather than an `a`.+ (This value can be converted to an a using `render Nothing`.)++ * Remove fromText from `TemplateTarget`. Now we use `fromString`+ from Data.String.++ * Parameterize `Template` on underlying stringlike type.++ * Improved behavior of partials.++ * Improve indent functions: don't drop final newline.++ * Allow blank lines in nested section.++ * Indent for Text/String: don't indent empty lines.++ * Additional tests and documentation about nesting.++ * Render items in for loop before separator.+ Otherwise we throw off column calculation.++ * Remove `+-reflow`; replace with toggle `$~$`.++ * Remove pNewline parser; it isn't needed now.++ * Remove `+-nest`.++ * Fix nest parsing bug.++ * Improve nesting.++ + Change `Nested` constructor for `Template` so it doesn't take+ a parameter.+ + Nesting level is now determined dynamically at render time+ rather than at compile time. This gives much better results+ when nesting occurs after template directives.+ Benchmarks show a slight penalty in performance (from 3.5ms to 3.1ms+ in rendering), but it's not too much.++ * Add filters. Filters transform the value of a variable, e.g. changing+ a map into an array of key/value pairs. Closes #5.++ + Internal: Add `Filter` type and `[Filter]` parameter on `Variable`.+ + Remove `unVariable`; now we have `varParts` and `varFilters`.+ + Document filters in README.md.+ + Implement filters.+ + Add tests.++ * Add `ToYAML`, `FromYAML` instances for `Context`, `Val`.+ ## 0.6.2 * Remove unnecessary `TemplateTarget` constraints on
doctemplates.cabal view
@@ -1,5 +1,5 @@ name: doctemplates-version: 0.7+version: 0.7.1 synopsis: Pandoc-style document templates description: This is the text templating system used by pandoc. It supports variable interpolation, iteration,
src/Text/DocTemplates.hs view
@@ -306,9 +306,9 @@ == Filters -A filter transforms the value of a variable. Filters are specified using-a slash (@\/@) between the variable name and the filter name. They may-go anywhere a variable can go. Example:+A filter transforms the value of a variable or partial. Filters are+specified using a slash (@\/@) between the variable name (or partial)+and the filter name. Example: > $for(name)$ > $name/uppercase$@@ -318,6 +318,10 @@ > - $it.key$: $it.value$ > $endfor$ +Filters may also be applied to the results of partials:++> $employee:name()/uppercase$+ Filters may be chained: > $for(employees/pairs)$@@ -350,6 +354,9 @@ - @reverse@: Reverses a textual value or array, and has no effect on other values. +- @chomp@: Removes trailing newlines (and breakable space) from a+ textual value, and has no effect on other values.+ - @alpha@: Converts a textual value that can be read as an integer into a lowercase alphabetic character @a..z@ (mod 26), and has no effect on other values. This can be used to get lettered enumeration@@ -375,8 +382,6 @@ - @center n \"leftborder\" \"rightborder\"@: Renders a textual value in a block of width @n@, aligned to the center, and has no effect on other values.-- -}
src/Text/DocTemplates/Internal.hs view
@@ -70,7 +70,7 @@ | Conditional Variable (Template a) (Template a) | Iterate Variable (Template a) (Template a) | Nested (Template a)- | Partial (Template a)+ | Partial [Filter] (Template a) | Literal (Doc a) | Concat (Template a) (Template a) | Empty@@ -92,6 +92,7 @@ | ToLowercase | ToLength | Reverse+ | Chomp | ToAlpha | ToRoman | Block Alignment Int Border@@ -315,6 +316,10 @@ traverse (pure . fromText . T.reverse . toText) d ListVal xs -> ListVal (reverse xs) _ -> val+applyFilter Chomp val =+ case val of+ SimpleVal d -> SimpleVal $ DL.chomp d+ _ -> val applyFilter ToAlpha val = case val of SimpleVal (DL.Text _ t) ->@@ -439,7 +444,11 @@ renderTemp (Nested t) ctx = do n <- S.get DL.nest n <$> renderTemp t ctx-renderTemp (Partial t) ctx = renderTemp t ctx+renderTemp (Partial fs t) ctx = do+ val' <- renderTemp t ctx+ return $ case applyFilters fs (SimpleVal val') of+ SimpleVal x -> x+ _ -> mempty renderTemp (Concat t1 t2) ctx = mappend <$> renderTemp t1 ctx <*> renderTemp t2 ctx renderTemp Empty _ = return mempty
src/Text/DocTemplates/Parser.hs view
@@ -253,7 +253,7 @@ go (Iterate w t1 t2) = Iterate (reletter v w) (changeToIt v t1) (changeToIt v t2) go (Concat t1 t2) = changeToIt v t1 <> changeToIt v t2- go (Partial t) = Partial t -- don't reletter inside partial+ go (Partial fs t) = Partial fs t -- don't reletter inside partial go (Nested t) = Nested (go t) go x = x reletter (Variable vs _fs) (Variable ws gs) =@@ -349,9 +349,10 @@ P.setPosition oldPos return res' P.putState oldst+ fs <- many pFilter case mbvar of- Just var -> return $ Iterate var t separ- Nothing -> return $ Partial t+ Just var -> return $ Iterate var (Partial fs t) separ+ Nothing -> return $ Partial fs t removeFinalNewline :: Text -> Text removeFinalNewline t =@@ -414,6 +415,7 @@ "alpha" -> return ToAlpha "roman" -> return ToRoman "reverse" -> return Reverse+ "chomp" -> return Chomp "left" -> Block LeftAligned <$> pBlockWidth <*> pBlockBorders "right" -> Block RightAligned <$> pBlockWidth <*> pBlockBorders "center" -> Block Centered <$> pBlockWidth <*> pBlockBorders
+ test/filter-and-partial.test view
@@ -0,0 +1,16 @@+{ "employee":+ [ { "name": { "first": "John", "last": "Doe" } }+ , { "name": { "first": "Omar", "last": "Smith" }+ , "salary": "30000" }+ , { "name": { "first": "Sara", "last": "Chen" }+ , "salary": "60000" }+ ]+}+.+$for(employee)$+$it:name()/uppercase$+$endfor$+.+(JOHN) DOE+(OMAR) SMITH+(SARA) CHEN
test/filters.test view
@@ -6,7 +6,8 @@ { "sax": 2 } ], "items": [ "one with\na line break", "two",- "three with\na line break" ]+ "three with\na line break" ],+ "hasblanks": "hello\n\n" } . $bar/length$@@ -32,6 +33,8 @@ $endfor$ $items/pairs/reverse:enum()$++($hasblanks/chomp$) . 0 2@@ -59,3 +62,6 @@ B. two A. one with a line break+++(hello)
− test/keyval.txt
@@ -1,1 +0,0 @@-