def gen_odt(filename):
    print("Converting {0}...".format(filename), end=' ')
    template_path = '_template.ott'

    # Create the ODF document.
    styles = glmf_styles(template_path)

    # from pprint import pprint
    # pprint(styles)

    odf_doc = ODFDocument(styles=styles,
                          style_mapping=mapping)

    # Create the ODF writer.
    prompt = PythonPromptManager()
    writer = ODFWriter(odf_doc=odf_doc, prompt=prompt,
                       odf_renderer=GLMFODFRenderer
                       )

    # Read Markdown file.
    md = _read_md(filename)

    # Convert the Markdown file to the ODF file.
    convert(md, from_='markdown', to='opendocument', writer=writer)

    # Save the ODF chapter.
    writer.contents.save(op.join(op.splitext(filename)[0] + '.odt'))

    print("done.")
Exemplo n.º 2
0
    def _from_markdown(self, markdown_filepath, package, pid=None):
        signature = qtype.MethodSignature.from_markdown(markdown_filepath)

        with open(markdown_filepath) as fh:
            _, template = frontmatter.parse(fh.read())

        # TODO: verify that `id` is a valid Python identifier
        id = os.path.splitext(os.path.basename(markdown_filepath))[0]

        # TODO handle default values for optional parameters when that's
        # supported
        function_def_line = 'def %s(%s, %s):' % (
            id, ', '.join(signature.inputs), ', '.join(signature.parameters))
        indent = ' ' * 4
        function_body = ipymd.convert(template, from_='markdown', to='python')
        function_body = textwrap.indent(function_body, indent)
        function_return_line = '%sreturn %s' % (
            indent, ', '.join(signature.outputs))

        function_str = '\n'.join([function_def_line,
                                  function_body,
                                  function_return_line])

        scope = {}
        exec(function_str, scope)
        function = scope[id]

        self.__init__(package, id, signature, function,
                      ('markdown', markdown_filepath), pid=pid)
Exemplo n.º 3
0
    def _from_markdown(self, markdown_filepath, package, pid=None):
        signature = qtype.MethodSignature.from_markdown(markdown_filepath)

        with open(markdown_filepath) as fh:
            _, template = frontmatter.parse(fh.read())

        # TODO: verify that `id` is a valid Python identifier
        id = os.path.splitext(os.path.basename(markdown_filepath))[0]

        # TODO handle default values for optional parameters when that's
        # supported
        function_def_line = 'def %s(%s, %s):' % (
            id, ', '.join(signature.inputs), ', '.join(signature.parameters))
        indent = ' ' * 4
        function_body = ipymd.convert(template, from_='markdown', to='python')
        function_body = textwrap.indent(function_body, indent)
        function_return_line = '%sreturn %s' % (
            indent, ', '.join(signature.outputs))

        function_str = '\n'.join([function_def_line,
                                  function_body,
                                  function_return_line])

        scope = {}
        exec(function_str, scope)
        function = scope[id]

        self.__init__(package, id, signature, function,
                      ('markdown', markdown_filepath), pid=pid)
Exemplo n.º 4
0
def build_iab_main(input_dir, output_dir, out_format, ext, css=None):
    """ Convert md sources to readable book content, maintaining dir structure.

        A few additional processing steps happen here:
         * Add Table of Contents to the top of each section.
         * Create links from sha1 aliases.

        Parameters
        ----------
        input_dir : str
            Root path for the markdown files.
        output_dir : str
            Root path for the output files.
        out_format : str
            The ipymd format that output files should be written in (for example,
            ``notebook``).
        ext : str
            The extension to use for output files.

    """
    # Walk the input root directory. We only care about root and files
    # inside this loop (nothing happens with dirs).
    for unit_number, (unit, chapters) in enumerate(input_dir):
        # Iterate over the files in the current root.
        if unit_number == 0:
            unit_path = ''
        else:
            unit_path = str(unit_number) + '/'
        for chapter_number, content_md in enumerate(chapters):
            if chapter_number == 0:
                chapter_path = 'index'
            else:
                chapter_path = str(chapter_number)
            path = '%s%s' % (unit_path, chapter_path)
            # Convert it from markdown
            output_s = ipymd.convert(content_md, from_='markdown', to='notebook')
            # define the output filepath
            output_fp = get_output_fp(output_dir, path, ext)
            try:
                os.makedirs(os.path.split(output_fp)[0])
            except OSError:
                pass

            # write the output ipynb
            nbformat.write(output_s, output_fp)

    if out_format == 'html' or out_format == 's3':
        c = Config()
        c.ExecutePreprocessor.timeout = 600
        html_exporter = HTMLExporter(preprocessors=['nbconvert.preprocessors.execute.ExecutePreprocessor'],
                                     config=c)

        for root, dirs, files in os.walk(output_dir):
            if css:
                shutil.copy(css, os.path.join(root, 'custom.css'))
            for f in files:
                html_out, _ = html_exporter.from_filename(os.path.join(root, f))
                output_fn = os.path.splitext(f)[0] + ext
                output_fp = os.path.join(root, output_fn)
                open(output_fp, 'w').write(html_out)
Exemplo n.º 5
0
 def _format_script(self, provenance_lines, setup_lines, teardown_lines):
     provenance_str = "\n".join(provenance_lines)
     setup_str = "\n".join(setup_lines)
     teardown_str = "\n".join(teardown_lines)
     py_template = ipymd.convert(self.template, from_="markdown",
                                 to='python')
     return "\n\n".join(
         [provenance_str, setup_str, py_template, teardown_str])
Exemplo n.º 6
0
 def _format_script(self, provenance_lines, setup_lines, teardown_lines):
     provenance_str = "\n".join(provenance_lines)
     setup_str = "\n".join(setup_lines)
     teardown_str = "\n".join(teardown_lines)
     py_template = ipymd.convert(self.template,
                                 from_="markdown",
                                 to='python')
     return "\n\n".join(
         [provenance_str, setup_str, py_template, teardown_str])
Exemplo n.º 7
0
    def _from_markdown(self, markdown_filepath, plugin_name):
        with open(markdown_filepath) as fh:
            metadata, template = frontmatter.parse(fh.read())

        input_types = collections.OrderedDict()
        for input_ in metadata['inputs']:
            # TODO validate each nested dict has exactly two items
            name, type_tuple = list(input_.items())[0]
            input_types[name] = self._split_type_tuple(type_tuple, 'semantic')

        param_types = collections.OrderedDict()
        for parameter in metadata['parameters']:
            # TODO validate each nested dict has exactly two items
            name, type_tuple = list(parameter.items())[0]
            param_types[name] = self._split_type_tuple(type_tuple, 'primitive')
        output_types = collections.OrderedDict()
        for output in metadata['outputs']:
            # TODO validate each nested dict has exactly two items
            name, type_tuple = list(output.items())[0]
            output_types[name] = self._split_type_tuple(type_tuple, 'semantic')

        signature = qtype.Signature(input_types, param_types, output_types)

        # TODO: verify that `id_` is a valid Python identifier
        id_ = os.path.splitext(os.path.basename(markdown_filepath))[0]

        # TODO handle default values for optional parameters when that's
        # supported
        function_def_line = 'def %s(%s, %s):' % (id_,
                                                 ', '.join(input_types),
                                                 ', '.join(param_types))
        indent = ' ' * 4
        function_body = ipymd.convert(template, from_='markdown', to='python')
        function_body = textwrap.indent(function_body, indent)
        function_return_line = '%sreturn %s' % (indent,
                                                ', '.join(output_types))

        function_str = '\n'.join([function_def_line,
                                  function_body,
                                  function_return_line])

        scope = {}
        exec(function_str, scope)
        function = scope[id_]

        name = metadata['name']
        description = metadata['description']

        self._init(id_, signature, function, ('markdown', markdown_filepath),
                   name, description, template, plugin_name)
Exemplo n.º 8
0
    def _from_markdown(self, markdown_filepath):
        with open(markdown_filepath) as fh:
            metadata, template = frontmatter.parse(fh.read())

        input_types = collections.OrderedDict()
        for input_ in metadata["inputs"]:
            # TODO validate each nested dict has exactly two items
            name, type_expr = list(input_.items())[0]
            input_types[name] = self._parse_semantic_type(type_expr)

        param_types = collections.OrderedDict()
        for parameter in metadata["parameters"]:
            # TODO validate each nested dict has exactly two items
            name, type_expr = list(parameter.items())[0]
            param_types[name] = self._parse_primitive_type(type_expr)

        output_types = collections.OrderedDict()
        for output in metadata["outputs"]:
            # TODO validate each nested dict has exactly two items
            name, type_expr = list(output.items())[0]
            output_types[name] = self._parse_semantic_type(type_expr)

        signature = qiime.sdk.Signature(input_types, param_types, output_types)

        # TODO: verify that `id_` is a valid Python identifier
        id_ = os.path.splitext(os.path.basename(markdown_filepath))[0]

        # TODO handle default values for optional parameters when that's
        # supported
        function_def_line = "def %s(%s, %s):" % (id_, ", ".join(input_types), ", ".join(param_types))
        indent = " " * 4
        function_body = ipymd.convert(template, from_="markdown", to="python")
        function_body = textwrap.indent(function_body, indent)
        function_return_line = "%sreturn %s" % (indent, ", ".join(output_types))

        function_str = "\n".join([function_def_line, function_body, function_return_line])

        scope = {}
        exec(function_str, scope)
        function = scope[id_]

        name = metadata["name"]
        description = metadata["description"]

        self._init(id_, signature, function, ("markdown", markdown_filepath), name, description, template)