Skip to content

Commit f83c606

Browse files
committed
Merge branch 'diegobz-master'
2 parents 8d67aa8 + 8e9bbcd commit f83c606

File tree

4 files changed

+100
-31
lines changed

4 files changed

+100
-31
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
*.rst
21
*~
32
markdown_include.egg-info/
43
dist/

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,19 @@ fragments present in the Markdown.
3232
By default, all file-names are evaluated relative to the location from which
3333
Markdown is being called. If you would like to change the directory relative to
3434
which paths are evaluated, then this can be done by specifying the extension
35-
setting ``base_dir``.
35+
setting ``base_path``.
36+
37+
38+
```python
39+
import markdown
40+
from markdown_include.include import MarkdownInclude
41+
42+
# Markdown Extensions
43+
markdown_include = MarkdownInclude(
44+
configs={'base_path':'/srv/content/', 'encoding': 'iso-8859-1'}
45+
)
46+
html = markdown.markdown(source, extensions=[markdown_include])
47+
```
3648

3749
##ChangeLog
3850
###Version 0.4

README.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Markdown-Include
2+
================
3+
4+
This is an extension to
5+
`Python-Markdown <https://pythonhosted.org/Markdown/>`__ which provides
6+
an "include" function, similar to that found in LaTeX (and also the C
7+
pre-processor and Fortran). I originally wrote it for my
8+
`FORD <https://pypi.python.org/pypi/FORD>`__ Fortran auto-documentation
9+
generator.
10+
11+
Installation
12+
------------
13+
14+
This module can now be installed using ``pip``.
15+
16+
::
17+
18+
pip install markdown-include
19+
20+
Usage
21+
-----
22+
23+
This module can be used in a program in the following way:
24+
25+
::
26+
27+
import markdown
28+
html = markdown.markdown(source, extensions=[markdown_include.include'])
29+
30+
The syntax for use within your Markdown files is ``{!filename!}``. This
31+
statement will be replaced by the contents of ``filename``.
32+
Markdown-Include will work recursively, so any included files within
33+
``filename`` wil also be included. This replacement is done prior to any
34+
other Markdown processing, so any Markdown syntax that you want can be used
35+
within your included files. Note that this is a change from the previous
36+
version. It was felt that this syntax was less likely to conflict with any code
37+
fragments present in the Markdown.
38+
39+
By default, all file-names are evaluated relative to the location from
40+
which Markdown is being called. If you would like to change the
41+
directory relative to which paths are evaluated, then this can be done
42+
by specifying the extension setting ``base_path``.
43+
44+
::
45+
46+
import markdown
47+
from markdown_include.include import MarkdownInclude
48+
49+
# Markdown Extensions
50+
markdown_include = MarkdownInclude(
51+
configs={'base_path':'/srv/content/', 'encoding': 'iso-8859-1'}
52+
)
53+
html = markdown.markdown(source, extensions=[markdown_include])
54+

markdown_include/include.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,102 @@
22
# -*- coding: utf-8 -*-
33
#
44
# include.py
5-
#
5+
#
66
# Copyright 2015 Christopher MacMackin <cmacmackin@gmail.com>
7-
#
7+
#
88
# This program is free software; you can redistribute it and/or modify
99
# it under the terms of the GNU General Public License as published by
1010
# the Free Software Foundation; either version 2 of the License, or
1111
# (at your option) any later version.
12-
#
12+
#
1313
# This program is distributed in the hope that it will be useful,
1414
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1515
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
# GNU General Public License for more details.
17-
#
17+
#
1818
# You should have received a copy of the GNU General Public License
1919
# along with this program; if not, write to the Free Software
2020
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
2121
# MA 02110-1301, USA.
22-
#
23-
#
24-
25-
22+
#
23+
#
2624

2725
from __future__ import print_function
28-
from markdown.extensions import Extension
29-
from markdown.preprocessors import Preprocessor
3026
import re
3127
import os.path
28+
from codecs import open
29+
from markdown.extensions import Extension
30+
from markdown.preprocessors import Preprocessor
3231

3332
INC_SYNTAX = re.compile(r'\{!\s*(.+?)\s*!\}')
3433

3534

3635
class MarkdownInclude(Extension):
3736
def __init__(self, configs=[]):
38-
self.config = {'base_path': ['.', 'Location from which to evaluate relative paths for the include statement.'],}
37+
self.config = {
38+
'base_path': ['.', 'Default location from which to evaluate ' \
39+
'relative paths for the include statement.'],
40+
'encoding': ['utf-8', 'Encoding of the files used by the include ' \
41+
'statement.']
42+
}
3943
for key, value in configs.items():
4044
self.setConfig(key, value)
41-
45+
4246
def extendMarkdown(self, md, md_globals):
43-
md.preprocessors.add('include', IncludePreprocessor(md,self.getConfigs()),'_begin')
44-
47+
md.preprocessors.add(
48+
'include', IncludePreprocessor(md,self.getConfigs()),'_begin'
49+
)
4550

4651

4752
class IncludePreprocessor(Preprocessor):
4853
'''
4954
This provides an "include" function for Markdown, similar to that found in
50-
LaTeX (also the C pre-processor and Fortran). The syntax is {{filename}},
55+
LaTeX (also the C pre-processor and Fortran). The syntax is {!filename!},
5156
which will be replaced by the contents of filename. Any such statements in
5257
filename will also be replaced. This replacement is done prior to any other
5358
Markdown processing. All file-names are evaluated relative to the location
5459
from which Markdown is being called.
5560
'''
56-
def __init__(self,md,config):
61+
def __init__(self, md, config):
5762
super(IncludePreprocessor, self).__init__(md)
5863
self.base_path = config['base_path']
59-
64+
self.encoding = config['encoding']
65+
6066
def run(self, lines):
6167
done = False
62-
6368
while not done:
6469
for line in lines:
6570
loc = lines.index(line)
6671
m = INC_SYNTAX.search(line)
67-
72+
6873
if m:
6974
filename = m.group(1)
7075
filename = os.path.expanduser(filename)
7176
if not os.path.isabs(filename):
72-
filename = os.path.normpath(os.path.join(self.base_path,filename))
77+
filename = os.path.normpath(
78+
os.path.join(self.base_path,filename)
79+
)
7380
try:
74-
with open(filename, 'r') as r:
81+
with open(filename, 'r', encoding=self.encoding) as r:
7582
text = r.readlines()
76-
except:
77-
print('Warning: could not find file {}. Ignoring include statement.'.format(filename))
83+
except Exception, e:
84+
print('Warning: could not find file {}. Ignoring '
85+
'include statement. Error: {}'.format(filename, e))
7886
lines[loc] = INC_SYNTAX.sub('',line)
79-
8087
break
81-
88+
8289
line_split = INC_SYNTAX.split(line,maxsplit=0)
8390
if len(text) == 0: text.append('')
8491
for i in range(len(text)):
8592
text[i] = text[i][0:-1]
86-
text[0] = line_split[0] + text[0]
93+
text[0] = line_split[0] + text[0]
8794
text[-1] = text[-1] + line_split[2]
8895
lines = lines[:loc] + text + lines[loc+1:]
8996
break
90-
9197
else:
9298
done = True
93-
9499
return lines
95100

96101

97-
98102
def makeExtension(*args,**kwargs):
99103
return MarkdownInclude(kwargs)

0 commit comments

Comments
 (0)