__main__.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:markdown_for_academia 作者: MarkdownForAcademia 项目源码 文件源码
def __invoke_pandoc__(input_content: str, output_format: str,
                      extra_args: dict, output_file: str, default: dict):
    """
    invoke pandoc via pypandoc.
    :param output_file: the file name of the output file
    :param default: the default argument dict.
                    the key is long name of the para, the
    :param input_content: the markdown content ready to be compiled
    :param output_format: a string indicate the output format of the file
                            like 'beamer', 'pdf', 'html')
    :param extra_args: all the other argument that pandoc accepts
    """

    def __convert_arg(arg: Tuple[str, Union[str, bool, list]]):
        """
        a helper that converts the a arg tuple into regular command line arg
        :param arg: a tuple, where the first is the long name of the argument
                    the second is the value of the argument
        :return: if the second one is a bool,
        """
        # extract info from arg
        arg_name = arg[0]
        val = arg[1]

        # if the value is true
        # if the second element is false it should be filtered out by
        # the parent function
        if val is True:
            return '--{arg}'.format(arg=arg_name)

        # if the value is a list
        # gives the
        elif isinstance(val, list):
            return ' '.join("--{arg}={val}".format(arg=arg_name, val=element)
                            for element in val)

        # if the val is a string
        else:
            return "--{arg}={val}".format(arg=arg_name, val=val)

    # merge the two dict
    arg_dict = default.copy()
    arg_dict.update(extra_args)  # default will be overloaded via extra_args

    # convert extra args to list
    # exclude where the second one is false
    converted_args = [__convert_arg(arg) for arg in arg_dict.items()
                      if arg[1] is not False]

    # invoke pandoc
    pypandoc.convert_text(source=input_content, format='md',
                          to=output_format, outputfile=output_file,
                          extra_args=converted_args)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号