From 23bea90e9f6ec844d40e1b602ffc789dc5e878fd Mon Sep 17 00:00:00 2001 From: Bryan Date: Fri, 20 Feb 2015 20:53:40 -0500 Subject: [PATCH] #404 handling line breaks in markdown tags --- .../notes/migrations/0020_markdown_to_html.py | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/karmaworld/apps/notes/migrations/0020_markdown_to_html.py b/karmaworld/apps/notes/migrations/0020_markdown_to_html.py index 0db844f..4f6f113 100644 --- a/karmaworld/apps/notes/migrations/0020_markdown_to_html.py +++ b/karmaworld/apps/notes/migrations/0020_markdown_to_html.py @@ -3,8 +3,9 @@ from south.utils import datetime_utils as datetime from south.db import db from south.v2 import DataMigration from django.db import models + +import re import markdown -from notes.models import NoteMarkdown from notes import sanitizer class Migration(DataMigration): @@ -14,8 +15,26 @@ class Migration(DataMigration): # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. + + # find URLs in markdown tags + expr = re.compile('\[(.*)\]\(([^)]+)\)') + # find whitespace + whitespace = re.compile('[\s]+') + # remove all whitespace found in second group of a regex result + def remove_whitespace_from_tag(match): + parms = list(match.groups()) + parms[1] = whitespace.sub('', parms[1]) + return u'[{0}]({1})'.format(*parms) + + # filter non-empty markdown fields into the html field for notemarkdown in orm['notes.NoteMarkdown'].objects.exclude(markdown=""): - notemarkdown.html = sanitizer.sanitize_html_to_editable(markdown.markdown(notemarkdown.markdown)) + # find markdown link tags and remove all whitespace from the URLs. + # https://github.com/FinalsClub/karmaworld/issues/404 + md = expr.sub(remove_whitespace_from_tag, notemarkdown.markdown) + # convert markdown to HTML + md = markdown.markdown(md) + # cleanup converted HTML + notemarkdown.html = sanitizer.sanitize_html_to_editable(md) notemarkdown.save() def backwards(self, orm): -- 2.25.1