def _convert_section_to_markdown(self, content, title, short_title, section_type):
     """Convert JSON section to markdown format using pandoc."""
     pandoc_cmd = [
         "pandoc",
         "--markdown-headings=atx",
         "--wrap=preserve",
         "--columns=999",
         "--standalone",
         "--from=json",
         "--to=markdown+yaml_metadata_block",
     ]
     meta = {"title": {"t": "MetaInlines", "c": title}}
     if short_title is not None:
         meta["short_title"] = {
             "t": "MetaInlines",
             "c": panzertools.destringify(short_title),
         }
     if section_type is not None:
         meta["type"] = {"t": "MetaInlines", "c": [{"t": "Str", "c": section_type}]}
     section_json = json.dumps(
         {"blocks": content, "pandoc-api-version": [1, 22], "meta": meta}
     ).encode(ENCODING)
     proc = Popen(pandoc_cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
     out, err = proc.communicate(input=section_json, timeout=self.PANDOC_TIMEOUT)
     out = out.decode(ENCODING)
     err = err.decode(ENCODING)
     if proc.returncode != 0:
         panzertools.log("ERROR", err)
         raise RuntimeError("pandoc process exited with non-zero return code.")
     return out
    def _handle_node(self, node, section_path):
        handle_node_map = {
            "BulletList": self._handle_bulletlist,
            "CodeBlock": self._handle_codeblock,
            "DefinitionList": self._handle_definitionlist,
            "Div": self._handle_div,
            "Header": self._handle_header,
            "Image": self._handle_image,
            "Link": self._handle_link,
            "OrderedList": self._handle_orderedlist,
            "Quoted": self._handle_quoted,
            "Span": self._handle_span,
            "Table": self._handle_table,
            ("Emph", "Strong"): self._handle_emph_strong,
            ("Para", "Plain"): self._handle_para,
            (
                "LineBreak",
                "Math",
                "SoftBreak",
                "Space",
                "Str",
            ): lambda *args: None,
        }

        for elem_type in handle_node_map:
            if node["t"] == elem_type or node["t"] in elem_type:
                handle_node_map[elem_type](node, section_path)
                return

        panzertools.log(
            "WARNING",
            "CreateMapOfIds: Unknown element {} in section {}: {}".format(
                node["t"], section_path, pformat(node)
            ),
        )
示例#3
0
def main():
    """docstring for main"""
    OPTIONS = panzertools.read_options()
    if OPTIONS['pandoc']['output'] != '-':
        move_temp_files_back(OPTIONS['pandoc']['output'])
    else:
        panzertools.log('INFO', 'not run')
示例#4
0
def main():
    """docstring for main"""
    OPTIONS = panzertools.read_options()
    filepath = OPTIONS['pandoc']['output']
    if filepath != '-' and not OPTIONS['pandoc']['pdf_output']:
        print_log(filepath)
    else:
        panzertools.log('INFO', 'not run')
示例#5
0
def main():
    """docstring for main"""
    OPTIONS = panzertools.read_options()
    filepath = OPTIONS['pandoc']['output']
    if filepath != '-' and not OPTIONS['pandoc']['pdf_output']:
        print_log(filepath)
    else:
        panzertools.log('INFO', 'not run')
示例#6
0
def log(level, msg):
    import os
    import sys
    if 'PANZER_SHARED' in os.environ:
        sys.path.append(os.path.join(os.environ['PANZER_SHARED'], 'python'))
        import panzertools
        panzertools.log(level, msg)
    else:
        print(level + ': ' + msg, file=sys.stderr)
示例#7
0
def log(level, msg):
    import os
    import sys
    if 'PANZER_SHARED' in os.environ:
        sys.path.append(os.path.join(os.environ['PANZER_SHARED'], 'python'))
        import panzertools
        panzertools.log(level, msg)
    else:
        print(level + ': ' + msg, file=sys.stderr)
示例#8
0
def log(level, msg):
    import os
    import sys
    if 'PANZER_SHARED' in os.environ:
        sys.path.append(os.path.join(os.environ['PANZER_SHARED'], 'python'))
        import panzertools
        panzertools.log(level, msg)
    else:
        pass
示例#9
0
def main():
    OPTIONS = panzertools.read_options()
    filepath = OPTIONS['pandoc']['output']
    if filepath != '-' \
       and not OPTIONS['pandoc']['pdf_output'] \
       and os.path.exists(filepath):
        os.remove(filepath)
        panzertools.log('INFO', 'removed "%s"' % filepath)
    else:
        panzertools.log('INFO', 'not run')
示例#10
0
def main():
    """docstring for main"""
    ast = json.load(sys.stdin)
    panzertools.log('INFO', 'adding "References" section heading')
    try:
        ast[1].append(Header(1, attributes({'id': 'references'}), [Str('References')]))
    except (KeyError, IndexError):
        panzertools.log('WARNING', '"name" field inside "author" field not found')
    sys.stdout.write(json.dumps(ast))
    sys.stdout.flush()
示例#11
0
def main():
    OPTIONS = panzertools.read_options()
    filepath = OPTIONS['pandoc']['output']
    if filepath != '-' \
       and not OPTIONS['pandoc']['pdf_output'] \
       and os.path.exists(filepath):
        os.remove(filepath)
        panzertools.log('INFO', 'removed "%s"' % filepath)
    else:
        panzertools.log('INFO', 'not run')
示例#12
0
def get_list(meta):
    if get_list.checked == True:
        pass
    else:
        try:
            get_list.checked = True
            get_list.hitlist = [stringify(x) for x in meta.get('smallcaps', {})['c']]
            panzertools.log('INFO', 'small caps: ' + repr(get_list.hitlist))
        except KeyError:
            pass
    return get_list.hitlist
示例#13
0
def main():
    """docstring for main"""
    OPTIONS = panzertools.read_options()
    filepath = OPTIONS['pandoc']['output']
    if filepath == '-':
        panzertools.log('INFO', 'not run')
        return
    target = panzertools.FileInfo(filepath)
    target.set_extension('.pdf')
    pdfpath = target.fullpath()
    if os.path.exists(pdfpath):
        open_pdf(pdfpath)
 def _print_section(section, depth):
     if depth > MAX_LEVELS:
         return
     title = GenerateInnodoc._concatenate_strings(section["title"])
     indent = " " * depth
     msg = "{}{} ({})".format(indent, title, section["id"])
     panzertools.log("INFO", msg)
     try:
         for subsection in section["children"]:
             _print_section(subsection, depth + 1)
     except KeyError:
         pass
示例#15
0
def main():
    """docstring for main"""
    OPTIONS = panzertools.read_options()
    filepath = OPTIONS['pandoc']['output']
    if filepath == '-':
        panzertools.log('INFO', 'not run')
        return
    target = panzertools.FileInfo(filepath)
    target.set_extension('.pdf')
    pdfpath = target.fullpath()
    if os.path.exists(pdfpath):
        open_pdf(pdfpath)
示例#16
0
def main():
    """docstring for main"""
    OPTIONS = panzertools.read_options()
    filepath = OPTIONS['pandoc']['output']
    if filepath != '-' and not OPTIONS['pandoc']['pdf_output'] and os.path.exists(filepath):
        old_cwd = os.getcwd()
        try:
            run_latexmk(filepath)
        finally:
            os.chdir(old_cwd)
            panzertools.log('INFO', 'restored working directory to "%s"' % old_cwd)
    else:
        panzertools.log('INFO', 'not run')
示例#17
0
def move_temp_files_back(filepath):
    """Moves temporary files back into .tmp directory,

    Creates new .tmp if doesn't already exist

    Args:
        fullpath: fullpath to mangle
    """
    target = panzertools.FileInfo(filepath)
    mangled_target = target.mangle()
    basename = mangled_target.basename()
    pattern_to_match = basename + '.*'
    temp_path = os.path.join(target.parents(), TEMP_DIR)
    # check whether .tmp already exists...
    if os.path.exists(temp_path):
        # if .tmp exists, delete old temp files in it
        temp_files = glob.glob(os.path.join(temp_path, pattern_to_match))
        for temp_file in temp_files:
            os.remove(temp_file)
            panzertools.log('DEBUG', 'deleting %s' % temp_path)
    else:
        # otherwise make a new .tmp
        os.makedirs(temp_path)
        panzertools.log('DEBUG', 'creating %s' % temp_path)
    # move current temp files from current dir into .tmp
    panzertools.log('INFO', 'moving files back to %s' % temp_path)
    temp_files = glob.glob(os.path.join(target.parents(), pattern_to_match))
    for temp_file in temp_files:
        shutil.move(temp_file, temp_path)
        panzertools.log('DEBUG', 'moving %s to %s' % (temp_file, temp_path))
示例#18
0
def main():
    """docstring for main"""
    OPTIONS = panzertools.read_options()
    filepath = OPTIONS['pandoc']['output']
    if filepath != '-' and not OPTIONS['pandoc'][
            'pdf_output'] and os.path.exists(filepath):
        old_cwd = os.getcwd()
        try:
            run_latexmk(filepath)
        finally:
            os.chdir(old_cwd)
            panzertools.log('INFO',
                            'restored working directory to "%s"' % old_cwd)
    else:
        panzertools.log('INFO', 'not run')
 def _handle_span(self, node, section):
     attrs = self._attrs_to_dict(node["c"][0][2])
     if "data-index-term" in attrs.keys():
         pass
     elif not attrs.keys():
         for sub_node in node["c"][1]:
             self._handle_node(sub_node, section)
     elif "question" in node["c"][0][1]:
         pass
     else:
         panzertools.log(
             "WARNING",
             r"Found unknown span in section {}: {}".format(
                 section["id"], pformat(node)
             ),
         )
示例#20
0
def main():
    """docstring for main"""
    ast = json.load(sys.stdin)
    panzertools.log('INFO', 'adding "References" section heading')
    try:
        ast['blocks'].append(
            Header(1,
                   attributes({
                       'id': 'references',
                       'classes': ['unnumbered']
                   }), [Str('References')]))
    except (KeyError, IndexError):
        panzertools.log('WARNING',
                        '"name" field inside "author" field not found')
    sys.stdout.write(json.dumps(ast))
    sys.stdout.flush()
    def _write_section(self, section, outdir, depth, root=False):
        """Write a single section to a JSON file."""
        if depth > MAX_LEVELS:
            return

        if root:
            outdir_section = outdir
        else:
            outdir_section = os.path.join(outdir, section["id"])
        os.makedirs(outdir_section, exist_ok=True)

        try:
            content = section["content"]
            del section["content"]
        except KeyError:
            content = []

        filename = "content.{}".format(OUTPUT_FORMAT_EXT_MAP[self.output_format])
        filepath = os.path.join(outdir_section, filename)

        if self.output_format == "markdown":
            try:
                section_type = section["type"]
            except KeyError:
                section_type = None
            try:
                short_title = section["short_title"]
            except KeyError:
                short_title = None
            output = self._convert_section_to_markdown(
                content, section["title"], short_title, section_type
            )
            with open(filepath, "w") as sfile:
                sfile.write(output)
        elif self.output_format == "json":
            with open(filepath, "w") as sfile:
                json.dump(content, sfile)
        panzertools.log("INFO", "Wrote section {}".format(section["id"]))

        try:
            for subsection in section["children"]:
                self._write_section(subsection, outdir_section, depth + 1)
        except KeyError:
            pass
 def _handle_link(self, cmd, node, section):
     target = node["c"][2][0]
     if target.startswith("#"):
         target = target[1:]
     try:
         # try ID
         section_path = self.id_map[target]
         url = self._generate_section_link(section_path, target)
     except KeyError:
         # try section ID
         try:
             url = self._generate_section_link(self.section_map[target])
         except KeyError:
             panzertools.log(
                 "WARNING",
                 "Found {}: Couldn't map ID={} in section {}".format(
                     cmd, target, section["id"]
                 ),
             )
             return
     node["c"][0][2] = []  # remove attributes
     node["c"][2][0] = url
     panzertools.log("DEBUG", "Found {}: '{}' -> '{}'".format(cmd, target, url))
    def _handle_node(self, node, section):
        handle_node_map = {
            "BulletList": self._handle_bulletlist,
            "DefinitionList": self._handle_definitionlist,
            "Div": self._handle_div,
            "Link": self._handle_ref,
            "OrderedList": self._handle_orderedlist,
            "Quoted": self._handle_quoted,
            "Span": self._handle_span,
            "Strong": self._handle_strong,
            "Table": self._handle_table,
            ("Para", "Plain", "Emph"): self._handle_para,
            (
                "Code",
                "CodeBlock",
                "Header",
                "Image",
                "LineBreak",
                "Math",
                "SoftBreak",
                "Space",
                "Str",
            ): lambda *args: None,
        }

        for elem_type in handle_node_map:
            if node["t"] == elem_type or node["t"] in elem_type:
                handle_node_map[elem_type](node, section)
                return

        panzertools.log(
            "WARNING",
            "PostprocessLinks: Unknown element {} in section {}: {}".format(
                node["t"], section["id"], pformat(node)
            ),
        )
    def update_manifest(self, title, outdir):
        """Update ``manifest.yml`` file.

        If it doesn't exist it will be created.
        """
        manifest_path = os.path.abspath(os.path.join(outdir, "..", "manifest.yml"))
        try:
            with open(manifest_path) as manifest_file:
                manifest = yaml.safe_load(manifest_file)
        except FileNotFoundError:
            manifest = {self.LANGKEY: [], self.TITLEKEY: {}}

        if self.lang not in manifest[self.LANGKEY]:
            manifest[self.LANGKEY].append(self.lang)
        manifest[self.TITLEKEY][self.lang] = title

        with open(manifest_path, "w") as manifest_file:
            yaml.dump(
                manifest,
                manifest_file,
                default_flow_style=False,
                allow_unicode=True,
            )
        panzertools.log("INFO", "Wrote: {}".format(manifest_path))
示例#25
0
def open_pdf(filepath):
    fullpath = os.path.abspath(filepath)
    target = panzertools.FileInfo(filepath)
    command = ['open']
    command.extend([target.filename()])
    panzertools.log('INFO', 'running "%s"' % ' '.join(command))
    try:
        p = subprocess.Popen(command,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout_bytes, stderr_bytes = p.communicate()
        if stdout_bytes:
            stdout = stdout_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stdout.splitlines():
                panzertools.log('INFO', line)
        if stderr_bytes:
            stderr = stderr_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stderr.splitlines():
                panzertools.log('INFO', line)
    except OSError as error:
        panzertools.log('ERROR', error)
示例#26
0
def main():
    """docstring for main"""
    ast = json.load(sys.stdin)
    panzertools.log('INFO', 'transforming "author" field from list of "name" fields to list of inlines')
    try:
        if ast['meta']['author']['t'] == 'MetaList':
            authors = list()
            for auth in ast['meta']['author']['c']:
                authors.append(dict(auth['c']['name']))
            ast['meta']['author']['t'] = 'MetaList'
            ast['meta']['author']['c'] = authors
            panzertools.log('INFO', 'done')
    except (KeyError, IndexError):
        panzertools.log('WARNING', '"name" field inside "author" field not found')
    sys.stdout.write(json.dumps(ast))
    sys.stdout.flush()
示例#27
0
def move_temp_files_out(filepath):
    """Moves temporary files out of .tmp directory

    Args:
        basename: name (without extension) of temporary files
    """
    target = panzertools.FileInfo(filepath)
    mangled_target = target.mangle()
    basename = mangled_target.basename()
    temp_path = os.path.join(target.parents(), TEMP_DIR)
    if not os.path.exists(temp_path):
        panzertools.log('DEBUG', 'no %s found' % temp_path)
        return
    pattern_to_match = basename + '.*'
    temp_files = glob.glob(os.path.join(temp_path, pattern_to_match))
    panzertools.log('INFO', 'moving files out of %s' % temp_path)
    for temp_file in temp_files:
        shutil.copy(temp_file, target.parents())
        panzertools.log('DEBUG', 'copying %s to %s' % (temp_file, target.parents()))
示例#28
0
def main():
    """docstring for main"""
    ast = json.load(sys.stdin)
    panzertools.log(
        'INFO',
        'transforming "author" field from list of "name" fields to list of inlines'
    )
    try:
        if ast['meta']['author']['t'] == 'MetaList':
            authors = list()
            for auth in ast['meta']['author']['c']:
                authors.append(dict(auth['c']['name']))
            ast['meta']['author']['t'] = 'MetaList'
            ast['meta']['author']['c'] = authors
            panzertools.log('INFO', 'done')
    except (KeyError, IndexError):
        panzertools.log('WARNING',
                        '"name" field inside "author" field not found')
    sys.stdout.write(json.dumps(ast))
    sys.stdout.flush()
示例#29
0
def move_temp_files_out(filepath):
    """Moves temporary files out of .tmp directory

    Args:
        basename: name (without extension) of temporary files
    """
    target = panzertools.FileInfo(filepath)
    mangled_target = target.mangle()
    basename = mangled_target.basename()
    temp_path = os.path.join(target.parents(), TEMP_DIR)
    if not os.path.exists(temp_path):
        panzertools.log('DEBUG', 'no %s found' % temp_path)
        return
    pattern_to_match = basename + '.*'
    temp_files = glob.glob(os.path.join(temp_path, pattern_to_match))
    panzertools.log('INFO', 'moving files out of %s' % temp_path)
    for temp_file in temp_files:
        shutil.copy(temp_file, target.parents())
        panzertools.log('DEBUG',
                        'copying %s to %s' % (temp_file, target.parents()))
 def __init__(self, debug=False):
     self.debug = debug
     self.options = panzertools.read_options()
     self.filepath = self.options["pandoc"]["output"]
     self.convert_to = "json"
     if os.environ.get("INNOCONV_GENERATE_INNODOC_MARKDOWN"):
         panzertools.log("INFO", "Converting to Markdown.")
         self.convert_to = "markdown"
     if self.options["pandoc"]["write"] != "json":
         panzertools.log("ERROR", "Output is expected to be JSON!")
         sys.exit(0)
     # extract lang
     self.lang = None
     for key in self.options["pandoc"]["options"]["r"]["metadata"]:
         match = re.match(r"^lang:([a-z]{2})$", key[0])
         if match:
             self.lang = match.group(1)
     if not self.lang:
         raise RuntimeError("Error: Unable to extract lang key from metadata!")
     panzertools.log("INFO", "Found lang key={}".format(self.lang))
示例#31
0
def run_latexmk(filepath):
    """docstring for run_latexmk"""
    target = panzertools.FileInfo(filepath)
    os.chdir(target.parents())
    panzertools.log('INFO', 'changed working directory to "%s"' % os.getcwd())

    target_mangled = target.mangle()
    panzertools.log('DEBUG',
                    'copying %s to %s' %
                    (target.filename(), target_mangled.filename()))
    shutil.copy(target.filename(), target_mangled.filename())

    command = ['latexmk']
    command.extend(LATEXMK_OPTS)
    command.extend([target_mangled.filename()])
    panzertools.log('INFO', 'running "%s"' % ' '.join(command))

    pdf = panzertools.FileInfo(target.filename())
    pdf.set_extension('.pdf')
    pdf_mangled = pdf.mangle()

    before_pdf_ = -1
    if os.path.exists(pdf_mangled.filename()):
        before_pdf_ = os.path.getmtime(pdf_mangled.filename())
    my_env = os.environ.copy()
    my_env['LC_MESSAGES'] = 'en_GB.UTF-8'
    my_env['LANG']='en_GB.UTF-8'
    my_env['LC_CTYPE']='en_GB.UTF-8'
    my_env['LC_NUMERIC']='en_GB.UTF-8'
    my_env['LC_TIME']='en_GB.UTF-8'
    my_env['LC_COLLATE']='en_GB.UTF-8'
    my_env['LC_MONETARY']='en_GB.UTF-8'
    my_env['LC_MESSAGES']='en_GB.UTF-8'
    my_env['LC_PAPER']='en_GB.UTF-8'
    my_env['LC_NAME']='en_GB.UTF-8'
    my_env['LC_ADDRESS']='en_GB.UTF-8'
    my_env['LC_TELEPHONE']='en_GB.UTF-8'
    my_env['LC_MEASUREMENT']='en_GB.UTF-8'
    my_env['LC_IDENTIFICATION']='en_GB.UTF-8'
    try:
        p = subprocess.Popen(command,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             env=my_env)
        stdout_bytes, stderr_bytes = p.communicate()
        if stdout_bytes:
            stdout = stdout_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stdout.splitlines():
                panzertools.log('INFO', line)
        if stderr_bytes:
            stderr = stderr_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stderr.splitlines():
                panzertools.log('INFO', line)
    except OSError as error:
        panzertools.log('ERROR', error)

    after_pdf_ = -1
    if os.path.exists(pdf_mangled.filename()):
        after_pdf_ = os.path.getmtime(pdf_mangled.filename())

    # no output file exists
    if not os.path.exists(pdf_mangled.filename()):
        panzertools.log('ERROR', 'no output written')
    # output file exists, but is unchanged
    elif before_pdf_ == after_pdf_:
        if os.path.exists(pdf.filename()) \
        and os.path.getsize(pdf.filename()) == os.path.getsize(pdf_mangled.filename()) \
        and filecmp.cmp(pdf.filename(), pdf_mangled.filename(), shallow=False):
            panzertools.log('INFO', 'target already up to date "%s"' % pdf.filename())
        else:
            panzertools.log('ERROR', 'no output written')
    else:
        shutil.copy(pdf_mangled.filename(), pdf.filename())
        panzertools.log('INFO', 'output written to "%s"' % pdf.filename())
示例#32
0
def print_log(filepath):
    """docstring for print_log"""
    target = panzertools.FileInfo(filepath)
    mangled_target = target.mangle()
    mangled_target.set_extension('.log')
    command = ['/usr/local/bin/pplatex', '--input', mangled_target.fullpath()]
    panzertools.log('DEBUG', 'running %s' % command)
    stdout_list = list()
    try:
        p = subprocess.Popen(command,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout_bytes, stderr_bytes = p.communicate()
        if stdout_bytes:
            stdout = stdout_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stdout.splitlines():
                level = parse_level(line)
                panzertools.log(level, line)
        if stderr_bytes:
            stderr = stderr_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stderr.splitlines():
                panzertools.log('ERROR', line)
            panzertools.log('ERROR', 'failed to parse latex log file')
            panzertools.log(
                'INFO', 'consult the log file "%s" for more information' %
                mangled_target.fullpath())
    except subprocess.CalledProcessError:
        panzertools.log('ERROR', 'pplatex failed')
    for line in stdout_list:
        panzertools.log('INFO', line)
示例#33
0
- base level values may be from 1 to 5

if 2 is passed then:

    # Section

is transformed to -->

    ## Section

"""

import sys
import os
sys.path.append(os.path.join(os.environ['PANZER_SHARED'], 'python'))
import panzertools
from pandocfilters import toJSONFilter, Header

new_level = 1

def rehead(key, value, format, meta):
    if key == 'Header':
        return Header(new_level + (value[0] - 1), value[1], value[2])

if __name__ == "__main__":
    if len(sys.argv) >= 3:
        new_level = max(min(5, int(sys.argv[2])), 1)
    panzertools.log('INFO', 'base header level set to %d' % new_level)
    toJSONFilter(rehead)

示例#34
0
"""

import json
import sys
import os
sys.path.append(os.path.join(os.environ['PANZER_SHARED'], 'python'))
import panzertools
from pandocfilters import *


def rehead(key, value, format, meta):
    if key == 'Header' and value[0] == 1:
        if format == 'latex':
            text = '\\newpage\n\\thispagestyle{empty}\n\\setcounter{page}{1}'
            return [
                Para([RawInline('latex', text)]),
                Header(value[0], value[1], value[2])
            ]
        elif format == 'html':
            text = '<hr>'
            return [
                Para([RawInline('html', text)]),
                Header(value[0], value[1], value[2])
            ]


if __name__ == "__main__":
    panzertools.log('INFO', 'adding page break before each H1 section')
    toJSONFilter(rehead)
示例#35
0
if 2 is passed then:

    # Section

is transformed to -->

    ## Section

"""

import sys
import os
sys.path.append(os.path.join(os.environ['PANZER_SHARED'], 'python'))
import panzertools
from pandocfilters import toJSONFilter, Header

new_level = 1


def rehead(key, value, format, meta):
    if key == 'Header':
        return Header(new_level + (value[0] - 1), value[1], value[2])


if __name__ == "__main__":
    if len(sys.argv) >= 3:
        new_level = max(min(5, int(sys.argv[2])), 1)
    panzertools.log('INFO', 'base header level set to %d' % new_level)
    toJSONFilter(rehead)
示例#36
0
def print_log(filepath):
    """docstring for print_log"""
    target = panzertools.FileInfo(filepath)
    mangled_target = target.mangle()
    mangled_target.set_extension('.log')
    command = [ '/usr/local/bin/pplatex', '--input', mangled_target.fullpath() ]
    panzertools.log('DEBUG', 'running %s' % command)
    stdout_list = list()
    try:
        p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout_bytes, stderr_bytes = p.communicate()
        if stdout_bytes:
            stdout = stdout_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stdout.splitlines():
                level = parse_level(line)
                panzertools.log(level, line)
        if stderr_bytes:
            stderr = stderr_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stderr.splitlines():
                panzertools.log('ERROR', line)
            panzertools.log('ERROR', 'failed to parse latex log file')
            panzertools.log('INFO', 'consult the log file "%s" for more information' % mangled_target.fullpath())
    except subprocess.CalledProcessError:
        panzertools.log('ERROR', 'pplatex failed')
    for line in stdout_list:
        panzertools.log('INFO', line)
    def main(self):
        """Post-flight script entry point."""
        # load pandoc output
        with open(self.filepath, "r") as doc_file:
            doc = json.load(doc_file)

        # extract sections from headers
        sections, _ = ExtractSectionTree(doc["blocks"], 1).get_tree()
        panzertools.log("INFO", "Extracted table of contents.")

        # rewrite internal links
        section_map = CreateMapOfSectionIds(sections).get_map()
        panzertools.log("INFO", "Created map of sections from AST.")
        id_map = CreateMapOfIds(sections).create()
        panzertools.log("INFO", "Created ID map from AST.")
        PostprocessLinks(sections, section_map, id_map).process()
        panzertools.log("INFO", "Post-processed links.")

        # output directory
        outdir = os.path.normpath(os.path.dirname(self.filepath))
        if os.path.basename(outdir) != self.lang:  # append lang if necessary
            outdir = os.path.join(outdir, self.lang)
        os.makedirs(outdir, exist_ok=True)

        # write sections to file
        sections = WriteSections(sections, outdir, self.convert_to).write_sections()

        if os.environ.get("INNOCONV_GENERATE_INNODOC_MARKDOWN"):
            # write metadata file
            try:
                title = doc["meta"]["title"]["c"]
            except KeyError:
                title = "UNKNOWN COURSE"
            self.update_manifest(title, outdir)
        else:
            # write TOC
            tocpath = os.path.join(outdir, "toc.json")
            with open(tocpath, "w") as toc_file:
                json.dump(sections, toc_file)
            panzertools.log("INFO", "Wrote: {}".format(tocpath))

        # print toc tree
        if self.debug:
            self._print_sections(sections)

        # remove pandoc output file
        os.unlink(self.filepath)
        panzertools.log(
            "INFO", "Removed original pandoc output: {}".format(self.filepath)
        )
示例#38
0
def run_latexmk(filepath):
    """docstring for run_latexmk"""
    target = panzertools.FileInfo(filepath)
    os.chdir(target.parents())
    panzertools.log('INFO', 'changed working directory to "%s"' % os.getcwd())

    target_mangled = target.mangle()
    panzertools.log(
        'DEBUG',
        'copying %s to %s' % (target.filename(), target_mangled.filename()))
    shutil.copy(target.filename(), target_mangled.filename())

    command = ['latexmk']
    command.extend(LATEXMK_OPTS)
    command.extend([target_mangled.filename()])
    panzertools.log('INFO', 'running "%s"' % ' '.join(command))

    pdf = panzertools.FileInfo(target.filename())
    pdf.set_extension('.pdf')
    pdf_mangled = pdf.mangle()

    before_pdf_ = -1
    if os.path.exists(pdf_mangled.filename()):
        before_pdf_ = os.path.getmtime(pdf_mangled.filename())
    my_env = os.environ.copy()
    my_env['LC_MESSAGES'] = 'en_GB.UTF-8'
    my_env['LANG'] = 'en_GB.UTF-8'
    my_env['LC_CTYPE'] = 'en_GB.UTF-8'
    my_env['LC_NUMERIC'] = 'en_GB.UTF-8'
    my_env['LC_TIME'] = 'en_GB.UTF-8'
    my_env['LC_COLLATE'] = 'en_GB.UTF-8'
    my_env['LC_MONETARY'] = 'en_GB.UTF-8'
    my_env['LC_MESSAGES'] = 'en_GB.UTF-8'
    my_env['LC_PAPER'] = 'en_GB.UTF-8'
    my_env['LC_NAME'] = 'en_GB.UTF-8'
    my_env['LC_ADDRESS'] = 'en_GB.UTF-8'
    my_env['LC_TELEPHONE'] = 'en_GB.UTF-8'
    my_env['LC_MEASUREMENT'] = 'en_GB.UTF-8'
    my_env['LC_IDENTIFICATION'] = 'en_GB.UTF-8'
    try:
        p = subprocess.Popen(command,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             env=my_env)
        stdout_bytes, stderr_bytes = p.communicate()
        if stdout_bytes:
            stdout = stdout_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stdout.splitlines():
                panzertools.log('INFO', line)
        if stderr_bytes:
            stderr = stderr_bytes.decode(panzertools.ENCODING, errors='ignore')
            for line in stderr.splitlines():
                panzertools.log('INFO', line)
    except OSError as error:
        panzertools.log('ERROR', error)

    after_pdf_ = -1
    if os.path.exists(pdf_mangled.filename()):
        after_pdf_ = os.path.getmtime(pdf_mangled.filename())

    # no output file exists
    if not os.path.exists(pdf_mangled.filename()):
        panzertools.log('ERROR', 'no output written')
    # output file exists, but is unchanged
    elif before_pdf_ == after_pdf_:
        if os.path.exists(pdf.filename()) \
        and os.path.getsize(pdf.filename()) == os.path.getsize(pdf_mangled.filename()) \
        and filecmp.cmp(pdf.filename(), pdf_mangled.filename(), shallow=False):
            panzertools.log('INFO',
                            'target already up to date "%s"' % pdf.filename())
        else:
            panzertools.log('ERROR', 'no output written')
    else:
        shutil.copy(pdf_mangled.filename(), pdf.filename())
        panzertools.log('INFO', 'output written to "%s"' % pdf.filename())
示例#39
0
"""
Add page break at the start of each H1 section

    --- page break ---

    # Section

"""

import json
import sys
import os
sys.path.append(os.path.join(os.environ['PANZER_SHARED'], 'python'))
import panzertools
from pandocfilters import *

def rehead(key, value, format, meta):
    if key == 'Header' and value[0] == 1:
        if format == 'latex':
            text = '\\newpage\n\\thispagestyle{empty}\n\\setcounter{page}{1}'
            insert = RawInline('latex', text)
        elif format == 'html':
            text = '<hr>'
            insert = RawInline('html', text)
        return [Para([insert]), Header(value[0], value[1], value[2])]

if __name__ == "__main__":
    panzertools.log('INFO', 'adding page break before each H1 section')
    toJSONFilter(rehead)