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

add --replace option #5

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mdtogh can **convert** your md files into html files like github does with featu
* offline renderer
* proxy support(respect https\_proxy environment variable)
* cache support
* do replacements in produced html documents

##demo

Expand Down Expand Up @@ -104,6 +105,10 @@ Offline rendering:
**Recommanded** options to generate `several files`:

$ mdtogh 01.md 02.md

options to do replacements in the generated files:

$ mdtogh --replace @@version@@,v1.2.3 01.md

For more options:

Expand Down
58 changes: 34 additions & 24 deletions mdtogh/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@
<path> is a file or a directory to render, [default: '.']
Options:
--templates=<path> path of templates, it should contains all three files: content.html, toc.html, index.html
--cache_path=<path> path to store style file cache, default to current directory
--system_css using system wide css.
--css when NOT set, css contents are generate into html
--abscss link css with absolute path, use only with --css is set
--gfm Use GitHub-Flavored Markdown, e.g. comments or issues
--context=<repo> The repository context, only taken into account with --gfm
--user=<username> GitHub username for API authentication
--pass=<password> GitHub password for API authentication
--toc Generate table of contents
--toc_depth=<n> Max toc depth, default to 2
--toc_file=<file> You can specific a file to be toc(Just incase you have one)
--book=<book.json> Generate toc with book info, only used when --toc is set
--offline Use offline renderer
--encoding=<utf-8> encode for file, use only when --offline is set
--refresh clear cached styles & refetch them
--file_reg=<reg_exp> when path is a directory, using reg_exp to get file, this reg_exp must obey python's rules
if not set, mdtogh will get all files end with .md or .markdown, Notice: this is case-insensitive.
--timeout=<time> timeout for request github in second. [default: 20]
--templates=<path> path of templates, it should contains all three files: content.html, toc.html, index.html
--cache_path=<path> path to store style file cache, default to current directory
--system_css using system wide css.
--css when NOT set, css contents are generate into html
--abscss link css with absolute path, use only with --css is set
--gfm Use GitHub-Flavored Markdown, e.g. comments or issues
--context=<repo> The repository context, only taken into account with --gfm
--user=<username> GitHub username for API authentication
--pass=<password> GitHub password for API authentication
--toc Generate table of contents
--toc_depth=<n> Max toc depth, default to 2
--toc_file=<file> You can specific a file to be toc(Just incase you have one)
--book=<book.json> Generate toc with book info, only used when --toc is set
--offline Use offline renderer
--encoding=<utf-8> encode for file, use only when --offline is set
--refresh clear cached styles & refetch them
--file_reg=<reg_exp> when path is a directory, using reg_exp to get file, this reg_exp must obey python's rules
if not set, mdtogh will get all files end with .md or .markdown, Notice: this is case-insensitive.
--timeout=<time> timeout for request github in second. [default: 20]
--replace=<match,replacement> replace <match> in markdown documents with <replacement>
Notice:
Due to limitation by github, the rate of anonymous access to github api is limit to 60 in an hour.
Expand All @@ -41,7 +42,7 @@
"""

import sys
from docopt import docopt
from docopt import docopt, DocoptExit
from .transform import transform
from . import __version__
import jinja2
Expand All @@ -63,10 +64,19 @@ def main(argv=None):
#json.dump(args, sys.stdout)

try:
transform(args['<path>'], args['--cache_path'], args['--system_css'], args['--css'], args['--abscss'],
args['--gfm'], args['--user'],args['--pass'],
args['--toc'], args['--toc_depth'], args['--toc_file'], args['--book'],
args['--offline'], args['--encoding'], args['--refresh'], args['--file_reg'], args['--templates'], args['--timeout'])
replace_dict = {}
if args['--replace'] is not None:
if ',' not in args['--replace']:
raise DocoptExit('--replace argument must use `,` to separate <match> from <replacement>')

match, replacement = args['--replace'].split(',', 1)
replace_dict = {match: replacement}

transform(args['<path>'], args['--cache_path'], args['--system_css'], args['--css'], args['--abscss'],
args['--gfm'], args['--user'],args['--pass'],
args['--toc'], args['--toc_depth'], args['--toc_file'], args['--book'],
args['--offline'], args['--encoding'], args['--refresh'], args['--file_reg'], args['--templates'],
args['--timeout'], replace_dict)
return 0
except ValueError as e:
print "Error: ", e
Expand Down
16 changes: 15 additions & 1 deletion mdtogh/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
import codecs
import json

def transform(paths = None, cache_path = None, system_css = False, css = False, abscss = False, gfm = False, username = None, password = None, needtoc = True, toc_depth = None, toc_file = None, book = '', offline = False, encoding = 'utf-8', refresh = False, file_reg = None, template_path = None, timeout = 20):
def transform(paths = None, cache_path = None, system_css = False, css = False, abscss = False, gfm = False,
username = None, password = None, needtoc = True, toc_depth = None, toc_file = None, book = '',
offline = False, encoding = 'utf-8', refresh = False, file_reg = None, template_path = None,
timeout = 20, replace_dict = None):

#first, initial enviroment for jinjia2
init_env(template_path)

if len(paths) == 0:
paths = ['.']

if replace_dict is None:
replace_dict = {}

#Get style file
styles, style_paths = get_style(cache_path, system_css, refresh)
#compile file reg exp
Expand Down Expand Up @@ -124,6 +130,10 @@ def transform(paths = None, cache_path = None, system_css = False, css = False,
book_index = render_index(bookinfo['title'], bookinfo['coverimage'], bookinfo['description'], rtoc, True if toc_file else False)
else:
book_index = render_index('', '', '', rtoc, True if toc_file else False)

for match, replacement in replace_dict.iteritems():
book_index = book_index.replace(match, replacement)

with open('index.html', 'w') as f:
f.write(book_index.encode('utf-8'))
else:
Expand All @@ -135,6 +145,10 @@ def transform(paths = None, cache_path = None, system_css = False, css = False,
n = contents[i + 1][0] if i + 1 != len(contents) else None

rendered = render_with_template('', contents[i][1], rtoc, p, n, css, abscss, needtoc, styles, style_paths)

for match, replacement in replace_dict.iteritems():
rendered = rendered.replace(match, replacement)

with open(contents[i][0], 'w') as f:
f.write(rendered.encode('utf-8'))

Expand Down