Esempio n. 1
0
 def __raw_output(self, cmd, pattern=None):
     data = ensure_string(
         Popen([self.pandoc, cmd], stdout=PIPE).communicate()[0])
     if pattern:
         return re.search(pattern, data, re.DOTALL).group(1)
     else:
         return data
Esempio n. 2
0
    def __call__(self, args, should_open):
        largs = shlex.split(args)
        if largs == [] or largs[0].startswith('-'):
            largs = ['html'] + largs  # make sure we pass an output format
        p = self.pandoc_info.build_argument_parser()
        c_vars = vars(p.parse_args(largs))

        # Infer arguments from vim environment

        # a) bibliographies
        if 'bibliographies' in plugin_enabled_modules():
            local_bibs = vim.eval('b:pandoc_biblio_bibs')
            found_bibs = find_bibfiles()
            if local_bibs or found_bibs and not c_vars['bibliography']:
                c_vars['bibliography'] = []
            if local_bibs:
                c_vars['bibliography'].extend(local_bibs)
            if found_bibs:
                c_vars['bibliography'].extend(found_bibs)

        # Now, we must determine what are our input and output files

        # a) First, let's see what is the desired output format...
        output_format = c_vars['output_format'] \
            if self.pandoc_info.is_valid_output_format(c_vars['output_format']) \
            or c_vars['output_format'] == 'pdf' \
            else "html"
        # overwrite --to with this value
        # 'pdf' is not a valid output format, we pass it to -o instead)
        if output_format != 'pdf':
            c_vars['to'] = output_format
        else:
            c_vars['to'] = 'latex'

        if output_format == 'pdf':
            # pdf engine
            if self.pandoc_info.version >= '2':
                engine_option = 'pdf_engine'
            else:
                engine_option = 'latex_engine'
            if not c_vars[engine_option]:
                try:  # try a buffer local engine
                    engine_var = ensure_string(
                        vim.current.buffer.vars['pandoc_command_latex_engine'])
                except:  # use te global value
                    engine_var = ensure_string(
                        vim.vars['pandoc#command#latex_engine'])
                c_vars[engine_option] = str(engine_var)

        if not c_vars['output']:
            self._output_file_path = vim.eval('expand("%:r")') + '.' \
                + self.formats_table[re.split("[-+]", output_format)[0]]
            c_vars['output'] = self._output_file_path
        else:
            self._output_file_path = os.path.expanduser(c_vars['output'][0])

        input_arg = '"' + vim.eval('expand("%")') + '"'

        # Now, we reconstruct the pandoc call
        arglist = []
        arglist.append(ensure_string(vim.vars['pandoc#compiler#command']))
        arglist.append(ensure_string(vim.vars['pandoc#compiler#arguments']))
        # Only consider enabled flags and arguments with values
        extra_arg_vars_keys = [
            k for k in c_vars.keys() if c_vars[k] and k != 'output_format'
        ]
        for var in extra_arg_vars_keys:
            real_var = var.replace("_", "-")
            val = c_vars[var]
            if type(val
                    ) == list and len(val) > 1:  # multiple values, repeat keys
                for vv in val:
                    if type(vv) == list and type(vv[0]) == list:
                        vv = vv[0][0]
                    elif type(vv) == list:
                        vv = vv[0]
                    elif type(val) == bool:
                        vv = None
                    if vv:
                        vv = os.path.expanduser(vv)
                        arglist.append("--" + real_var + '="' + str(vv) + '"')
                    else:
                        arglist.append("--" + real_var)
            else:
                if type(val) == list and type(val[0]) == list:
                    val = val[0][0]
                elif type(val) == list:
                    val = val[0]
                elif type(val) == bool:
                    val = None
                if val:
                    val = os.path.expanduser(val)
                    arglist.append('--' + real_var + '="' + str(val) + '"')
                else:
                    arglist.append('--' + real_var)
        arglist.append(input_arg)

        self._run_command = " ".join(arglist)
        # execute
        self.execute(should_open)
Esempio n. 3
0
 def __raw_output(self, cmd, pattern=None):
     data = ensure_string(Popen([self.pandoc, cmd], stdout=PIPE).communicate()[0])
     if pattern:
         return re.search(pattern, data, re.DOTALL).group(1)
     else:
         return data
Esempio n. 4
0
    def __call__(self, args, should_open):
        largs = shlex.split(args)
        if largs == [] or largs[0].startswith('-'):
            largs = ['html'] + largs # make sure we pass an output format
        p = self.pandoc_info.build_argument_parser()
        c_vars = vars(p.parse_args(largs))

        # Infer arguments from vim environment

        # a) bibliographies
        if 'bibliographies' in plugin_enabled_modules():
            local_bibs = vim.eval('b:pandoc_biblio_bibs')
            found_bibs = find_bibfiles()
            if local_bibs or found_bibs and not c_vars['bibliography']:
                c_vars['bibliography'] = []
            if local_bibs:
                c_vars['bibliography'].extend(local_bibs)
            if found_bibs:
                c_vars['bibliography'].extend(found_bibs)

        # Now, we must determine what are our input and output files

        # a) First, let's see what is the desired output format...
        output_format = c_vars['output_format'] \
            if self.pandoc_info.is_valid_output_format(c_vars['output_format']) \
            or c_vars['output_format'] == 'pdf' \
            else "html"
        # overwrite --to with this value
        # 'pdf' is not a valid output format, we pass it to -o instead)
        if output_format != 'pdf':
            c_vars['to'] = output_format
        else:
            c_vars['to'] = 'latex'

        if output_format == 'pdf':
            # pdf engine
            if self.pandoc_info.version >= '2':
                engine_option = 'pdf_engine'
            else:
                engine_option = 'latex_engine'
            if not c_vars[engine_option]:
                try: # try a buffer local engine
                    engine_var = ensure_string(vim.current.buffer.vars['pandoc_command_latex_engine'])
                except: # use te global value
                    engine_var = ensure_string(vim.vars['pandoc#command#latex_engine'])
                c_vars[engine_option] = str(engine_var)

        if not c_vars['output']:
            self._output_file_path = vim.eval('expand("%:r")') + '.' \
                + self.formats_table[re.split("[-+]", output_format)[0]]
            c_vars['output'] = self._output_file_path
        else:
            self._output_file_path = os.path.expanduser(c_vars['output'][0])

        input_arg = '"' + vim.eval('expand("%")') + '"'

        # Now, we reconstruct the pandoc call
        arglist = []
        arglist.append(ensure_string(vim.vars['pandoc#compiler#command']))
        arglist.append(ensure_string(vim.vars['pandoc#compiler#arguments']))
        # Only consider enabled flags and arguments with values
        extra_arg_vars_keys = [k for k in c_vars.keys() if c_vars[k] and k != 'output_format']
        for var in extra_arg_vars_keys:
            real_var = var.replace("_", "-")
            val = c_vars[var]
            if type(val) == list and len(val) > 1: # multiple values, repeat keys
                for vv in val:
                    if type(vv) == list and type(vv[0]) == list:
                        vv = vv[0][0]
                    elif type(vv) == list:
                        vv = vv[0]
                    elif type(val) == bool:
                        vv = None
                    if vv:
                        vv = os.path.expanduser(vv)
                        arglist.append("--" + real_var + '="' + str(vv) + '"')
                    else:
                        arglist.append("--" + real_var)
            else:
                if type(val) == list and type(val[0]) == list:
                    val = val[0][0]
                elif type(val) == list:
                    val = val[0]
                elif type(val) == bool:
                    val = None
                if val:
                    val = os.path.expanduser(val)
                    arglist.append('--' + real_var + '="' + str(val) + '"')
                else:
                    arglist.append('--' + real_var)
        arglist.append(input_arg)

        self._run_command = " ".join(arglist)
        # execute
        self.execute(should_open)