Mercurial > libervia-backend
diff frontends/src/jp/cmd_blog.py @ 1885:edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 05 Mar 2016 19:21:35 +0100 |
parents | abb2f253188e |
children | f3db27508b31 |
line wrap: on
line diff
--- a/frontends/src/jp/cmd_blog.py Sat Mar 05 18:00:56 2016 +0100 +++ b/frontends/src/jp/cmd_blog.py Sat Mar 05 19:21:35 2016 +0100 @@ -101,6 +101,21 @@ # if not found, we use current syntax return self.host.bridge.getParamA("Syntax", "Composition", "value", self.profile) + def parse_args(self, cmd_line, **format_kw): + """Parse command arguments + + @param cmd_line(unicode): command line as found in sat.conf + @param format_kw: keywords used for formmating + @return (list(unicode)): list of arguments to pass to subprocess function + """ + try: + # we split the arguments and add the known fields + # we split arguments first to avoid escaping issues in file names + return [a.format(**format_kw) for a in shlex.split(cmd_line)] + except ValueError as e: + self.disp(u"Couldn't parse editor cmd [{cmd}]: {reason}".format(cmd=cmd_line, reason=e)) + return [] + class Edit(base.CommandBase, BlogCommon): @@ -205,13 +220,7 @@ except (NoOptionError, NoSectionError): # no, we check if we know the editor and have special arguments editor_args = EDITOR_ARGS_MAGIC.get(os.path.basename(editor), '') - try: - # we split the arguments and add the known fields - # we split arguments first to avoid escaping issues in file names - args = [a.format(content_file=content_file_path, metadata_file=meta_file_path) for a in shlex.split(editor_args)] - except ValueError as e: - self.disp(u"Couldn't parse editor cmd [{cmd}]: {reason}".format(cmd=editor_args, reason=e)) - args = [] + args = self.parse_args(editor_args, content_file=content_file_path, metadata_file=meta_file_path) if not args: args = [content_file_path] editor_exit = subprocess.call([editor] + args) @@ -303,7 +312,7 @@ self.host.quit(1) content = mb_data['content_xhtml'] - if current_syntax != 'XHTML': + if content and current_syntax != 'XHTML': content = self.host.bridge.syntaxConvert(content, 'XHTML', current_syntax, False, self.profile) content_file_obj.write(content.encode('utf-8')) content_file_obj.seek(0) @@ -326,18 +335,32 @@ url = 'file:{}'.format(self.urllib.quote(self.preview_file_path)) self.webbrowser.open_new_tab(url) + def _launchPreviewExt(self, cmd_line, opt_name): + url = 'file:{}'.format(self.urllib.quote(self.preview_file_path)) + args = self.parse_args(cmd_line, url=url, preview_file=self.preview_file_path) + if not args: + self.disp(u"Couln't find command in \"{name}\", abording".format(name=opt_name), error=True) + self.host.quit(1) + subprocess.Popen(args) + + def openPreviewExt(self): + self._launchPreviewExt(self.open_cb_cmd, "blog_preview_open_cmd") + + def updatePreviewExt(self): + self._launchPreviewExt(self.update_cb_cmd, "blog_preview_update_cmd") + def updateContent(self): - with open(self.current_file_path, 'rb') as f: - content_xhtml = f.read().decode('utf-8') - if self.syntax != 'XHTML': + with open(self.content_file_path, 'rb') as f: + content = f.read().decode('utf-8') + if content and self.syntax != 'XHTML': # we use safe=True because we want to have a preview as close as possible to what the # people will see - content_xhtml = self.host.bridge.syntaxConvert(content_xhtml, self.syntax, 'XHTML', True, self.profile) + content = self.host.bridge.syntaxConvert(content, self.syntax, 'XHTML', True, self.profile) xhtml = (u'<html xmlns="http://www.w3.org/1999/xhtml">' + u'<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>'+ '<body>{}</body>' + - u'</html>').format(content_xhtml) + u'</html>').format(content) with open(self.preview_file_path, 'wb') as f: f.write(xhtml.encode('utf-8')) @@ -370,16 +393,27 @@ sat_conf = config.parseMainConf() SYNTAX_EXT.update(config.getConfig(sat_conf, 'jp', CONF_SYNTAX_EXT, {})) - open_cb = self.showPreview - update_cb = self.showPreview + try: + self.open_cb_cmd = config.getConfig(sat_conf, 'jp', "blog_preview_open_cmd", Exception) + except (NoOptionError, NoSectionError): + self.open_cb_cmd = None + open_cb = self.showPreview + else: + open_cb = self.openPreviewExt + + self.update_cb_cmd = config.getConfig(sat_conf, 'jp', "blog_preview_update_cmd", self.open_cb_cmd) + if self.update_cb_cmd is None: + update_cb = self.showPreview + else: + update_cb = self.updatePreviewExt # which file do we need to edit? if self.args.file == 'current': - self.current_file_path = self.getCurrentFile(sat_conf) + self.content_file_path = self.getCurrentFile(sat_conf) else: - self.current_file_path = os.path.abspath(self.args.file) + self.content_file_path = os.path.abspath(self.args.file) - self.syntax = self.guessSyntaxFromPath(sat_conf, self.current_file_path) + self.syntax = self.guessSyntaxFromPath(sat_conf, self.content_file_path) # at this point the syntax is converted, we can display the preview