gogy1992 / google-code-prettify

Automatically exported from code.google.com/p/google-code-prettify
Apache License 2.0
0 stars 0 forks source link

Django Templates Don't Work #35

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
When viewing Django template files (such at this [1]) in the source
browser the syntax highlighting makes the code extremely difficult to
read. Pygments supports Django template highlighting[2], and could be
a good reference. As for detection of the highlighter to use,
detection of the iconic django {{ }} tags would probably be a safe
enough indicator.

[1] - http://code.google.com/p/rietveld/source/browse/trunk/templates/all.html

[2] -
http://dev.pocoo.org/projects/pygments/browser/pygments/lexers/templates.py#L193

Original issue reported on code.google.com by kubasik.kevin on 19 May 2008 at 7:44

GoogleCodeExporter commented 8 years ago
Is the content of a {%...%} or {{...}} arbitrary python or is it restricted to 
python
identifiers?

Original comment by mikesamuel@gmail.com on 14 Jan 2009 at 8:19

GoogleCodeExporter commented 8 years ago
Oh, and to avoid unnecessarily introducing multi-line tokens into HTML, can the
{%...%} or {{...}} constructs contain newline characters or the < character?  
If so,
is it common for them to?

Original comment by mikesamuel@gmail.com on 14 Jan 2009 at 9:47

GoogleCodeExporter commented 8 years ago
Mike, the contents of both {%...%} (template tags) and {{...}} (variables, 
possibly
piped through filters), can potentially be anything in Unicode space. The first
"word" will typically be similar to a Python identifier, but the remainder are
arguments which can be arbitrary strings.

Neither grouping can contain newlines. They're unlikely to contain things like 
the
'<' character. Experience (fwiw) suggests that the most likely things are going 
to be
alphanumeric characters (including alphabetic characters outside the ASCII 
range, and
that does happen as the Django international audience is huge), whitespace and 
double
and single quotes. The rest are legal (except for newlines), but less common.

Original comment by malc...@pointy-stick.com on 19 Jan 2009 at 5:58

GoogleCodeExporter commented 8 years ago
Thanks.  

Are they typically short?

I ask since if I can't distinguish HTML source from HTML containing Django 
template
groups, I want to be able to come up with a definition that admits few false 
positives.

The definitions I'm leaning towards so far are:
  /\{\{[^\0xa-0xd\x85\u2028\u2029]{2,40}?\}\}/
and
  /\{\%[^\0xa-0xd\x85\u2028\u2029]{2,40}?\%\}/

http://docs.djangoproject.com/en/dev/topics/templates/#topics-templates 
describes the
content as a language that has a few keywords, pythonesque identifiers, uses | 
and :
for piping and parameterization, and which has numeric and double-quoted string
literals, without string interpolation.  It's unclear whether it has reserved
keywords but that distinction isn't important to me.

Original comment by mikesamuel@gmail.com on 20 Jan 2009 at 11:45