| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Editra
Revision: 55443
Author: CJP
Date: 03 Sep 2008 15:01:25
Changes:Diff:| ... | ...@@ -95,6 +95,7 @@ | |
| 95 | 95 | @type menu: wx.Menu |
| 96 | 96 | |
| 97 | 97 | """ |
| 98 | # Fetch all the menu items for each generator object | |
| 98 | 99 | menu_items = list() |
| 99 | 100 | for observer in self.observers: |
| 100 | 101 | try: |
| ... | ...@@ -104,6 +105,7 @@ | |
| 104 | 105 | except Exception, msg: |
| 105 | 106 | util.Log("[generator][err] %s" % str(msg)) |
| 106 | 107 | |
| 108 | # Construct the menu | |
| 107 | 109 | menu_items.sort() |
| 108 | 110 | genmenu = ed_menu.EdMenu() |
| 109 | 111 | for item in menu_items: |
| ... | ...@@ -123,6 +125,8 @@ | |
| 123 | 125 | """ |
| 124 | 126 | gentext = None |
| 125 | 127 | start = time.time() |
| 128 | # Find the correct generator and run its generate method on the | |
| 129 | # given text control. | |
| 126 | 130 | for observer in self.observers: |
| 127 | 131 | if observer.GetId() == e_id: |
| 128 | 132 | gentext = observer.Generate(txt_ctrl) |
| ... | ...@@ -158,13 +162,18 @@ | |
| 158 | 162 | @return: string version of html object |
| 159 | 163 | |
| 160 | 164 | """ |
| 165 | # Assemble the embedded html | |
| 161 | 166 | style = "<style type=\"text/css\">\n%s</style>" |
| 162 | 167 | css = wx.EmptyString |
| 163 | 168 | for key in self.css: |
| 164 | 169 | css += str(self.css[key]) + "\n" |
| 165 | 170 | css = css % self.stc.GetFontDictionary() |
| 166 | 171 | style = style % css |
| 172 | ||
| 173 | # Insert the css into the head | |
| 167 | 174 | head = self.head.replace('</head>', style + "\n</head>") |
| 175 | ||
| 176 | # Assemble the body of the html | |
| 168 | 177 | html = "<html>\n%s\n%s\n</html>" |
| 169 | 178 | html = html % (head, self.body) |
| 170 | 179 | return html |
| ... | ...@@ -250,14 +259,16 @@ | |
| 250 | 259 | s_item = StyleItem() |
| 251 | 260 | s_item.SetAttrFromStr(stc.GetStyleByName(tag)) |
| 252 | 261 | self.css[tag] = CssItem(tag.split('_')[0], s_item) |
| 262 | ||
| 263 | # Case for unstyled documents | |
| 253 | 264 | if len(html) == 0: |
| 254 | # Case for unstyled documents | |
| 255 | 265 | s_item = StyleItem() |
| 256 | 266 | s_item.SetAttrFromStr(stc.GetStyleByName('default_style')) |
| 257 | 267 | self.css['default_style'] = CssItem('default', s_item) |
| 258 | 268 | html.append(self.TransformText(stc.GetText())) |
| 259 | 269 | else: |
| 260 | 270 | self.OptimizeCss() |
| 271 | ||
| 261 | 272 | return "<body class=\"default\">\n<pre>\n%s\n</pre>\n</body>" % \ |
| 262 | 273 | "".join(html) |
| 263 | 274 | |
| ... | ...@@ -282,10 +293,15 @@ | |
| 282 | 293 | @postcondition: css is optimized to remove any redundant entries |
| 283 | 294 | |
| 284 | 295 | """ |
| 296 | # Must have the default style defined | |
| 285 | 297 | if not self.css.has_key('default_style'): |
| 286 | 298 | return |
| 299 | ||
| 300 | # Don't style operators. This is to optimize the html size | |
| 287 | 301 | if self.css.has_key('operator_style'): |
| 288 | 302 | self.css.pop('operator_style') |
| 303 | ||
| 304 | # All other css elements will inheirit from the default | |
| 289 | 305 | default = self.css['default_style'] |
| 290 | 306 | for key in self.css: |
| 291 | 307 | if key == 'default_style': |
| ... | ...@@ -317,7 +333,7 @@ | |
| 317 | 333 | |
| 318 | 334 | #-----------------------------------------------------------------------------# |
| 319 | 335 | |
| 320 | class CssItem(object): | |
| 336 | class CssItem: | |
| 321 | 337 | """Converts an Edtira StyleItem to a Css item for use in |
| 322 | 338 | generating html. |
| 323 | 339 | |
| ... | ...@@ -333,7 +349,6 @@ | |
| 333 | 349 | @see: L{ed_style} |
| 334 | 350 | |
| 335 | 351 | """ |
| 336 | object.__init__(self) | |
| 337 | 352 | |
| 338 | 353 | # Attributes |
| 339 | 354 | self._tag = class_tag |
| ... | ...@@ -341,6 +356,7 @@ | |
| 341 | 356 | self._fore = style_item.GetFore() |
| 342 | 357 | self._font = style_item.GetFace() |
| 343 | 358 | self._size = style_item.GetSize() |
| 359 | ||
| 344 | 360 | # List of additional style specs |
| 345 | 361 | self._decor = self.ExtractDecorators() |
| 346 | 362 | self._decor.extend(style_item.GetModifierList()) |
| ... | ...@@ -359,6 +375,7 @@ | |
| 359 | 375 | @return: CssItem as a string |
| 360 | 376 | |
| 361 | 377 | """ |
| 378 | # Generate the main style attribures | |
| 362 | 379 | css = ".%s {\n%s}" |
| 363 | 380 | css_body = wx.EmptyString |
| 364 | 381 | if self._font != wx.EmptyString: |
| ... | ...@@ -373,6 +390,8 @@ | |
| 373 | 390 | if self._back != wx.EmptyString: |
| 374 | 391 | back = self._back.split(',') |
| 375 | 392 | css_body += u"\tbackground-color: %s;\n" % back[0] |
| 393 | ||
| 394 | # Add additional style modifiers | |
| 376 | 395 | for item in self._decor: |
| 377 | 396 | if item == u'bold': |
| 378 | 397 | css_body += u"\tfont-weight: %s;\n" % item |
| ... | ...@@ -382,6 +401,8 @@ | |
| 382 | 401 | css_body += u"\ttext-decoration: %s;\n" % item |
| 383 | 402 | else: |
| 384 | 403 | pass |
| 404 | ||
| 405 | # Format the tag and body into the css def | |
| 385 | 406 | if css_body != wx.EmptyString: |
| 386 | 407 | return css % (self._tag, css_body) |
| 387 | 408 | else: |
| ... | ...@@ -583,8 +604,9 @@ | |
| 583 | 604 | self.RegisterStyleCmd(tag, stc.GetItemByName(tag)) |
| 584 | 605 | tmp = list() |
| 585 | 606 | start = parse_pos |
| 607 | ||
| 608 | # Case for unstyled documents | |
| 586 | 609 | if tex == wx.EmptyString: |
| 587 | # Case for unstyled documents | |
| 588 | 610 | tex.append(self.TransformText(stc.GetText())) |
| 589 | 611 | return "\\begin{document}\n%s\n\\end{document}" % "".join(tex) |
| 590 | 612 | |
| ... | ...@@ -609,6 +631,7 @@ | |
| 609 | 631 | @return: the LaTeX document preamble |
| 610 | 632 | |
| 611 | 633 | """ |
| 634 | # Preamble template | |
| 612 | 635 | pre = ("%% \iffalse meta-comment\n" |
| 613 | 636 | "%%\n%% Generated by Editra %s\n" |
| 614 | 637 | "%% This is generator is Very Experimental.\n" |
| ... | ...@@ -623,9 +646,13 @@ | |
| 623 | 646 | "\\usepackage{color}\n" |
| 624 | 647 | "\\usepackage{alltt}\n" |
| 625 | 648 | "\\usepackage{times}\n") % ed_glob.VERSION |
| 649 | ||
| 650 | # Set the background color | |
| 626 | 651 | pre += ("\\pagecolor[rgb]{%s}\n" % \ |
| 627 | 652 | self.HexToRGB(self._dstyle.GetBack())) |
| 628 | 653 | pre += "\\parindent=0in\n\n" |
| 654 | ||
| 655 | # Insert all styling commands | |
| 629 | 656 | pre += "%% Begin Styling Command Definitions" |
| 630 | 657 | for cmd in self._cmds: |
| 631 | 658 | pre += ("\n" + self._cmds[cmd]) |
| ... | ...@@ -674,6 +701,8 @@ | |
| 674 | 701 | |
| 675 | 702 | """ |
| 676 | 703 | cmd_name = self.CreateCmdName(cmd_name) |
| 704 | ||
| 705 | # If we already made a command for this style return | |
| 677 | 706 | if cmd_name in self._cmds: |
| 678 | 707 | return |
| 679 | 708 | |
| ... | ...@@ -766,8 +795,11 @@ | |
| 766 | 795 | @return: generated rtf marked up text |
| 767 | 796 | |
| 768 | 797 | """ |
| 769 | if not self._stc: | |
| 798 | # Buffer hasn't been set | |
| 799 | if self._stc is None: | |
| 770 | 800 | return u'' |
| 801 | ||
| 802 | # Optimizations | |
| 771 | 803 | stc = self._stc |
| 772 | 804 | def_fore = stc.GetDefaultForeColour(as_hex=True) |
| 773 | 805 | self._colortbl.AddColor(def_fore) |
| ... | ...@@ -786,9 +818,12 @@ | |
| 786 | 818 | AddColor = self._colortbl.AddColor |
| 787 | 819 | GetColorIndex = self._colortbl.GetColorIndex |
| 788 | 820 | GetStyleAt = stc.GetStyleAt |
| 821 | ||
| 822 | # Parse all characters/style bytes in document | |
| 789 | 823 | for parse_pos in xrange(last_pos + 1): |
| 790 | 824 | sty_id = GetStyleAt(parse_pos) |
| 791 | 825 | end = parse_pos |
| 826 | ||
| 792 | 827 | # If style has changed build the previous section |
| 793 | 828 | if sty_id != last_id: |
| 794 | 829 | tag = stc.FindTagById(last_id) |
| ... | ...@@ -808,6 +843,7 @@ | |
| 808 | 843 | self.TransformText(stc.GetTextRange(start, end))) |
| 809 | 844 | start = end |
| 810 | 845 | last_id = sty_id |
| 846 | ||
| 811 | 847 | head = "{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0 %s;}}" % \ |
| 812 | 848 | stc.GetDefaultFont().GetFaceName() |
| 813 | 849 | return u"%s%s%s}" % (head, self._colortbl, "".join(tmp_txt)) |
| ... | ...@@ -857,7 +893,7 @@ | |
| 857 | 893 | |
| 858 | 894 | #-----------------------------------------------------------------------------# |
| 859 | 895 | |
| 860 | class RtfColorTbl(object): | |
| 896 | class RtfColorTbl: | |
| 861 | 897 | """A storage class to help with generating the color table for |
| 862 | 898 | the Rtf Generator Class. |
| 863 | 899 | @see: Rtf |
| ... | ...@@ -868,8 +904,6 @@ | |
| 868 | 904 | @summary: creates an object for managing an rtf documents color table |
| 869 | 905 | |
| 870 | 906 | """ |
| 871 | object.__init__(self) | |
| 872 | ||
| 873 | 907 | # Attributes |
| 874 | 908 | self._index = list() # manages the order of the tables keys |
| 875 | 909 | self._tbl = dict() # map of style item color vals to rtf defs |