Anonymous avatar Anonymous committed beb0b1f

template won't templatize content that's already a full html document

Comments (0)

Files changed (2)

nom/plugins/simpletemplate.py

 from string import Template
 from nom.filesystem import open_input_file
 from nom.handler import Filter, register_handler
+from nom.utils.html import has_html_tag
+
 from os.path import join, basename, splitext, exists
 
 import logging
             fh.close()
 
     def _process_string(self, string, env):
+        # Skip template processing altogether if the string already
+        # contains a full html document.
+        isdoc = env.get('is-complete-html', has_html_tag(string))
+        if isdoc: return (string,env)
+
         basepath = nom.config.base_url
         if basepath.startswith('http://'):
             basepath = '/' + basepath[7:].partition('/')[2]

nom/utils/html.py

+from HTMLParser import HTMLParser
+        
+def has_html_tag(content):
+
+    p = _FirstTagFinder()
+    fragment = content[:1024]
+    try:
+        p.feed(fragment)
+    except _TagFound, e:
+        return e.tag == 'html'
+
+    return False
+
+
+# There's no tag iterator interface to HTMLParser, so we use
+# exceptions to force a nonlocal exit.
+class _TagFound(Exception):
+    def __init__(self, tag):
+        self.tag = tag
+
+class _FirstTagFinder(HTMLParser):
+    def handle_starttag(self, tag, attrs):
+        raise _TagFound(tag)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.