def _GenRtf(self): """Generates the RTF equivalent of the displayed text in the current stc document window. @precondition: self._stc must have been set by a call to Generate @return: generated rtf marked up text """ # Buffer hasn't been set if self._stc is None: return u'' # Optimizations stc = self._stc def_fore = stc.GetDefaultForeColour(as_hex=True) self._colortbl.AddColor(def_fore) def_back = stc.GetDefaultBackColour(as_hex=True) self._colortbl.AddColor(def_back) last_pos = stc.GetLineEndPosition(stc.GetLineCount()) parse_pos = 0 last_id = None last_fore = None last_back = None start = end = 0 tmp_txt = list() font_tmp = "\\f0" fore_tmp = "\\cf%d" back_tmp = "\\cb%d" AddColor = self._colortbl.AddColor GetColorIndex = self._colortbl.GetColorIndex GetStyleAt = stc.GetStyleAt # Parse all characters/style bytes in document for parse_pos in xrange(last_pos + 1): sty_id = GetStyleAt(parse_pos) end = parse_pos # If style has changed build the previous section if sty_id != last_id: tag = stc.FindTagById(last_id) s_item = stc.GetItemByName(tag) AddColor(s_item.GetFore()) AddColor(s_item.GetBack()) tplate = font_tmp fid = GetColorIndex(s_item.GetFore()) if fid != last_fore: last_fore = fid tplate = tplate + (fore_tmp % fid) bid = GetColorIndex(s_item.GetBack()) if bid != last_back: last_back = bid tplate = tplate + (back_tmp % bid) tmp_txt.append(tplate + " " + \ self.TransformText(stc.GetTextRange(start, end))) start = end last_id = sty_id head = "{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0 %s;}}" % \ stc.GetDefaultFont().GetFaceName() return u"%s%s%s}" % (head, self._colortbl, "".join(tmp_txt))
def GenDoc(self): """Generates the document body of the LaTeX document @returns: the main body of the reference document marked up with latex """ tex = list() tmp = u'' start = parse_pos = 0 last_pos = self._stc.GetLineEndPosition(self._stc.GetLineCount()) # Define the default style self.RegisterStyleCmd('default_style', \ self._stc.GetItemByName('default_style')) # Get Document start point info last_id = self._stc.GetStyleAt(parse_pos) tmp = self.TransformText( self._stc.GetTextRange(parse_pos, parse_pos + 1)) tag = self._stc.FindTagById(last_id) if tag != wx.EmptyString: self.RegisterStyleCmd(tag, self._stc.GetItemByName(tag)) # Optimizations stc = self._stc GetStyleAt = stc.GetStyleAt GetTextRange = stc.GetTextRange TransformText = self.TransformText # Build LaTeX for parse_pos in xrange(last_pos + 1): curr_id = GetStyleAt(parse_pos) if parse_pos > 1: # This is the performance bottleneck, changeing the text # collection to when the style changes is much faster as # it only needs to be done once per style section instead # of once per character. Doing that however causes problems # with the style and resulting document formatting. tmp = TransformText(GetTextRange((parse_pos - 1), parse_pos)) if curr_id == 0 and GetStyleAt(parse_pos + 1) == last_id: curr_id = last_id # If style region has changed close section if curr_id != last_id or tmp[-1] == "\n": tmp_tex = TransformText(GetTextRange(start, parse_pos)) # tmp_tex = u"".join(tmp) if tag == "operator_style" or \ (tag == "default_style" and \ tmp_tex.isspace() and len(tmp_tex) <= 2): tex.append(tmp_tex) else: if "\\\\\n" in tmp_tex: tmp_tex = tmp_tex.replace("\\\\\n", "") tmp2 = "\\%s{%s}\\\\\n" else: tmp2 = "\\%s{%s}" cmd = self.CreateCmdName(tag) if cmd in [None, wx.EmptyString]: cmd = "defaultstyle" tex.append(tmp2 % (cmd, tmp_tex)) last_id = curr_id tag = stc.FindTagById(last_id) if tag not in [None, wx.EmptyString]: self.RegisterStyleCmd(tag, stc.GetItemByName(tag)) tmp = list() start = parse_pos # Case for unstyled documents if tex == wx.EmptyString: tex.append(self.TransformText(stc.GetText())) return "\\begin{document}\n%s\n\\end{document}" % "".join(tex)
def GenerateBody(self): """Generates the body of the html from the stc's content. To do this it does a character by character parse of the stc to determine style regions and generate css and and styled spans of html in order to generate an 'exact' html reqresentation of the stc's window. @return: the body section of the html generated from the text control """ html = list() parse_pos = 0 style_start = 0 style_end = 0 last_pos = self.stc.GetLineEndPosition(self.stc.GetLineCount()) + 1 # Get Document start point info last_id = self.stc.GetStyleAt(parse_pos) tag = self.stc.FindTagById(last_id) if tag != wx.EmptyString: s_item = StyleItem() s_item.SetAttrFromStr(self.stc.GetStyleByName(tag)) self.css[tag] = CssItem(tag.split('_')[0], s_item) # Optimizations stc = self.stc GetStyleAt = stc.GetStyleAt # Build Html while parse_pos < last_pos: parse_pos += 1 curr_id = GetStyleAt(parse_pos) style_end = parse_pos # If style region has changed close section if curr_id == 0 and GetStyleAt(parse_pos + 1) == last_id: curr_id = last_id if curr_id != last_id or parse_pos == last_pos: tmp = stc.GetTextRange(style_start, style_end) tmp = self.TransformText(tmp) if tmp.isspace() or tag in ["default_style", "operator_style"]: html.append(tmp) else: tmp2 = "<span class=\"%s\">%s</span>" html.append(tmp2 % (tag.split('_')[0], tmp)) last_id = curr_id style_start = style_end tag = stc.FindTagById(last_id) if tag not in self.css: s_item = StyleItem() s_item.SetAttrFromStr(stc.GetStyleByName(tag)) self.css[tag] = CssItem(tag.split('_')[0], s_item) # Case for unstyled documents if len(html) == 0: s_item = StyleItem() s_item.SetAttrFromStr(stc.GetStyleByName('default_style')) self.css['default_style'] = CssItem('default', s_item) html.append(self.TransformText(stc.GetText())) else: self.OptimizeCss() return "<body class=\"default\">\n<pre>\n%s\n</pre>\n</body>" % \ "".join(html)