Skip to content

Commit 5f4deaf

Browse files
committed
first commit
0 parents  commit 5f4deaf

File tree

9 files changed

+354
-0
lines changed

9 files changed

+354
-0
lines changed

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: python
2+
python:
3+
- "2.7"
4+
- "3.4"
5+
- "3.5"
6+
before_install:
7+
- wget https://github.com/jgm/pandoc/releases/download/2.0.6/pandoc-2.0.6-1-amd64.deb
8+
- sudo dpkg -i pandoc-2.0.6-1-amd64.deb
9+
- pandoc -v
10+
install:
11+
- pip install coveralls
12+
script:
13+
- coverage run --source=pandoc_codeblock_include setup.py test
14+
after_success:
15+
- coveralls

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2018, Christophe Demko
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include README.md
2+
include LICENSE
3+
include MANIFEST.in
4+

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# pandoc-codeblock-include
2+
[![Build Status](https://img.shields.io/travis/chdemko/pandoc-codeblock-include/master.svg)](https://travis-ci.org/chdemko/pandoc-codeblock-include/branches)
3+
[![Coveralls](https://img.shields.io/coveralls/github/chdemko/pandoc-codeblock-include/master.svg)](https://coveralls.io/github/chdemko/pandoc-codeblock-include?branch=master)
4+
[![Scrutinizer](https://img.shields.io/scrutinizer/g/chdemko/pandoc-codeblock-include.svg)](https://scrutinizer-ci.com/g/chdemko/pandoc-codeblock-include/)
5+
[![PyPI version](https://img.shields.io/pypi/v/pandoc-codeblock-include.svg)](https://pypi.org/project/pandoc-codeblock-include/)
6+
[![PyPI format](https://img.shields.io/pypi/format/pandoc-codeblock-include.svg)](https://pypi.org/project/pandoc-codeblock-include/)
7+
[![License](https://img.shields.io/pypi/l/pandoc-codeblock-include.svg)](https://raw.githubusercontent.com/chdemko/pandoc-codeblock-include/master/LICENSE)
8+
[![Downloads](https://img.shields.io/pypi/dm/pandoc-codeblock-include.svg)](https://pypi.org/project/pandoc-codeblock-include/)
9+
[![Python version](https://img.shields.io/pypi/pyversions/pandoc-codeblock-include.svg)](https://pypi.org/project/pandoc-codeblock-include/)
10+
[![Development Status](https://img.shields.io/pypi/status/pandoc-codeblock-include.svg)](https://pypi.org/project/pandoc-codeblock-include/)
11+
12+
*pandoc-codeblock-include* is a [pandoc] filter for including files in `CodeBlock` elements.
13+
14+
[pandoc]: http://pandoc.org/
15+
16+
Documentation
17+
-------------
18+
19+
See the [wiki pages](https://github.com/chdemko/pandoc-codeblock-include/wiki).
20+
21+
Usage
22+
-----
23+
24+
To apply the filter, use the following option with pandoc:
25+
26+
--filter pandoc-codeblock-include
27+
28+
Installation
29+
------------
30+
31+
*pandoc-codeblock-include* requires [python], a programming language that comes pre-installed on linux and Mac OS X, and which is easily installed [on Windows]. Either python 2.7 or 3.x will do.
32+
33+
Install *pandoc-codeblock-include* as root using the bash command
34+
35+
pip install pandoc-codeblock-include
36+
37+
To upgrade to the most recent release, use
38+
39+
pip install --upgrade pandoc-codeblock-include
40+
41+
`pip` is a script that downloads and installs modules from the Python Package Index, [PyPI]. It should come installed with your python distribution. If you are running linux, `pip` may be bundled separately. On a Debian-based system (including Ubuntu), you can install it as root using
42+
43+
apt-get update
44+
apt-get install python-pip
45+
46+
[python]: https://www.python.org
47+
[on Windows]: https://www.python.org/downloads/windows
48+
[PyPI]: https://pypi.org
49+
50+
51+
Getting Help
52+
------------
53+
54+
If you have any difficulties with *pandoc-codeblock-include*, please feel welcome to [file an issue] on github so that we can help.
55+
56+
[file an issue]: https://github.com/chdemko/pandoc-codeblock-include/issues
57+

pandoc_codeblock_include.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Pandoc filter for including file in code block
5+
"""
6+
7+
from panflute import *
8+
9+
def include(elem, doc):
10+
# Is it a CodeBlock?
11+
if isinstance(elem, CodeBlock):
12+
found = False
13+
start_from = 0
14+
end_at = -1
15+
for name, value in elem.attributes.items():
16+
if name == 'include':
17+
try:
18+
file_content = open(value, 'r').readlines()
19+
found = True
20+
except IOError:
21+
debug('[WARNING] pandoc-codeblock-include: ' + value + ' not found')
22+
elif name == 'start-from' or name == 'startFrom':
23+
try:
24+
start_from = int(value) - 1
25+
except ValueError:
26+
debug('[WARNING] pandoc-codeblock-include: ' + value + ' is not a correct integer')
27+
elif name == 'end-at' or name == 'endAt':
28+
try:
29+
end_at = int(value)
30+
except ValueError:
31+
debug('[WARNING] pandoc-codeblock-include: ' + value + ' is not a correct integer')
32+
if found:
33+
if end_at == -1:
34+
text = file_content[start_from:]
35+
else:
36+
text = file_content[start_from:end_at]
37+
elem.text = ''.join(text)
38+
39+
def main(doc = None):
40+
return run_filter(include, doc = doc)
41+
42+
if __name__ == '__main__':
43+
main()
44+

setup.cfg

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[bdist_wheel]
2+
# This flag says that the code is written to work on both Python 2 and Python
3+
# 3. If at all possible, it is good practice to do this. If you cannot, you
4+
# will need to generate wheels for each Python version that you support.
5+
universal=1
6+
7+
[metadata]
8+
description-file = README.md
9+
10+
[aliases]
11+
test=pytest
12+

setup.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"""A setuptools based setup module.
2+
3+
See:
4+
https://packaging.python.org/en/latest/distributing.html
5+
https://github.com/chdemko/pandoc-codeblock-include
6+
"""
7+
8+
# Always prefer setuptools over distutils
9+
from setuptools import setup, find_packages
10+
11+
# To use a consistent encoding
12+
from codecs import open
13+
from os import path, makedirs
14+
15+
here = path.abspath(path.dirname(__file__))
16+
17+
# Get the long description from the README file
18+
try:
19+
import pypandoc
20+
long_description = pypandoc.convert('README.md', 'rst')
21+
except (IOError, ImportError):
22+
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
23+
long_description = f.read()
24+
25+
setup(
26+
name='pandoc-codeblock-include',
27+
28+
# Versions should comply with PEP440. For a discussion on single-sourcing
29+
# the version across setup.py and the project code, see
30+
# https://packaging.python.org/en/latest/single_source_version.html
31+
version='0.0.1',
32+
33+
# The project's description
34+
description='A pandoc filter for including file in block code',
35+
long_description=long_description,
36+
37+
# The project's main homepage.
38+
url='https://github.com/chdemko/pandoc-codeblock-include',
39+
40+
# The project's download page
41+
download_url = 'https://github.com/chdemko/pandoc-codeblock-include/archive/master.zip',
42+
43+
# Author details
44+
author='Christophe Demko',
45+
author_email='[email protected]',
46+
47+
# Maintainer details
48+
maintainer='Christophe Demko',
49+
maintainer_email='[email protected]',
50+
51+
# Choose your license
52+
license='BSD-3-Clause',
53+
54+
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
55+
classifiers=[
56+
# How mature is this project? Common values are
57+
# 3 - Alpha
58+
# 4 - Beta
59+
# 5 - Production/Stable
60+
'Development Status :: 3 - Alpha',
61+
62+
# Specify the OS
63+
'Operating System :: OS Independent',
64+
65+
# Indicate who your project is intended for
66+
'Environment :: Console',
67+
'Intended Audience :: End Users/Desktop',
68+
'Intended Audience :: Developers',
69+
'Topic :: Software Development :: Build Tools',
70+
71+
# Specify the Python versions you support here. In particular, ensure
72+
# that you indicate whether you support Python 2, Python 3 or both.
73+
'Programming Language :: Python :: 2.7',
74+
'Programming Language :: Python :: 3.4',
75+
'Programming Language :: Python :: 3.5',
76+
],
77+
78+
# What does your project relate to?
79+
keywords='pandoc filters codeblock include',
80+
81+
# Alternatively, if you want to distribute just a my_module.py, uncomment
82+
# this:
83+
py_modules=["pandoc_codeblock_include"],
84+
85+
# To provide executable scripts, use entry points in preference to the
86+
# "scripts" keyword. Entry points provide cross-platform support and allow
87+
# pip to create the appropriate form of executable for the target platform.
88+
entry_points={
89+
'console_scripts': [
90+
'pandoc-codeblock-include = pandoc_codeblock_include:main',
91+
],
92+
},
93+
94+
95+
# List run-time dependencies here. These will be installed by pip when
96+
# your project is installed. For an analysis of "install_requires" vs pip's
97+
# requirements files see:
98+
# https://packaging.python.org/en/latest/requirements.html
99+
install_requires=[
100+
'panflute>=1.10',
101+
'pypandoc>=1.4'
102+
],
103+
104+
# List additional groups of dependencies here (e.g. development
105+
# dependencies). You can install these using the following syntax,
106+
# for example:
107+
# $ pip install -e .[dev,test]
108+
extras_require={
109+
'dev': ['check-manifest'],
110+
'test': ['coverage'],
111+
},
112+
113+
# packages=find_packages(),
114+
# include_package_data = True,
115+
116+
setup_requires=['pytest-runner'],
117+
tests_require=['pytest', 'coverage'],
118+
)

tests/helper.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This Python file uses the following encoding: utf-8
2+
3+
from panflute import *
4+
5+
import pandoc_codeblock_include
6+
7+
def conversion(markdown, format='markdown'):
8+
doc = convert_text(markdown, standalone = True)
9+
doc.format = format
10+
pandoc_codeblock_include.main(doc)
11+
return doc
12+
13+
def verify_conversion(markdown, expected, format='markdown'):
14+
doc = conversion(markdown, format)
15+
text = convert_text(doc, input_format='panflute', output_format='markdown', extra_args=['--wrap=none'], standalone=True)
16+
debug('**computed**')
17+
debug(text.strip())
18+
debug('**expected**')
19+
debug(expected.strip())
20+
assert text.strip() == expected.strip()
21+

tests/test_include.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# This Python file uses the following encoding: utf-8
2+
3+
from unittest import TestCase
4+
from panflute import *
5+
6+
from helper import verify_conversion
7+
8+
def test_include():
9+
verify_conversion(
10+
'''
11+
``` {include="tests/lorem"}
12+
```
13+
''',
14+
'''
15+
``` {include="tests/lorem"}
16+
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
17+
Duis pretium rutrum dignissim.
18+
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
19+
Interdum et malesuada fames ac ante ipsum primis in faucibus.
20+
Ut iaculis arcu sed dui ornare pretium.
21+
```
22+
'''
23+
)
24+
25+
def test_include_start_from():
26+
verify_conversion(
27+
'''
28+
``` {include="tests/lorem" start-from="2"}
29+
```
30+
''',
31+
'''
32+
``` {include="tests/lorem" start-from="2"}
33+
Duis pretium rutrum dignissim.
34+
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
35+
Interdum et malesuada fames ac ante ipsum primis in faucibus.
36+
Ut iaculis arcu sed dui ornare pretium.
37+
```
38+
'''
39+
)
40+
41+
def test_include_end_at():
42+
verify_conversion(
43+
'''
44+
``` {include="tests/lorem" end-at="2"}
45+
```
46+
''',
47+
'''
48+
``` {include="tests/lorem" end-at="2"}
49+
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
50+
Duis pretium rutrum dignissim.
51+
```
52+
'''
53+
)
54+

0 commit comments

Comments
 (0)