BuanBuan.py :  » Web-Frameworks » Karrigell » Karrigell-3.1 » common » demo » sqlite » wiki » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Karrigell 
Karrigell » Karrigell 3.1 » common » demo » sqlite » wiki » BuanBuan.py
"""BuanBuan, simple implementation of Wiki"""

import cStringIO,string,os,re,sys,time,cPickle,cgi
import sets

bolds=re.compile("'''(.+?)'''")
italics=re.compile("''(.+?)''")
superscripts=re.compile(r"\^(.+?)\^")  # caret for superscripted
hr = re.compile("^[\-]+.*",re.MULTILINE)
header = re.compile("^(!+)(.*)",re.MULTILINE)
wikiLink=re.compile(r"\b[A-Z][a-z]+[A-Z][a-z]+.*?\b")
noWikiLink=re.compile(r"\b_[A-Z][a-z]+[A-Z]+[a-z]+.*?\b")
anchor = re.compile('\[(.+)\|(.+)\]')
href_patt = re.compile('<\s*?[Aa]\s+?')
style = re.compile('^\{\s*(\w*)')
line = re.compile('(^.*)',re.MULTILINE)
olist = re.compile('^(\d)+\.(.*)')
ulist = re.compile('^([\*]+)(.*)')
protocols=["http:","ftp:","mailto:"]

def isLinkName(word):
    """Returns true if word is a valid Wiki link"""
    return wikiLink.match(word)

class BuanDoc:

    def __init__(self, name, text):
        self.wikiNames = sets.Set() # new pages (words as valid Wiki names)
        self.pageName = name
        self.text = cgi.escape(text)
        self.make_html()
    
    def make_html(self):
        # wiki names
        self.text = wikiLink.sub(self.handle_wikiLink,self.text)
        # anchors
        self.text = anchor.sub(self.handle_anchor,self.text)
        # non wiki names : remove first character _
        self.text = noWikiLink.sub(self.handle_nowiki,self.text)
        # bold, italic, superscript
        self.text=bolds.sub(self.handle_bold,self.text)
        self.text=italics.sub(self.handle_italic,self.text)
        self.text=superscripts.sub(self.handle_superscript,self.text)
        # horizontal rule, header
        self.text = hr.sub(self.handle_hr,self.text)
        self.text = header.sub(self.handle_header,self.text)
        # css style
        self.in_style = False
        #self.text = style.sub(self.handle_style,self.text)
        # ordered and unordered lists
        self.ol_depth = 0
        self.ul_depth = 0
        self.text = line.sub(self.handle_line,self.text)
    
    def handle_wikiLink(self,mo):
        link = mo.group()
        self.wikiNames.add(link)
        return '<a href="BuanShow.pih?pageName=%s">%s</a>' % (link,link)

    def handle_nowiki(self,mo):
        link = mo.group()
        return link[1:]

    def handle_anchor(self,mo):
        txt,href = mo.groups()
        if href.startswith('<a href="BuanShow.pih?pageName'):
            href = href[:href[:-4].rfind('>')+1]
            return href + txt + '</a>'
        else:
            return '<a href="%s">%s</a>' %(href,txt)

    def handle_style(self,mo):
        style = mo.groups()[0]
        text = mo.groups()[1].lstrip()
        return '<pre class = "%s">%s</pre>' %(style,text)

    def handle_hr(self,mo):
        return '<hr>\n'

    def handle_header(self,mo):
        level = len(mo.groups()[0])
        return '<h%s>%s</h%s>' %(level,mo.groups()[1].rstrip(),level)

    def handle_line(self,mo):
        text = mo.groups()[0]
        res = ''
        # ordered list ?
        mo = olist.match(text)
        if mo:
            depth = int(mo.groups()[0])
            text = mo.groups()[1]
            if depth > self.ol_depth:
                self.ol_depth = depth
                res = '<ol>\n'
            else:
                while depth < self.ol_depth:
                    res += '</ol>\n'
                    self.ol_depth -= 1
            res += '<li>'
        else:
            while self.ol_depth:
                res += '</ol>\n'
                self.ol_depth -= 1
        # unordered list ?
        mo = ulist.match(text)
        if mo:
            depth = len(mo.groups()[0])
            text = mo.groups()[1]
            if depth > self.ul_depth:
                self.ul_depth = depth
                res = '<ul>\n'
            else:
                while depth < self.ul_depth:
                    res += '</ul>\n'
                    self.ul_depth -= 1
            res += '<li>'
        else:
            while self.ul_depth:
                res += '</ul>\n'
                self.ul_depth -= 1
        # style ?
        mo = style.match(text)
        if mo:
            stylename = mo.groups()[0]
            res = '<pre class = "%s">' %stylename
            text = text[mo.end():].lstrip()
            self.in_style = True
        # end of style ?
        if text.startswith('}'):
            res = '</pre>'
            text = text[1:]
            self.in_style = False
        br = ''
        if not res and not self.in_style:
            br = '<p>'
        return br + res + text

    def handle_bold(self,mo):
        return "<b>%s</b>" %mo.groups()[0]

    def handle_italic(self,mo):
        return "<i>%s</i>" %mo.groups()[0]

    def handle_superscript(self,mo):
        return "<sup>%s</sup>" %mo.groups()[0]

def test():
    txt = """[prvious|blabla.pih?jjj]
!!!New features
1. refactoring of the Template module : execution of scripts is passed to modules mod_(extension)
1.a new option if .ini file specifies the supported extensions
1. added ^Cheetah^ support
1. replaced gadfly by [_KirbyBase|http://www.netpromi.com/kirbybase.html], removed dbStorage
2. exception '''SCRIPT_ERROR''' : raise SCRIPT_ERROR,msg prints the message msg and stops the script execution
{ python print
}
WiKi _NoWiKi
!!!To do
* document ''virtual'' hosts
** first
** second
* other point
--
!!! salut
1. ca va etre dur
2. de trouver
2. quelque chose
1. de special
{ html <br>
<ll>
}
* a dire
[essai de lien|AvecLienWiki]
"""
    b = BuanDoc('test',txt)
    print b.text

if __name__=="__main__":
    test()
    print isLinkName('LineWiki')
    print isLinkName('_LienWiki')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.