From b32ad7794d23e8aad680e021a226d39b8e8e9959 Mon Sep 17 00:00:00 2001 From: YDawn Date: Sun, 10 Nov 2019 14:24:49 -0500 Subject: [PATCH 1/3] Added support for basic paramaters --- README.md | 25 ++++++++++++++++++++++++- markdown_include/include.py | 23 ++++++++++++++++------- setup.py | 6 +++--- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 28e13d8..7df19c9 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. @@ -51,7 +55,7 @@ The following settings can be specified when initialising the plugin. catch. If false (default), a warning will be printed and Markdown will continue parsing the file. -##Examples +## Examples An example of setting the base path and file encoding is given below: ```python @@ -110,8 +114,27 @@ 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.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..e114a1e 100644 --- a/markdown_include/include.py +++ b/markdown_include/include.py @@ -30,6 +30,7 @@ from markdown.preprocessors import Preprocessor INC_SYNTAX = re.compile(r'\{!\s*(.+?)\s*!\}') +PARAM_SYNTAX = re.compile('--*\s') HEADING_SYNTAX = re.compile( '^#+' ) @@ -85,7 +86,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 +97,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,22 +121,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] break - + else: h = HEADING_SYNTAX.search(line) if h: headingDepth = len(h.group(0)) bonusHeading = '#' * headingDepth - + else: done = True return lines def makeExtension(*args,**kwargs): - return MarkdownInclude(kwargs) + return MarkdownInclude(kwargs) \ No newline at end of file diff --git a/setup.py b/setup.py index c8b7a06..178bb6b 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.2', 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 From 6f9381d9f9f48ef4404bba15b3abe56e945f5050 Mon Sep 17 00:00:00 2001 From: Yaniv Schahar Date: Sun, 10 Nov 2019 17:43:10 -0500 Subject: [PATCH 2/3] Update include.py Remove redundant constant --- markdown_include/include.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/markdown_include/include.py b/markdown_include/include.py index e114a1e..0b99321 100644 --- a/markdown_include/include.py +++ b/markdown_include/include.py @@ -30,7 +30,6 @@ from markdown.preprocessors import Preprocessor INC_SYNTAX = re.compile(r'\{!\s*(.+?)\s*!\}') -PARAM_SYNTAX = re.compile('--*\s') HEADING_SYNTAX = re.compile( '^#+' ) @@ -145,4 +144,4 @@ def run(self, lines): def makeExtension(*args,**kwargs): - return MarkdownInclude(kwargs) \ No newline at end of file + return MarkdownInclude(kwargs) From 0bf71b09a97a25cab73b14da7a7b6a3ed24f9ad0 Mon Sep 17 00:00:00 2001 From: YDawn Date: Mon, 11 Nov 2019 20:25:25 -0500 Subject: [PATCH 3/3] Added feature "trimNewlines" --- README.md | 9 +++++++-- markdown_include/include.py | 10 +++++++++- setup.py | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7df19c9..472e125 100644 --- a/README.md +++ b/README.md @@ -46,14 +46,17 @@ 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 @@ -133,6 +136,8 @@ 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 diff --git a/markdown_include/include.py b/markdown_include/include.py index e114a1e..0351401 100644 --- a/markdown_include/include.py +++ b/markdown_include/include.py @@ -50,7 +50,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) @@ -77,6 +80,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 @@ -131,6 +135,10 @@ def run(self, lines): 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: diff --git a/setup.py b/setup.py index 178bb6b..918bf56 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name = 'markdown-include', packages = find_packages(), - version = '0.5.2', + 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',