root / pycommunity / pycommunity / utils.py

Revision 78:b5dabe9762d1, 4.3 kB (checked in by Tarek Ziad?? <tarek@…>, 13 months ago)

rupy slides, starting up

Line 
1#!/usr/bin/python
2# -*- coding: UTF-8 -*-
3#
4# Copyright (c) 2006 Tarek Ziadé <tarek@ziade.org>
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License
8# as published by the Free Software Foundation; either version 2
9# of the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19# $Id: $
20""" helpers
21"""
22from docutils.core import publish_string
23from docutils.utils import SystemMessage
24import re
25
26# XXX this dependency suck, see how to remove it
27from glossary import GlossaryReader
28
29re_options = re.MULTILINE | re.IGNORECASE | re.DOTALL
30
31re_body = re.compile(r'<body>(.*?)</body>', re_options)
32re_title = re.compile(r'<h1 class="title">(.*?)</h1>', re_options)
33re_backlinks = re.compile(r'<div class="backlink">(.*?)</div>', re_options)
34re_head_title = re.compile(r'<title>(.*?)</title>', re_options)
35re_glossary = (re.compile(r'{{glossary:([a-zA-Z0-9_\-]*?)}}', re_options),
36               r"<acronym title='$2'>$1</a>")
37
38doc_replacer = r"""
39<a title='$1' alt='$1' href='$2'>$3</a>
40<img src="../media/expand.png" onclick="toggleElement('expandable-$1')"/>
41<div id="expandable-$1" class="expandable" style="display:none">
42$4
43<div>
44"""
45re_docs = (re.compile((r'{{(?:(?:recipe)|(?:tutorial)):'
46                        '([a-zA-Z0-9_\-]*?)(?:\:(.*?))?}}'),
47           re_options), doc_replacer)
48
49def _extractRe(expr, content):
50    """extracts a subcontent, given a compiled expression"""
51    content = expr.findall(content)
52    if content != []:
53        return content[0].strip()
54    return ''
55   
56def rest2Web(rest_file):
57    """returns an html content"""
58    source = open(rest_file).read()
59    try:
60        html = publish_string(source=source, writer_name='html')
61    except SystemMessage, e:
62        return 'Error', str(e)
63
64    body = _extractRe(re_body, html)
65    title = _extractRe(re_title, body)
66    return title, body
67
68def extractHtmlContent(html_file):
69    """extract the title and the body"""
70    html = open(html_file).read()
71    body = _extractRe(re_body, html)
72    title = _extractRe(re_title, body)
73    return title, removeBackLinks(body)
74
75def removeBackLinks(content):
76    """removes backlinks"""
77    return re_backlinks.sub('', content)
78
79def extractHeadTitle(html_file):
80    """gets the title"""
81    html = open(html_file).read()
82    return _extractRe(re_head_title, html)
83 
84
85def findDefinition(word, glossary):
86    try:
87        return glossary.getDefinition(word)
88    except KeyError:
89        return None
90
91def _grabContent(type, name, tutorial_folders, recipe_folders):
92    """gets a recipe or a turotial content"""
93    return """<b>Soon</b>"""
94
95def linkChecker(content, glossaryfile, tutorial_folders, recipe_folders):
96    """changes links on the fly"""
97    glossary = GlossaryReader(glossaryfile)
98
99    def glossary_replacer(match):
100        word = match.groups()[0]
101        definition = findDefinition(word, glossary)
102        if definition is None:
103            return match.group()
104        url = re_glossary[1]
105        url = url.replace('$1', word)
106        return url.replace('$2', definition)
107
108    def docs_replacer(match):
109        name = match.groups()[0]
110        text = match.groups()[1]
111       
112        if 'recipe' in match.group():
113            link = '../recipes/%s.html' % name
114            content = _grabContent('recipes', name, tutorial_folders,
115                                   recipe_folders)
116        else:
117            link = '../tutorials/%s.html' % name
118            content = _grabContent('tutorials', name, tutorial_folders,
119                                   recipe_folders)
120
121        url = re_docs[1]
122        url = url.replace('$1', name)
123        if text is not None:
124            url = url.replace('$3', text)
125        else:
126            url = url.replace('$3', name)
127
128        url = url.replace('$2', link)
129       
130        return url.replace('$4', content)
131
132    content = re_glossary[0].sub(glossary_replacer, content)
133    content = re_docs[0].sub(docs_replacer, content)
134    return content
Note: See TracBrowser for help on using the browser.