diff --git a/README.md b/README.md index 28e13d8..472e125 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -110,8 +117,29 @@ produces

End of included content.

``` +```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 diff --git a/markdown_include/include.py b/markdown_include/include.py index b9d1caf..0a4fed4 100644 --- a/markdown_include/include.py +++ b/markdown_include/include.py @@ -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) @@ -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 @@ -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( @@ -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 ' @@ -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 diff --git a/setup.py b/setup.py index c8b7a06..918bf56 100644 --- a/setup.py +++ b/setup.py @@ -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 = 'cmacmackin@gmail.com', - 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