Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for basic parameters #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Markdown is being called. If you would like to change the directory relative to
which paths are evaluated, then this can be done by specifying the extension
setting ``base_path``.

You can pass in paramaters to the included Markdown, using the notation
``{!filename --key="Value"!}``. In the target file, the parameter should be
using the format ``{{key}}``, and it would be replaced by ``value``.

## Configuration

The following settings can be specified when initialising the plugin.
Expand All @@ -42,16 +46,19 @@ The following settings can be specified when initialising the plugin.
paths for the include statement. (Default: the run-directory.)
- __encoding__: Encoding of the files used by the include statement. (Default: utf-8.)
- __inheritHeadingDepth__ : If true, increases headings on include
file by amount of previous heading. Combiens with headingOffset
file by amount of previous heading. Combines with headingOffset
option, below. (Default: False.)
- __headingOffset__: Increases heading depth by a specific ammount, in
- __headingOffset__: Increases heading depth by a specific amount, in
addition to the inheritHeadingDepth Option. (Default: 0)
- __throwException__: When true, if the extension is unable to find an
included file it will throw an exception which the user can
catch. If false (default), a warning will be printed and Markdown
will continue parsing the file.
- __trimNewlines__: Remove redundant newlines from files with specified
extension. Value is a comma delimited list of extensions.
Defaults to none.

##Examples
## Examples

An example of setting the base path and file encoding is given below:
```python
Expand Down Expand Up @@ -110,8 +117,29 @@ produces
<p>End of included content.</p>
```

```markdown
Source file
# Heading Level 1 of main file

{!included_file.md --name="John Smith"!}
```

and included_file.md

```markdown
Hello {{name}}
```

produces
```html
Hello John Smith
```

## ChangeLog
### Version 0.5.3
Remove new lines for specific extensions
### Version 0.5.2
Added basic support for parameters
### Version 0.5.1
Bugfix for a syntax error.
### Version 0.5
Expand Down
30 changes: 23 additions & 7 deletions markdown_include/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def __init__(self, configs={}):
'to find an included file it will throw an '\
'exception which the user can catch. If false '\
'(default), a warning will be printed and '\
'Markdown will continue parsing the file.']
'Markdown will continue parsing the file.'],
'trimNewlines': ['', 'Remove redundant newlines from files with '\
'specified extension. Value is a comma delimited '\
'list of extensions. Defaults to none.']
}
for key, value in configs.items():
self.setConfig(key, value)
Expand All @@ -76,6 +79,7 @@ def __init__(self, md, config):
self.inheritHeadingDepth = config['inheritHeadingDepth']
self.headingOffset = config['headingOffset']
self.throwException = config['throwException']
self.trimNewlines = config['trimNewlines']

def run(self, lines):
done = False
Expand All @@ -85,7 +89,9 @@ def run(self, lines):
m = INC_SYNTAX.search(line)

if m:
filename = m.group(1)
includeArgs = m.group(1).split('--')
filename = includeArgs.pop(0).strip()
params = includeArgs
filename = os.path.expanduser(filename)
if not os.path.isabs(filename):
filename = os.path.normpath(
Expand All @@ -94,7 +100,7 @@ def run(self, lines):
try:
with open(filename, 'r', encoding=self.encoding) as r:
text = r.readlines()

except Exception as e:
if not self.throwException:
print('Warning: could not find file {}. Ignoring '
Expand All @@ -118,18 +124,28 @@ def run(self, lines):
text[i] = '#' * self.headingOffset + text[i]
else:
text[i] = text[i][0:-1]

text[0] = line_split[0] + text[0]
text[-1] = text[-1] + line_split[2]
lines = lines[:loc] + text + lines[loc+1:]
lines = lines[:loc] + text + lines[loc + 1:]
if len(params) > 0:
# Process params
for param in params:
pName, pValue = param.strip().split('=')
pValue = pValue.rstrip('\"').lstrip('\"')
lines = [l.replace('{{' + pName + '}}', pValue) for l in lines]
if self.trimNewlines != '':
extension = os.path.splitext(filename)[1].lstrip('.')
if extension in self.trimNewlines.split(','):
lines = [l.rstrip('\n') for l in lines]
break

else:
h = HEADING_SYNTAX.search(line)
if h:
headingDepth = len(h.group(0))
bonusHeading = '#' * headingDepth

else:
done = True
return lines
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
setup(
name = 'markdown-include',
packages = find_packages(),
version = '0.5.1',
version = '0.5.3',
description = 'This is an extension to Python-Markdown which provides an "include" function, similar to that found in LaTeX (and also the C pre-processor and Fortran). I originally wrote it for my FORD Fortran auto-documentation generator.',
long_description = long_description,
author = 'Chris MacMackin',
author_email = '[email protected]',
url = 'https://github.com/cmacmackin/markdown-include/',
download_url = 'https://github.com/cmacmackin/markdown-include/tarball/v0.5.1',
url = 'https://github.com/cmacmackin/markdown-include/',
download_url = 'https://github.com/cmacmackin/markdown-include/tarball/v0.2',
keywords = ['Markdown', 'typesetting', 'include', 'plugin', 'extension'],
classifiers=[
# How mature is this project? Common values are
Expand Down