| 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 | """ |
|---|
| 22 | from docutils.core import publish_string |
|---|
| 23 | from docutils.utils import SystemMessage |
|---|
| 24 | import re |
|---|
| 25 | |
|---|
| 26 | # XXX this dependency suck, see how to remove it |
|---|
| 27 | from glossary import GlossaryReader |
|---|
| 28 | |
|---|
| 29 | re_options = re.MULTILINE | re.IGNORECASE | re.DOTALL |
|---|
| 30 | |
|---|
| 31 | re_body = re.compile(r'<body>(.*?)</body>', re_options) |
|---|
| 32 | re_title = re.compile(r'<h1 class="title">(.*?)</h1>', re_options) |
|---|
| 33 | re_backlinks = re.compile(r'<div class="backlink">(.*?)</div>', re_options) |
|---|
| 34 | re_head_title = re.compile(r'<title>(.*?)</title>', re_options) |
|---|
| 35 | re_glossary = (re.compile(r'{{glossary:([a-zA-Z0-9_\-]*?)}}', re_options), |
|---|
| 36 | r"<acronym title='$2'>$1</a>") |
|---|
| 37 | |
|---|
| 38 | doc_js = r""" |
|---|
| 39 | <script language="javascript"> |
|---|
| 40 | function toggleExpandable(id) { |
|---|
| 41 | /* toggle display state */ |
|---|
| 42 | element = document.getElementById(id); |
|---|
| 43 | if (element) { |
|---|
| 44 | element.style.display = True; |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | </script> |
|---|
| 48 | """ |
|---|
| 49 | |
|---|
| 50 | doc_replacer = r""" |
|---|
| 51 | <a title='$1' alt='$1' href='$2'>$3</a> |
|---|
| 52 | <img src="../media/expand.png" onclick="toggleExpandable('expandable-$1')"/> |
|---|
| 53 | <div id="expandable-$1" class="expandable" style="display:none"> |
|---|
| 54 | $4 |
|---|
| 55 | <div> |
|---|
| 56 | """ |
|---|
| 57 | re_docs = (re.compile((r'{{(?:(?:recipe)|(?:tutorial)):' |
|---|
| 58 | '([a-zA-Z0-9_\-]*?)(?:\:(.*?))?}}'), |
|---|
| 59 | re_options), doc_replacer) |
|---|
| 60 | |
|---|
| 61 | def _extractRe(expr, content): |
|---|
| 62 | """extracts a subcontent, given a compiled expression""" |
|---|
| 63 | content = expr.findall(content) |
|---|
| 64 | if content != []: |
|---|
| 65 | return content[0].strip() |
|---|
| 66 | return '' |
|---|
| 67 | |
|---|
| 68 | def rest2Web(rest_file): |
|---|
| 69 | """returns an html content""" |
|---|
| 70 | source = open(rest_file).read() |
|---|
| 71 | try: |
|---|
| 72 | html = publish_string(source=source, writer_name='html') |
|---|
| 73 | except SystemMessage, e: |
|---|
| 74 | return 'Error', str(e) |
|---|
| 75 | |
|---|
| 76 | body = _extractRe(re_body, html) |
|---|
| 77 | title = _extractRe(re_title, body) |
|---|
| 78 | return title, body |
|---|
| 79 | |
|---|
| 80 | def extractHtmlContent(html_file): |
|---|
| 81 | """extract the title and the body""" |
|---|
| 82 | html = open(html_file).read() |
|---|
| 83 | body = _extractRe(re_body, html) |
|---|
| 84 | title = _extractRe(re_title, body) |
|---|
| 85 | return title, removeBackLinks(body) |
|---|
| 86 | |
|---|
| 87 | def removeBackLinks(content): |
|---|
| 88 | """removes backlinks""" |
|---|
| 89 | return re_backlinks.sub('', content) |
|---|
| 90 | |
|---|
| 91 | def extractHeadTitle(html_file): |
|---|
| 92 | """gets the title""" |
|---|
| 93 | html = open(html_file).read() |
|---|
| 94 | return _extractRe(re_head_title, html) |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | def findDefinition(word, glossary): |
|---|
| 98 | try: |
|---|
| 99 | return glossary.getDefinition(word) |
|---|
| 100 | except KeyError: |
|---|
| 101 | return None |
|---|
| 102 | |
|---|
| 103 | def linkChecker(content, glossaryfile): |
|---|
| 104 | """changes links on the fly""" |
|---|
| 105 | glossary = GlossaryReader(glossaryfile) |
|---|
| 106 | |
|---|
| 107 | def glossary_replacer(match): |
|---|
| 108 | word = match.groups()[0] |
|---|
| 109 | definition = findDefinition(word, glossary) |
|---|
| 110 | if definition is None: |
|---|
| 111 | return match.group() |
|---|
| 112 | url = re_glossary[1] |
|---|
| 113 | url = url.replace('$1', word) |
|---|
| 114 | return url.replace('$2', definition) |
|---|
| 115 | |
|---|
| 116 | def docs_replacer(match): |
|---|
| 117 | name = match.groups()[0] |
|---|
| 118 | text = match.groups()[1] |
|---|
| 119 | |
|---|
| 120 | if 'recipe' in match.group(): |
|---|
| 121 | link = '../recipes/%s.html' % name |
|---|
| 122 | else: |
|---|
| 123 | link = '../tutorials/%s.html' % name |
|---|
| 124 | url = re_docs[1] |
|---|
| 125 | url = url.replace('$1', name) |
|---|
| 126 | if text is not None: |
|---|
| 127 | url = url.replace('$3', text) |
|---|
| 128 | else: |
|---|
| 129 | url = url.replace('$3', name) |
|---|
| 130 | |
|---|
| 131 | return url.replace('$2', link) |
|---|
| 132 | |
|---|
| 133 | content = re_glossary[0].sub(glossary_replacer, content) |
|---|
| 134 | content = re_docs[0].sub(docs_replacer, content) |
|---|
| 135 | return content |
|---|