diff options
Diffstat (limited to 'divergence')
-rwxr-xr-x | divergence | 32 |
1 files changed, 23 insertions, 9 deletions
@@ -18,12 +18,16 @@ import subprocess import logging as log +DEFAULT_HEADER = """This page was generated automatically from Markdown using +the 'divergence' tool. Edits will need to be merged manually.""" + class DivergenceProgram: def __init__(self, user, password, url, space, force_update=False, include_toc=False, - include_header=True): + header=None, + no_header=False): self.api = requests.Session() self.api.auth = (user, password) self.api.headers.update({'Content-Type': 'application/json'}) @@ -31,7 +35,8 @@ class DivergenceProgram: self.default_space = space self.force_update = force_update self.include_toc = include_toc - self.include_header = include_header + self.header = header # from command-line arg + self.no_header = no_header # from command-line arg # TODO: clean up this code duplication... use pandoc data directory # instead? self.pandoc_helper_path = None @@ -139,7 +144,7 @@ class DivergenceProgram: # TODO: only alphanum and spaces? return title - def convert(self, f): + def convert(self, f, header=None): proc = subprocess.run(["pandoc", "-t", self.pandoc_helper_path, f], stdout=subprocess.PIPE) assert proc.returncode == 0 @@ -149,11 +154,10 @@ class DivergenceProgram: <ac:parameter ac:name="minLevel">1</ac:parameter> <ac:parameter ac:name="maxLevel">3</ac:parameter> </ac:structured-macro>""" + body - if self.include_header: + if header: body = """<ac:structured-macro ac:name="info"> <ac:rich-text-body> - <p>This page was generated automatically from Markdown using the - 'divergence' tool. Edits will need to be merged manually. </p> + <p>""" + header + """</p> </ac:rich-text-body> </ac:structured-macro>\n""" + body return body @@ -180,8 +184,12 @@ class DivergenceProgram: space_key = meta.get('confluence-space-key', self.default_space) page_id = meta.get('confluence-page-id') + header = not self.no_header and ( # --no-header trumps all + self.header or # command-line value gets priority + meta.get('disclaimer-header') or # fall back to per-file + DEFAULT_HEADER ) log.debug(title) - body = self.convert(f) + body = self.convert(f, header) prev = self.get_page(title, space_key=space_key, page_id=page_id) log.debug(prev) if prev is None: @@ -225,6 +233,9 @@ required environment variables: parser.add_argument("-f", "--force", action='store_true', help='Forces an update even if we think nothing has changed') + parser.add_argument("--header", + action='store', + help='Specify header to insert into the confluence document') parser.add_argument("--no-header", action='store_true', help='Disables inserting disclaimer headers into the confluence document') @@ -257,7 +268,10 @@ required environment variables: if args.space_key is None: args.space_key = "~" + user - log.warn("Defaulting to home space: %s" % args.space_key) + log.warning("Defaulting to home space: %s" % args.space_key) + + if args.header and args.no_header: + parser.exit(-1, "Pick one of --header and --no_header.\n") try: subprocess.check_output(['pandoc', '--version']) @@ -267,7 +281,7 @@ required environment variables: dp = DivergenceProgram(user,password, url, args.space_key, force_update=args.force, - include_header=not args.no_header, + header=args.header, include_toc=args.toc) dp.run(args.FILE) |