r252390 - bibliograph.core/branches/1.0.3_FHNW/bibliograph/core

"Rodrigo Ristow" <svn-changes-z4DKO/[email protected]>
Newsgroups gmane.comp.web.zope.plone.collective.cvs
Message-ID <[email protected]>
Author: ristow
Date: Mon May 13 14:35:38 2013
New Revision: 252390

Modified:
   bibliograph.core/branches/1.0.3_FHNW/bibliograph/core/utils.py
Log:
fixed escape function:  check if it's a command (word), like: \url{}


Modified: bibliograph.core/branches/1.0.3_FHNW/bibliograph/core/utils.py
==============================================================================
--- bibliograph.core/branches/1.0.3_FHNW/bibliograph/core/utils.py	(original)
+++ bibliograph.core/branches/1.0.3_FHNW/bibliograph/core/utils.py	Mon May 13 14:35:38 2013
@@ -23,6 +23,7 @@
 
 _default_encoding = 'utf-8'
 _latex_cmds = ['\\','~', '#', '&', '%', '_']
+_latex_cmds_long = ['\\url',]
 _entity_mapping = {'&mdash;':'{---}',
                    '&ndash;':'{--}',
                    }
@@ -148,11 +149,21 @@
     return text
 
 ###############################################################################
-
 def _normalize(text, resolve_unicode=True, escape_latex=True):
+
+    def is_command_word(unitext, charpos):
+        """ Verify if it is a bibtex command (word).
+        """
+        cmd_fund=False
+        for cmd in  _latex_cmds_word:
+            if unitext[charpos:].startswith(cmd):
+                return cmd
+
     unitext = _decode(text)
     out = ""
-    for unichar in unitext:
+    charpos = 0
+    while charpos < len(unitext):
+        unichar = unitext[charpos]
         char_code = ord(unichar)
         char = _encode(unichar)
 
@@ -162,15 +173,24 @@
            out += char
         # Char must to be escaped (latex syntax)
         elif escape_latex and char in _latex_cmds:
-           out += '\\' + char
+            # check if it's a command (word), like: \url
+            cmd_word = is_command_word(unitext, charpos)
+            if cmd_word:
+                # do not escape
+                print "ignored cmd from escape:",cmd_word
+                out += cmd_word
+                charpos += len(cmd_word)-1
+            else:
+                # escape char
+                out += '\\' + char
         # Char must to be encoded
         elif resolve_unicode and unichar in _utf8enc2latex_mapping.keys():
            out += _encode(_utf8enc2latex_mapping[unichar])
         else:
            out += char
+        charpos += 1
     return _encode(_decode(out).replace(r'$}{$',''))
 
-
 ###############################################################################
 
 def _resolveEntities(text):

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and 
their applications. This 200-page book is written by three acclaimed 
leaders in the field. The early access version is available now. 
Download your free book today! http://p.sf.net/sfu/neotech_d2d_may
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.