use regex in member variable, non-capture some regex groups

This commit is contained in:
Stephan Gabler 2012-07-23 10:36:38 +02:00
parent 01131ce5c8
commit a78c08a60b
3 changed files with 10 additions and 10 deletions

View file

@ -30,7 +30,7 @@ class Journal:
config.update(kwargs)
self.config = config
# TODO: comment regex
self.path_search = re.compile('[\[\(]?(([A-Z]:|/)\S*?\.(tif|tiff|jpg|jpeg|gif|png))[\[\)]?')
self.path_search = re.compile('[\[\(]?((?:[A-Z]:|/)\S*?\.(?:tif|tiff|jpg|jpeg|gif|png))[\[\)]?')
self.data_path = os.path.splitext(self.config['journal'])[0] + '_data'
# TODO: maybe move to setup in jrnl.py
if not os.path.exists(self.data_path):
@ -244,14 +244,12 @@ class Journal:
return date
def parse_for_images(self, body):
print 'in parse for images'
# TODO: parse also for name (like used in markdown [name](url))
path_start = re.compile('[\[\(]?([A-Z]:|/).*?\.(tif|tiff|jpg|jpeg|gif|png)[\[\)]?')
for word in body.split():
res = path_start.match(word)
if res and len(res.groups()) == 2 and os.path.exists(word.strip('()[]')):
res = self.path_search.match(word)
if res and os.path.exists(res.groups()[0]):
word = word.strip('()[]')
print word
ext = os.path.splitext(os.path.basename(word))[1]
random_name = 'img_' + self._random_string() + ext
shutil.copyfile(word, os.path.join(self.data_path, random_name))

View file

@ -6,6 +6,8 @@ import os
from jrnl.Journal import Journal
import shutil
# TODO: move test data to a data folder
class TestClasses(unittest.TestCase):
"""Test the behavior of the classes.
@ -40,13 +42,14 @@ class TestClasses(unittest.TestCase):
def test_rendering_md(self):
pass
def test_rendering_htm(self):
def test_rendering_html(self):
pass
def test_open_in_browser(self):
pass
def test_pathsearch_regex(self):
# TODO: test for windows path
true_positive = ['/Volumes/dedan/bla.png',
'/Users/dedan/projects/jrnl/tests/golden.jpg',
'/Volumes/dedan/test.png']
@ -55,7 +58,7 @@ class TestClasses(unittest.TestCase):
self.config['journal'] = os.path.join(self.test_data_path, 'empty.txt')
journal = Journal(config=self.config)
with open(os.path.join(self.test_data_path, 'url_test.txt')) as f:
results = [res[0] for res in journal.path_search.findall(f.read())]
results = [res for res in journal.path_search.findall(f.read())]
for tp in true_positive:
self.assertIn(tp, results)
for fp in false_positive:

View file

@ -1,6 +1,5 @@
ein normalser satz.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, (/Volumes/dedan/bla.png
) sed do eiusmod tempor incididunt ut (http://en.wikipedia.org/wiki/Generative_model) labore et dolore magna aliqua. Ut enim ad minim veniam, (/Users/dedan/projects/jrnl/tests/golden.jpg)
Lorem ipsum dolor sit amet, consectetur adipisicing elit, (/Volumes/dedan/bla.png) sed do eiusmod tempor incididunt ut (http://en.wikipedia.org/wiki/Generative_model) labore et dolore magna aliqua. Ut enim ad minim veniam, (/Users/dedan/projects/jrnl/tests/golden.jpg)
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor http://www.dict.cc/?s=descendant in reprehenderit in voluptate velit esse /Volumes/dedan/test.png cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non (/Volumes/dedan/bla.blub) proident, sunt in culpa qui officia deserunt mollit anim id est laborum.