willmcgugan / postmarkup

Automatically exported from code.google.com/p/postmarkup
3 stars 3 forks source link

Adding table tags #23

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Howdy,

Is it possible to have table tags added? I've created a code diff for what 
works already using your code as the example.

Example Using code diff below:
postmarkup.render_bbcode("[table][tr][td]Row 1, Col 1[/td][td]Row 1, Col 
2[/td][/tr][tr][td]Row 2, Col 1[/td][td]Row 2, Col 2[/td][/tr][/table]")

Code:
Index: postmarkup.py
--- postmarkup.py 
+++ postmarkup.py (unsaved) 
@@ -120,6 +120,11 @@
     add_tag(SizeTag, u"size")
     add_tag(ColorTag, u"color")
     add_tag(CenterTag, u"center")
+    
+    # Table information
+    add_tag(TableTag, u"table")
+    add_tag(TableRowTag, u"tr")
+    add_tag(TableDataTag, u"td")

     if use_pygments:
         assert pygments_available, "Install Pygments (http://pygments.org/) or call create with use_pygments=False"
@@ -314,6 +319,66 @@
         else:
             return u""

+class TableTag(TagBase):
+
+    def __init__(self, name, **kwargs):
+        TagBase.__init__(self, name, strip_first_newline=True)
+
+    def open(self, parser, *args):
+        TagBase.open(self, parser, *args)
+
+    def close(self, parser, *args):
+        TagBase.close(self, parser, *args)
+
+    def render_open(self, parser, node_index):
+        if self.params:
+            return u'<table>%s'%(PostMarkup.standard_replace(self.params))
+        else:
+            return u'<table>'
+
+    def render_close(self, parser, node_index):
+        return u"</table>"
+
+class TableRowTag(TagBase):
+
+    def __init__(self, name, **kwargs):
+        TagBase.__init__(self, name, strip_first_newline=True)
+
+    def open(self, parser, *args):
+        TagBase.open(self, parser, *args)
+
+    def close(self, parser, *args):
+        TagBase.close(self, parser, *args)
+
+    def render_open(self, parser, node_index):
+        if self.params:
+            return u'<tr>%s'%(PostMarkup.standard_replace(self.params))
+        else:
+            return u'<tr>'
+
+    def render_close(self, parser, node_index):
+        return u"</tr>"
+
+class TableDataTag(TagBase):
+
+    def __init__(self, name, **kwargs):
+        TagBase.__init__(self, name, strip_first_newline=True)
+
+    def open(self, parser, *args):
+        TagBase.open(self, parser, *args)
+
+    def close(self, parser, *args):
+        TagBase.close(self, parser, *args)
+
+    def render_open(self, parser, node_index):
+        if self.params:
+            return u'<td>%s'%(PostMarkup.standard_replace(self.params))
+        else:
+            return u'<td>'
+
+    def render_close(self, parser, node_index):
+        return u"</td>"
+    

 class QuoteTag(TagBase):

Thank you for your time and assistance,
- Kusinwolf

Original issue reported on code.google.com by kusinw...@gmail.com on 3 Feb 2011 at 9:45

GoogleCodeExporter commented 8 years ago
I apologize for the issue showing up as a Defect, I did not see any way to 
change to Enhancement request.

Original comment by kusinw...@gmail.com on 3 Feb 2011 at 9:45

GoogleCodeExporter commented 8 years ago
Seems like a good idea. Have you been using your patch for a while? Any issues?

Original comment by willmcgugan on 4 Dec 2011 at 9:44

GoogleCodeExporter commented 8 years ago
Howdy,

I have been using my patch since I posted. It works wonderfully, I haven't made 
any changes to that code since the post.

I apologize in advance if anything below has already been addressed or will not 
be worked on, but these are my experiences and fixes over the course of this 
year of using the module, last updated when I downloaded in February.

I did how ever find a bug with the [color] tag, if you have 
"[color=red]Something[color]" and forget to put the "/" in, you will get an 
exception of 

"IndexError: list index out of range
    color = self.params.split()[0:1][0].lower()"

I fixed this bug by checking for content in self.params first and if nothing is 
there, just return nothing.

@@ -561,20 +542,21 @@
         TagBase.__init__(self, name, inline=True)

     def render_open(self, parser, node_index):
-
+        # They may have entered [color] instead of [/color]
+        if not self.params:
+            self.color = None
+            return u""
+        
         valid_chars = self.valid_chars
         color = self.params.split()[0:1][0].lower()
         self.color = "".join([c for c in color if c in valid_chars])
-
-        if not self.color:
-            return u""
-
+        
         return u'<span style="color:%s">' % self.color

     def render_close(self, parser, node_index):
-
         if not self.color:
             return u''
+        
         return u'</span>'

I also made a number of other changes, such as I walked through every line of 
code explicitly noting each string is unicode to speed up the module. I ran the 
entire module through profiler and found the conversion from a non-unicode 
string to unicode was expensive and happen often.

The tokenizer was the largest change I made, this was to fix the assumption of 
"[" and/or "]" were for the postmarkup module exclusively. Much of the text I 
run through the module tends to have brackets for various reasons.

If you are interested in seeing my changes in detail, please contact me at 
kusinwolf@gmail.com and I will provide those changes via diff. Also, I have 
provided full credit of the module in the headers to you, siting this project 
for the work. :)

Original comment by kusinw...@gmail.com on 16 Dec 2011 at 5:05

GoogleCodeExporter commented 8 years ago
The color tag was fixed. At least I can't reproduce the problem.

Not sure what your changes regarding square brackets do. Postmarkup will allow 
square brackets though unmodified if they are not part of a tag. e.g. 
"[b]:-][/b]" works fine.

I think you may have been working with a rather old version, but I would like 
to see your patch! I'll fire off an email.

Original comment by willmcgugan on 16 Dec 2011 at 5:15

GoogleCodeExporter commented 8 years ago
Cleaning up old bugs. Will accept a patch against the current version if you 
have it...

Original comment by willmcgugan on 18 Nov 2012 at 5:08