Skip to content

Commit

Permalink
Add raw keyword argument to filesource function, which defaults to Fa…
Browse files Browse the repository at this point in the history
…lse.

Update Python packages.
  • Loading branch information
yaph committed Mar 9, 2017
1 parent b8189d7 commit b9900aa
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
7 changes: 0 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ install: clean
uninstall:
pip uninstall -y logya

reinstall: uninstall
make install

lint:
flake8 logya tests

Expand Down Expand Up @@ -83,7 +80,3 @@ dist: clean
python setup.py sdist
python setup.py bdist_wheel
ls -l dist

# Reinstall and test shortcut
rt: reinstall
make test-all
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changes
4.0.0 to 4.1.0
--------------

* Added raw keyword argument to filesource function, which defaults to False.
* Added keep option for `generate` command, which does not remove an existing deploy directory.
* Load newer versions of bootstrap, fontawesome and jquery via CDN.
* Set meta noindex,follow for index pages in starter site.
Expand Down
10 changes: 9 additions & 1 deletion docs/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The template engine that comes with Logya is `jinja2 <http://jinja.pocoo.org/>`_
filesource
----------

You can use the ``filesource`` function to include the text of an external file on a page. The optional ``limit`` parameter specifies how many lines to include, if not provided the whole file will be included. The file content is escaped, so that you can display HTML or other source code. The example below is taken from the `d3.geomap documentation <http://d3-geomap.github.io/>`_.
You can use the ``filesource`` function to include the text of an external file on a page. The optional ``limit`` parameter specifies how many lines to include, if not provided the whole file will be included. The file content is escaped by default, so that you can display HTML or other source code. The example below is taken from the `d3.geomap documentation <http://d3-geomap.github.io/>`_.

::

Expand All @@ -23,6 +23,14 @@ This function is mainly intended for documentation purposes as it allows you to

<pre>{{ filesource(data, lines=6) }}...</pre>

To not escape the content, you can set raw to True.

::

<pre>{{ filesource(data, raw=True) }}...</pre>

This can be used for example to inline SVG code when using SVG sprites.

get_doc
-------

Expand Down
14 changes: 10 additions & 4 deletions logya/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
from jinja2 import Environment, BaseLoader, TemplateNotFound, escape


def filesource(logya_inst, name, lines=None):
def filesource(logya_inst, name, lines=None, raw=False):
"""Read and return source of text files.
A template function that reads the source of the given file and returns it.
The text is escaped so it can be rendered safely on a Web page.
The content is escaped by default so it can be rendered safely on a Web page.
The lines keyword argument is used to limit the number of lines returned.
To not escape the content you can set the raw keyword argument to False.
A use case is for documentation projects to show the source code used
to render the current example.
"""
Expand All @@ -22,6 +25,9 @@ def filesource(logya_inst, name, lines=None):
content = f.read()
else:
content = ''.join(f.readlines()[:lines])
if raw:
return content

return escape(content)


Expand Down Expand Up @@ -54,8 +60,8 @@ def __init__(self, logya_inst):
self.env.trim_blocks = True

# Add filesource global to allow for including the source of a file.
self.env.globals['filesource'] = lambda x, lines=None: filesource(
logya_inst, x, lines=lines)
self.env.globals['filesource'] = lambda x, lines=None, raw=False: filesource(
logya_inst, x, lines=lines, raw=raw)

self.env.globals['get_doc'] = lambda x: get_doc(logya_inst, x)

Expand Down
3 changes: 3 additions & 0 deletions requirements-base.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Jinja2
Markdown
PyYAML
11 changes: 8 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Jinja2==2.8
Markdown==2.6.6
PyYAML==3.11
appdirs==1.4.3
Jinja2==2.9.5
Markdown==2.6.8
MarkupSafe==1.0
packaging==16.8
pyparsing==2.2.0
PyYAML==3.12
six==1.10.0

0 comments on commit b9900aa

Please sign in to comment.