Ejemplo n.º 1
0
def main():
    try:
        exit_code = cli(sys.argv[1:])
        sys.exit(exit_code)
    except Exception:
        print_error(traceback.format_exc())
        sys.exit(1)
Ejemplo n.º 2
0
def main():
    try:
        cli(sys.argv[1:])
        sys.exit(0)
    except Exception as e:
        print_error(e)
        sys.exit(1)
Ejemplo n.º 3
0
def handle_images(tex_lines, front_matter, context):
    '''
    We want to support including images in two contexts:

    1. GitHub flavored markdown
    2. Inside PDF documents

    Each context has conflicting constraints. We translate between each approach
    as best we can here.

    The markdown allows URLs to images hosted elsewhere, while
    LaTeX does not.  We (will) solve this by downloading images to `./tmp`.

    The markdown requires relative paths from the document where the image is
    used, to the for location of the image file.  It seems that LaTeX does not
    (although there may be a way to make it work).  We solve this by
    translating and copying the images to `./tmp`.

    The markdown supports SVGs, while LaTeX does not.  Thus, we convert SVGs
    into PDFs, and save them within `./tmp`.  Note that the SVG to PDF
    conversion is not perfect, and that there are some features of SVGs that are not supported, such as:

    - Masks
    - Style sheets
    - Color gradients
    - Embedded bitmaps
    '''
    # TODO: make the path handling more generic. Currently it assumes the CWD
    # is in `regulatory`, that the image path in the markdown is in `../images/` and is
    # placed into `regulatory/tmp/images/`.  E.g., resolve the relative URL
    # from the document's path, then flatten the full path into a single path,
    # and copy it into tmp
    # TODO: handle downloading externally hosted images
    for index, line in enumerate(tex_lines):
        line_contains_img = img_pattern.search(line)
        if line_contains_img:
            input_path = line_contains_img.group('path')
            if not os.path.isfile(input_path):
                print_error("Image does not exist: " +
                            os.path.abspath(input_path))

            input_directory, input_filename_w_ext = os.path.split(input_path)
            output_directory = os.path.join('./tmp/', input_directory)
            os.makedirs(output_directory, exist_ok=True)

            line_contains_svg = svg_pattern.search(line)
            if line_contains_svg:
                input_filename, _ = os.path.splitext(input_filename_w_ext)
                output_path = os.path.join(output_directory,
                                           input_filename + '.pdf')
                svg_to_pdf(input_path, output_path)
            else:
                output_path = os.path.join(output_directory,
                                           input_filename_w_ext)
                shutil.copyfile(input_path, output_path)

            tex_lines[
                index] = r'\includegraphics[width=0.95\textwidth]{' + output_path + '}'
Ejemplo n.º 4
0
def print_errors(errors):
    for error in errors:
        print_error(error)