Skip to content

Commit cbe4534

Browse files
authored
✨ Add support for get_template_from_string() (#4)
* ✨ Add support for get_template_from_string() With this addition, moban-slim will be compatible with 0.3.8. Also accepts extensions in the engine constructor. Closes #1 * 🔨 Refactor code and add changelog * ⚡ Return to the main slimish-jinja release * 🔥 Remove outdated tests * 💚 Fix unit test * 📚 Update changelog with new release date and issues addressed * ⚡ Add quick start example and personalized action text
1 parent 62e5f71 commit cbe4534

20 files changed

+204
-49
lines changed

Diff for: .moban.d/CUSTOM_README.rst.jj2

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
{%endblock%}
55

66
{%block features%}
7-
With `slimish-jinja2 for Python 3 <git+https://github.com/cliu13/slimish-jinja2@python3>`_, this library allow moban users to
8-
have slim template in their next documentation endeavour.
7+
With `slimish-jinja2 for Python 3 <slimish-jinja>=1.1.0>`_, this library allow moban users to
8+
have slim template in their next documentation endeavour.
99
{%endblock%}

Diff for: .moban.d/custom_travis.yml.jj2

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends 'travis.yml.jj2' %}
2+
3+
{%block custom_python_versions%}
4+
python:
5+
- 3.8
6+
- 3.7
7+
- 3.6
8+
{%endblock%}

Diff for: .moban.d/slim_example.rst.jj2

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Given a data.json file with the following content
2+
3+
.. code-block::
4+
5+
{
6+
"person": {
7+
"firstname": "Smith",
8+
"lastname": "Jones",
9+
},
10+
}
11+
12+
.. code-block:: bash
13+
14+
{% raw %}
15+
$ moban --template-type slim -c data.json "{{person.firstname}} {{person.lastname}}"
16+
Slimming <p>{{first... to moban.output
17+
Slimmed 1 file.
18+
$ cat moban.output
19+
Smith Jones
20+
{% endraw %}

Diff for: .moban.d/tests/custom_requirements.txt.jj2

+2
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
moban
55
black;python_version>="3.6"
66
isort;python_version>="3.6"
7+
slimish-jinja>=1.1.0
8+
jinja2-fsloader>=0.2.0
79
{%endblock%}

Diff for: .moban.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ configuration:
88
targets:
99
- README.rst: CUSTOM_README.rst.jj2
1010
- setup.py: custom_setup.py.jj2
11-
- requirements.txt: requirements.txt.jj2
1211
- "tests/requirements.txt": "tests/custom_requirements.txt.jj2"
1312
- "docs/source/conf.py": "docs/conf.py_t"
14-
- test.sh: test.script.jj2
1513
- "moban_slim/_version.py": "_version.py.jj2"
1614
- .gitignore: gitignore.jj2
17-
- .travis.yml: travis.yml.jj2
18-
- Pipfile: Pipfile.jj2
15+
- .travis.yml: custom_travis.yml.jj2
16+
- output: CHANGELOG.rst
17+
configuration: changelog.yml
18+
template: CHANGELOG.rst.jj2

Diff for: .travis.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ language: python
33
notifications:
44
email: false
55
python:
6-
- pypy-5.3.1
7-
- 3.7-dev
6+
- 3.8
7+
- 3.7
88
- 3.6
9-
- 3.5
10-
- 3.4
11-
- 2.7
129
before_install:
1310
- if [[ $TRAVIS_PYTHON_VERSION == "2.6" ]]; then pip install flake8==2.6.2; fi
1411
- if [[ -f min_requirements.txt && "$MINREQ" -eq 1 ]]; then

Diff for: CHANGELOG.rst

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
11
Change log
2-
===========
2+
================================================================================
3+
4+
0.0.2 - 16-06-2020
5+
--------------------------------------------------------------------------------
6+
7+
Updated
8+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
10+
#. `#1 <https://github.com/moremoban/moban-slim/issues/1>`_: support moban
11+
v0.3.8
12+
#. `#3 <https://github.com/moremoban/moban-slim/issues/3>`_: support moban
13+
v0.3.9
14+
#. `#5 <https://github.com/moremoban/moban-slim/issues/5>`_: support moban
15+
v.0.4.1
16+
#. `#6 <https://github.com/moremoban/moban-slim/issues/6>`_: support moban
17+
v6.0.0
18+
19+
0.0.1 - 30-11-2018
20+
--------------------------------------------------------------------------------
21+
22+
First release
23+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
#. render slim files for moban

Diff for: Pipfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ name = 'pypi'
77
python_version= '3.6'
88

99
[packages]
10-
git+https://github.com/cliu13/slimish-jinja2@python3
10+
slimish-jinja>=1.1.0
1111
lml>=0.0.7
12+
jinja2-fsloader>=0.2.0

Diff for: README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ moban-slim
1010

1111

1212

13-
With `slimish-jinja2 for Python 3 <git+https://github.com/cliu13/slimish-jinja2@python3>`_, this library allow moban users to
14-
have slim template in their next documentation endeavour.
13+
With `slimish-jinja2 for Python 3 <slimish-jinja>=1.1.0>`_, this library allow moban users to
14+
have slim template in their next documentation endeavour.
1515

1616
Installation
1717
================================================================================

Diff for: changelog.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: moban-slim
2+
organisation: moremoban
3+
releases:
4+
- changes:
5+
- action: Updated
6+
details:
7+
- "`#1`: support moban v0.3.8"
8+
- "`#3`: support moban v0.3.9"
9+
- "`#5`: support moban v.0.4.1"
10+
- "`#6`: support moban v6.0.0"
11+
date: 16-06-2020
12+
version: 0.0.2
13+
- changes:
14+
- action: First release
15+
details:
16+
- render slim files for moban
17+
date: 30-11-2018
18+
version: 0.0.1

Diff for: moban-slim.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ organisation: "moremoban"
33
author: "Charlie Liu, C.W et al"
44
55
company: "Moremoban members and its contributors"
6-
version: "0.0.1dev"
7-
current_version: "0.0.1dev"
8-
release: "0.0.1dev"
9-
copyright_year: 2018
6+
version: "0.0.2"
7+
current_version: "0.0.2"
8+
release: "0.0.2"
9+
copyright_year: 2018-2020
1010
license: MIT
1111
dependencies:
12-
- git+https://github.com/cliu13/slimish-jinja2@python3
12+
- slimish-jinja>=1.1.0
1313
- lml>=0.0.7
1414
description: "Provide slim templating capability to moban."

Diff for: moban_slim/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "0.0.1dev"
1+
__version__ = "0.0.2"
22
__author__ = "Charlie Liu, C.W et al"

Diff for: moban_slim/engine.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
import codecs
1+
from typing import Dict
22

3-
from jinja2 import Environment, FileSystemLoader
3+
from jinja2 import Environment, FileSystemLoader, Template
4+
from moban.externals import file_system
45
from slimish_jinja import SlimishExtension
5-
import moban.utils as utils
6+
7+
from jinja2_fsloader import FSLoader
68

79

810
class EngineSlim(object):
9-
def __init__(self, template_dirs):
10-
self.template_dirs = template_dirs
11+
ACTION_IN_PRESENT_CONTINUOUS_TENSE = "Slimming"
12+
ACTION_IN_PAST_TENSE = "Slimmed"
13+
14+
def __init__(self, template_fs, options: Dict = None):
15+
self.template_fs = template_fs
1116
self.jj2_env = Environment(
12-
loader=FileSystemLoader(template_dirs),
13-
extensions=[SlimishExtension]
17+
loader=FSLoader(template_fs), extensions=[SlimishExtension],
1418
)
1519
self.jj2_env.slim_debug = True
1620

17-
def get_template(self, template_file):
21+
def get_template(self, template_file: str):
22+
template_file = file_system.to_unicode(template_file)
1823
template = self.jj2_env.get_template(template_file)
1924
return template
2025

21-
def apply_template(self, template, data, _):
26+
def get_template_from_string(self, string: str):
27+
string = file_system.to_unicode(string)
28+
return Template(string)
29+
30+
def apply_template(self, template, data: Dict, _):
2231
rendered_content = template.render(**data)
2332
return rendered_content

Diff for: requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
git+https://github.com/cliu13/slimish-jinja2@python3
1+
slimish-jinja>=1.1.0
22
lml>=0.0.7
3+
jinja2-fsloader>=0.2.0

Diff for: run_nose.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import nose
2-
nose.run()
2+
3+
nose.run()

Diff for: setup.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
NAME = 'moban-slim'
1515
AUTHOR = 'Charlie Liu, C.W et al'
16-
VERSION = '0.0.1'
16+
VERSION = '0.0.2'
1717
1818
LICENSE = 'MIT'
1919
DESCRIPTION = (
2020
'Provide slim templating capability to moban.'
2121
)
2222
URL = 'https://github.com/moremoban/moban-slim'
23-
DOWNLOAD_URL = '%s/archive/0.0.1.tar.gz' % URL
23+
DOWNLOAD_URL = '%s/archive/0.0.2.tar.gz' % URL
2424
FILES = ['README.rst', 'CONTRIBUTORS.rst', 'CHANGELOG.rst']
2525
KEYWORDS = [
2626
'python',
@@ -39,8 +39,9 @@
3939
]
4040

4141
INSTALL_REQUIRES = [
42-
'git+https://github.com/cliu13/slimish-jinja2@python3',
42+
'slimish-jinja>=1.1.0',
4343
'lml>=0.0.7',
44+
'jinja2-fsloader>=0.2.0',
4445
]
4546
SETUP_COMMANDS = {}
4647

@@ -51,8 +52,8 @@
5152
# You do not need to read beyond this line
5253
PUBLISH_COMMAND = '{0} setup.py sdist bdist_wheel upload -r pypi'.format(
5354
sys.executable)
54-
GS_COMMAND = ('gs moban-slim v0.0.1 ' +
55-
"Find 0.0.1 in changelog for more details")
55+
GS_COMMAND = ('gs moban-slim v0.0.2 ' +
56+
"Find 0.0.2 in changelog for more details")
5657
NO_GS_MESSAGE = ('Automatic github release is disabled. ' +
5758
'Please install gease to enable it.')
5859
UPLOAD_FAILED_MSG = (

Diff for: test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pip freeze
2-
nosetests --with-coverage --cover-package moban_slim --cover-package tests tests docs/source moban_slim && flake8 . --exclude=.moban.d,docs --builtins=unicode,xrange,long
2+
nosetests --with-coverage --cover-package moban_slim --cover-package tests tests docs/source moban_slim

Diff for: tests/fixtures/slim_tests/expected_output_2.txt

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>
5+
6+
Slimish-Jinja Example
7+
8+
</title>
9+
<meta content="template language" name="keywords" />
10+
<script>
11+
12+
</script>
13+
</head>
14+
<body id="home" class="fluid liquid">
15+
<h1>
16+
This is my header.
17+
</h1>
18+
<div id="contents" class="main">
19+
<div> </div>
20+
<p>Dynamic my_content</p>
21+
<p>
22+
Nested dyanmic my_content
23+
Left indent is preserved in text blocks.
24+
</p>
25+
<p>
26+
<a href="http://www.google.com">Google</a>
27+
</p>
28+
</div>
29+
<ul class="my_class">
30+
31+
<li>foo</li>
32+
33+
<li>bar</li>
34+
35+
36+
<li>bar</li>
37+
38+
<li>baz</li>
39+
40+
41+
<li>baz</li>
42+
43+
44+
</ul>
45+
</body>
46+
</html>

Diff for: tests/requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ flake8
66
moban
77
black;python_version>="3.6"
88
isort;python_version>="3.6"
9-
git+https://github.com/cliu13/slimish-jinja2@python3
9+
slimish-jinja>=1.1.0
10+
jinja2-fsloader>=0.2.0

Diff for: tests/test_slim_engine.py

+37-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
11
import os
22

3+
from moban.core import ENGINES, plugins
34
from nose.tools import eq_
4-
from moban.plugins import ENGINES, BaseEngine
5+
56
from moban_slim.engine import EngineSlim
67

8+
plugins.make_sure_all_pkg_are_loaded()
9+
710

811
def test_slim_engine_type():
912
engine = ENGINES.get_engine("slim", [], "")
10-
assert engine.engine_cls == EngineSlim
11-
pass
13+
assert engine.engine.__class__.__name__ == "EngineSlim"
1214

1315

1416
def test_slim_file_tests():
1517
output = "test.txt"
1618
path = os.path.join("tests", "fixtures", "slim_tests")
17-
engine = BaseEngine([path], path, EngineSlim)
19+
engine = ENGINES.get_engine("slim", [path], path)
1820
engine.render_to_file("file_tests.slim", "file_tests.json", output)
1921
with open(output, "r") as output_file:
20-
expected_path = os.path.join("tests", "fixtures", "slim_tests",
21-
"expected_output.txt")
22+
# In some versions of python, the attributes of the
23+
# meta tag on line 9 of file_tests.slim are flipped.
24+
# To fix this, we check for both cases.
25+
expected_path = os.path.join(
26+
"tests", "fixtures", "slim_tests", "expected_output.txt"
27+
)
28+
expected_path_2 = os.path.join(
29+
"tests", "fixtures", "slim_tests", "expected_output_2.txt"
30+
)
2231
with open(expected_path) as expected_file:
23-
expected = expected_file.read()
24-
content = output_file.read()
25-
eq_(content, expected)
26-
os.unlink(output)
32+
with open(expected_path_2) as expected_file_2:
33+
expected = expected_file.read()
34+
expected_2 = expected_file_2.read()
35+
content = output_file.read()
36+
try:
37+
eq_(content, expected)
38+
except AssertionError:
39+
eq_(content, expected_2)
40+
os.unlink(output)
41+
42+
43+
def test_slim_string_template():
44+
string_template = "{{ content }}"
45+
output = "test.txt"
46+
path = os.path.join("tests", "fixtures", "slim_tests")
47+
engine = ENGINES.get_engine("slim", [path], path)
48+
engine.render_string_to_file(string_template, "file_tests.json", output)
49+
with open(output, "r") as output_file:
50+
expected = "my_content"
51+
content = output_file.read()
52+
eq_(content, expected)
53+
os.unlink(output)

0 commit comments

Comments
 (0)