text in red
- - # dump fixed BBCode - > bbcode.parse ('[p][color=red]text in red').bbcode() - [p][color=red]text in red[/color][/p] - - > bbcode.parse ('This [b][i]code[/b] will be fixed[/invalid]').bbcode() - This [b][i]code[/i][/b] will be fixed - - # dump fixed bbcode - > str (bbcodeparse ('This [b][i]code[/b] will be fixed[/invalid]')) - This [b][i]code[/i][/b] will be fixed - ''' - _bbcode = '' - _tokens = [] - - def __init__ (self, bbcode = '', fixInvalidCode = True): - ''' Initialize and parse bbcode string (if any is given) - ''' - self.parse (bbcode, fixInvalidCode) - return - - def __str__ (self): - return self.bbcode() - - def parse (self, bbcode = None, fixInvalidCode = True): - ''' - It will parse and return the token list, trying to fix tags if - fixInvalidCode is True - - It will return the current object to allow chaining - - Example: - code = bbcode() - code.parse ('bold', True) -> - code.parse ('bolditalics', True) -> internally will add the missing '' - ''' - if bbcode is not None: - self._bbcode = bbcode - self._tokens = self.tokenize (bbcode) - if fixInvalidCode: - self._tokens = self.fixWrongTags (self._tokens) - - return self - - # return ALL tokens - def getTokens (self): - return self._tokens - - def bbcode (self): - ''' - Dump BBCode again. This is useful for dumping valid BBCode - ''' - bbcode = [] - for token in self._tokens: - if token is None: - continue - - if isinstance (token, basestring): - bbcode.append (token.replace (u'[', u'\[').replace (u']', u'\]')) - continue - - tag = token['tag'] # opening or closing simple tag. e.g: 'b', '/b', '/u', ... - tagOpener = (u'/' if tag[0] == u'/' else u'') - - if (tagOpener == '/') or ('args' not in token): - bbcode.append (u'[' + tag + u']') - else: - # process args - argstr = '' - - # the arg with the same name as the tag repersents the '=whatever' - if tag in token['args']: - if re.match ('\s|"', token['args'][tag]) is None: - argstr = u'=' + token['args'][tag] - else: - argstr = u'="' + token['args'][tag].replace (u'"', u'\"') + u'"' - - for (k,v) in token['args'].iteritems(): - if k == tag: # already processed - continue - argstr += ' ' + k + u'="' + v.replace (u'"', u'\"') + u'"' - - bbcode.append (u'[' + tag + argstr + ']') - - return u''.join (bbcode) - - def html (self, allowClassAttr = False, doDeepCopy = True): - ''' - Convert current parsed code to HTML - - @allowClassAttr - Is something like [b class="asdf"] allowed? - - @doDeepCopy - True: it does a deep copy of tokens so this list will remain unchanged - False: tokens will be modified internally, but the output will be produced like 5x faster - it's a good idea to use False when the string parsed is huge and this is the - last operation on the string - - Example: - code = bbcode ('[b]bold[/b]') - code.html() -> 'bold' - ''' - from bbcode2html import bbcode2html - return bbcode2html.convertToHTML (self._tokens, allowClassAttr = allowClassAttr, doDeepCopy = doDeepCopy) - - - @staticmethod - def fixWrongTags (inTokenList): - ''' Add missing tokens that have not been closed properly and try to fix some scenarios - ''' - opened = [] - outTokenList = [] - for token in inTokenList: - # normal string... do nothing - if isinstance(token, basestring): - outTokenList.append (token) - else: - # if starts with '/' is closing a tag - if token['tag'][0] == '/': - while (len (opened) > 0) and (opened[-1] != token['tag'][1:]): - outTokenList.append ({'tag' : '/' + opened[-1] }) - del opened[-1] - - if len(opened): - del opened[-1] - outTokenList.append (token) - - # opening tag - else: - # if I open the same tag I opened before, close it, and open it again - if (len(opened) > 0) and (token['tag'] == opened[-1]): - outTokenList.append ({'tag' : '/' + opened[-1] }) - else: - opened.append (token['tag']) - outTokenList.append (token) - - # close all elements that have not been closed - while len(opened): - outTokenList.append ({'tag' : '/' + opened[-1] }) - del opened[-1] - - return outTokenList - - @staticmethod - def tokenize(code): - ''' - Tokenize BBCode tags and parameters - - Return the token list using a internal format. See the example: - [ - { 'tag' : 'p', 'args' : { 'font' : 'arial' } }, - 'This is ', - { 'tag' : 'url', 'args' : {'url' : 'http://www.google.com'} }, - 'a link to google', - { 'tag' : '/url' }, - { 'tag' : '/p' } - ] - ''' - re_tags = re.compile (r'(\[[^]]+\])', re.DOTALL | re.UNICODE) - re_tagName = re.compile (r'\[([^]=\s]+)([^]]*)\]', re.DOTALL | re.UNICODE) - #re_tagArgs = re.compile (r'\s*([^=]*)=(("([^"]+)")|([^\s]+))', re.DOTALL | re.UNICODE) - re_tagArgs = re.compile (r'\s*([\w]*)=(("([^"]+)")|([^\s]+))', re.DOTALL | re.UNICODE) - - # get a unique name and replace escaped braces encode utf8 to - # prevent CLI/Web from barfing on unicode chars. Not sure why - # this even needs to be 'unique' like this, but that's the way - # they wrote it. - unique = hashlib.md5(code.encode('utf8')).hexdigest() - code = code.replace ('\[', unique+'_OPEN_BRACE') - code = code.replace ('\]', unique+'_CLOSE_BRACE') - - splitted = re_tags.split(code) - - outTokenList = [] - for token in splitted: - if len(token) == 0: - continue - - if token[0] == '[': - match = re_tagName.match (token) - if match: - tagName = match.group(1) - tagArgs = match.group(2) - - tagToken = { 'tag' : tagName.lower() } - - # parse arguments (if any) - if len(tagArgs) > 0: - allArgs = re_tagArgs.findall(tagArgs) - - tagArgs = {} - for arg in allArgs: - # if the argument has no name, use the tagName itself - argName = (arg[0] if arg[0] != '' else tagName) - argValue = (arg[3] if (arg[1][0] == '"') else arg[4]) - - tagArgs[argName.lower()] = argValue.replace ('\"', '"') - - tagToken['args'] = tagArgs - - outTokenList.append (tagToken) - - # no match, append the text as it is - else: - outTokenList.append (token) - # append the text as it is - else: - outTokenList.append (token) - - # restore escaped braces back (once code is parsed) - restoredTokenList = [] - for token in outTokenList: - if isinstance (token, basestring): - token = token.replace (unique+'_OPEN_BRACE', '[').replace (unique+'_CLOSE_BRACE', ']') - restoredTokenList.append (token) - - return restoredTokenList - diff --git a/fanficdownloader/bbcodeutils/readme.txt b/fanficdownloader/bbcodeutils/readme.txt deleted file mode 100644 index e8a5949..0000000 --- a/fanficdownloader/bbcodeutils/readme.txt +++ /dev/null @@ -1,81 +0,0 @@ -AUTHOR - Pau Sanchez - http://www.codigomanso.com/ - -VERSION: - bbcodeutils v1.0 - -LICENSE - This code is licensed under Creative Commons Attribution 3.0 - http://creativecommons.org/licenses/by/3.0/ - - You can use this python module or any part of the code you want as long as you add - my name as a contributor to your project. - -DESCRIPTION - This module can be used to produce HTML from BBCode, to generate BBCode or to fix invalid BBCode. - - The classes are: - - bbcodeparser - - bbcodebuilder - - bbcode2html - - You can use bbcodeparser to parse BBCode and to produce output in any format you want. - - Open the python file to find more information and examples of use of each class. It can - be a good idea to check the test.py for examples - - To run the unit tests: - > python test.py - - To run the performance test: - > python test.py BBCodeTests.performanceTest - - -EXAMPLES OF BBCode: - - [b] -> bold - [u] -> underline - [i] -> italic - - [center] -> center the text inside - [color=XXX] -> change color of text - [size=XXX] -> change size of text - - Lists: - [ul] -> unordered list - [ol] -> ordered list - [li] -> list item - - [list] -> start unordered list - [*] -> list item - [list=1] -> start a list of numbers - [list=a] -> start a list of alphabetic characters - - Advanced: - [url] -> link to url - [url=http://link/url/]text[/url] - [url link=http://link/url/ title="This is the title"]text[/url] - - [img]http://to/image[/img] - [img=230x330]http://to/image[/img] - [img="Alt text here"]http://to/image[/img] - [img="Alt text here" width=320 height=240]http://to/image[/img] - - [email]asdf@asdf.com[/email] - [email=john@asdf.com]John Smith[/email] - - [google]search this[/google] - [wikipedia]Tom Hanks[/wikipedia] - [wikipedia lang=es]Tom Hanks[/wikipedia] - - Tables: - [table] - [tr] - [th] - [td] - - Advanced: - [google] - [wikipedia] - diff --git a/fanficdownloader/bbcodeutils/test.py b/fanficdownloader/bbcodeutils/test.py deleted file mode 100644 index 15e1093..0000000 --- a/fanficdownloader/bbcodeutils/test.py +++ /dev/null @@ -1,420 +0,0 @@ -#!/usr/bin/python -# -*- coding: UTF-8 -*- -# -# Author: Pau Sanchez (contact@pausanchez.com) -# Version: v1.0 -# Last Modified: 2010/09/15 -# -# For the latest version check out: -# http://www.codigomanso.com/en/projects -# -# My blog: -# http://www.codigomanso.com/en/ - English Version -# http://www.codigomanso.com/es/ - Spanish Version -# - -from bbcodeparser import bbcodeparser -from bbcodebuilder import bbcodebuilder - -import random -import unittest - -class BBCodeTests(unittest.TestCase): - def setUp (self): - self.bbcode = bbcodeparser() - return - - def testConstructor (self): - self.assertEqual (bbcodeparser ('whatever').html(), 'whatever') - self.assertEqual (bbcodeparser ('[b]bold[/b]').html(), 'bold') - self.assertEqual (str (bbcodeparser ('[b]bold[/b]')), '[b]bold[/b]') - return - - def testBold (self): - self.assertEqual (self.bbcode.parse ('whatever').html(), 'whatever') - self.assertEqual (self.bbcode.parse ('[b]bold[/b]').html(), 'bold') - self.assertEqual (self.bbcode.parse ('[B]bold[/b]').html(), 'bold') - self.assertEqual (self.bbcode.parse ('this is [B]bold[/B]').html(), 'this is bold') - return - - def testItalic (self): - self.assertEqual (self.bbcode.parse ('[i]italic[/i]').html(), 'italic') - return - - def testUnderline (self): - self.assertEqual (self.bbcode.parse ('[u]italic[/u]').html(), 'italic') - return - - def testURLs (self): - self.assertEqual ( - self.bbcode.parse ('[url]http://www.google.com[/url]').html(), - 'http://www.google.com' - ) - self.assertEqual ( - self.bbcode.parse ('[url=http://www.google.com]Google[/url]').html(), - 'Google' - ) - self.assertEqual ( - self.bbcode.parse ('[url="http://www.google.com"]Google[/url]').html(), - 'Google' - ) - self.assertEqual ( - self.bbcode.parse ('[url="http://www.google.com" title="Search Engine"]Google[/url]').html(), - 'Google' - ) - self.assertEqual ( - self.bbcode.parse ('[url link="http://www.google.com"]Google[/url]').html(), - 'Google' - ) - return - - def testPTag (self): - self.assertEqual ( - self.bbcode.parse ('[p color=#0000ff]blue[/p]').html(), - u'blue
' - ) - self.assertEqual ( - self.bbcode.parse ('[p size=12]12pt font[/p]').html(), - u'12pt font
' - ) - - self.assertEqual ( - self.bbcode.parse ('[p font=arial]arial[/p]').html(), - u'arial
' - ) - - self.assertEqual ( - self.bbcode.parse ('[p font=arial color=blue size=14]blue 14pt arial').html(), - u'blue 14pt arial
' - ) - - self.assertEqual ( - self.bbcode.parse ('[p class=whatever]text[/p]').html(), - u'text
' - ) - return - - def testColorTag (self): - self.assertEqual ( - self.bbcode.parse ('[color=#0000ff]blue[/color]').html(), - u'blue' - ) - return - - def testSizeTag (self): - self.assertEqual ( - self.bbcode.parse ('[size=12]12pt font[/size]').html(), - u'12pt font' - ) - return - - def testEmail(self): - self.assertEqual ( - self.bbcode.parse ('[email]asdf@asdf.com[/email]').html(), - u'asdf@asdf.com' - ) - - self.assertEqual ( - self.bbcode.parse ('[email=john@smith.com]John Smith[/email]').html(), - u'John Smith' - ) - return - - def testImgTag (self): - self.assertEqual ( - self.bbcode.parse ('[img]http://www.codigomanso.com/image.jpg[/img]').html(), - u'
'
- )
-
- self.assertEqual (
- self.bbcode.parse ('[img="This is the ALT of the image"]http://www.codigomanso.com/image.jpg[/img]').html(),
- u'
'
- )
-
- self.assertEqual (
- self.bbcode.parse ('[img=320x200]http://www.codigomanso.com/image.jpg[/img]').html(),
- u'
'
- )
-
- self.assertEqual (
- self.bbcode.parse ('[img=320x200 title="Image Test"]http://www.codigomanso.com/image.jpg[/img]').html(),
- u'
'
- )
-
- self.assertEqual (
- self.bbcode.parse ('[img="whatever" width=320 height="212" title="Image Test"]http://www.codigomanso.com/image.jpg[/img]').html(),
- u'
'
- )
- return
-
- def testGoogleURL (self):
- self.assertEqual (
- self.bbcode.parse ('[google]asdf[/google]').html(),
- u'asdf'
- )
- self.assertEqual (
- self.bbcode.parse ('[google]Tom Hanks[/google]').html(),
- u'Tom Hanks'
- )
- return
-
- def testWikipediaURL (self):
- self.assertEqual (
- self.bbcode.parse ('[wikipedia]Tom Hanks[/wikipedia]').html(),
- u'Tom Hanks'
- )
-
- self.assertEqual (
- self.bbcode.parse ('[wikipedia language=en]Tom Hanks[/wikipedia]').html(),
- u'Tom Hanks'
- )
-
- self.assertEqual (
- self.bbcode.parse ('[wikipedia lang=es]Tom Hanks[/wikipedia]').html(),
- u'Tom Hanks'
- )
-
- self.assertEqual (
- self.bbcode.parse ('[wikipedia=es]Tom Hanks[/wikipedia]').html(),
- u'Tom Hanks'
- )
- return
-
- def testListTags (self):
- self.assertEqual (
- self.bbcode.parse ('[ul][li]item 1[/li][li]item 2[/li][/ul]').html(),
- u'bold
' - ) - self.assertEqual ( - self.bbcode.parse ('[p][b]a ').html(), - 'a <b>
' - ) - - self.assertEqual ( - self.bbcode.parse ('[ol][li]item 1[li]item 2[/li][/ol]').html(), - u'This is bold and this italic and this is red and this is also red. -
- - fix bold and italic -
- http://www.codigomanso.com/
- Codigo Manso
- Codigo Manso
-
- | big | -
|---|