| Index: sitescripts/cms/converters.py |
| =================================================================== |
| --- a/sitescripts/cms/converters.py |
| +++ b/sitescripts/cms/converters.py |
| @@ -10,17 +10,17 @@ |
| # Adblock Plus is distributed in the hope that it will be useful, |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| # GNU General Public License for more details. |
| # |
| # You should have received a copy of the GNU General Public License |
| # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| -import re, jinja2, markdown |
| +import os, imp, re, jinja2, markdown |
| from ..utils import get_custom_template_environment |
| # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs are |
| # inserted into the <head> tag |
| orig_isBlockLevel = markdown.util.isBlockLevel |
| def isBlockLevel(tag): |
| if tag == "head": |
| return True |
| @@ -145,43 +145,63 @@ class RawConverter(Converter): |
| def get_html(self, source): |
| result = self.insert_localized_strings(source, html_escapes) |
| result = self.process_links(result) |
| return result |
| class MarkdownConverter(Converter): |
| def get_html(self, source): |
| def remove_unnecessary_entities(match): |
| - char = chr(int(match.group(1))) |
| + char = unichr(int(match.group(1))) |
| if char in html_escapes: |
| return match.group(0) |
| else: |
| return char |
| escapes = {} |
| for char in markdown.Markdown.ESCAPED_CHARS: |
| escapes[char] = "&#" + str(ord(char)) + ";" |
| for key, value in html_escapes.iteritems(): |
| escapes[key] = value |
| + md = markdown.Markdown(output="html5", extensions=["attr_list"]) |
| + md.preprocessors["html_block"].markdown_in_raw = True |
| + |
| result = self.insert_localized_strings(source, escapes) |
| - result = markdown.Markdown(output="html5", extensions=["attr_list"]).convert(result) |
| + result = md.convert(result) |
| result = re.sub(r"&#(\d+);", remove_unnecessary_entities, result) |
| result = self.process_links(result) |
| return result |
| class TemplateConverter(Converter): |
| def __init__(self, *args, **kwargs): |
| Converter.__init__(self, *args, **kwargs) |
| filters = { |
| "translate": self.translate, |
| "linkify": self.linkify, |
| "toclist": self.toclist, |
| } |
| + |
| + for filename in self._params["source"].list_files("filters"): |
| + root, ext = os.path.splitext(filename) |
| + if ext.lower() != ".py": |
| + continue |
| + |
| + path = "%s/%s" % ("filters", filename) |
| + code = self._params["source"].read_file(path) |
| + module = imp.new_module(root.replace("/", ".")) |
| + exec code in module.__dict__ |
| + |
| + func = os.path.basename(root) |
| + if not hasattr(module, func): |
| + raise Exception("Expected function %s not found in filter file %s" % (func, filename)) |
| + filters[func] = getattr(module, func) |
| + filters[func].module_ref = module # Prevent garbage collection |
| + |
| self._env = get_custom_template_environment(filters) |
| def get_html(self, source): |
| template = self._env.from_string(source) |
| return template.render(self._params) |
| def translate(self, name, page=None, links=[]): |
| if page == None: |