David Chambers avatar David Chambers committed 5672ab9

Mango's replacements no longer affect code snippets.

Comments (0)

Files changed (1)

 
 RE = {
 	'replacements': (
-		(re.compile(r'(?<!\\)\.\.\.(?!\.)'), u'\u2026'), # ... -> ellipsis
-		(re.compile(r' -- '), u'\u2009\u2014\u2009'),    # [space][hyphen][hyphen][space] -> [thin space][em dash][thin space]
-		(re.compile(r'(?<!\\)<<(?!<)'), u'\u00AB'),      # << -> «
-		(re.compile(r'(?<!\\)>>(?!>)'), u'\u00BB'),      # >> -> »
+		(re.compile(r'(?<!\\)\.\.\.(?!\.)'), u'\u2026'),     # ... -> ellipsis
+		(re.compile(r' -- '), u'\u2009\u2014\u2009'),        # [space][hyphen][hyphen][space] -> [thin space][em dash][thin space]
+		(re.compile(r'(?<!\\)&lt;&lt;(?!&lt;)'), u'\u00AB'), # << -> «
+		(re.compile(r'(?<!\\)&gt;&gt;(?!&gt;)'), u'\u00BB'), # >> -> »
 	),
 	'heading':       re.compile(r'(?m)\s*<(h[1-6])[^>]*>(?P<title>.+?)</\1>$(?P<html>[\s\S]*)'),
 
 		c = Context(parse_markdown(re.sub(RE['update_pipes'], '', capture)))
 		text = text.replace(capture, t.render(c))
 
-	# These replacements should not interfere with Markdown syntax.
-	if REPLACEMENTS:
-		for key, value in dict(RE['replacements']).items():
-			text = re.sub(key, value, text) 
-
 	md = markdown.Markdown(extensions = ('meta',) + MARKDOWN_EXTENSIONS)
 	html = md.convert(text)
 
+	if REPLACEMENTS: # perform replacements on HTML so that code snippets are not affected
+		fragments = re.split(r'(?s)(<code>.*?</code>|<pre>.*?</pre>)', html)
+		html = ''
+		for fragment in fragments:
+			if not re.match(r'(?s)^<(code|pre)>.*?</\1>$', fragment):
+				for key, value in dict(RE['replacements']).items():
+					fragment = re.sub(key, value, fragment)
+			html += fragment
+
 	meta = {}
 	if hasattr(md, 'Meta'): # this needs to be checked as this doesn't exist if the file was empty
 		for key, value in md.Meta.items(): # note: every item in md.Meta.items() is a list
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.