mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-12 01:18:31 +02:00
use regex in member variable, non-capture some regex groups
This commit is contained in:
parent
01131ce5c8
commit
a78c08a60b
3 changed files with 10 additions and 10 deletions
|
@ -30,7 +30,7 @@ class Journal:
|
||||||
config.update(kwargs)
|
config.update(kwargs)
|
||||||
self.config = config
|
self.config = config
|
||||||
# TODO: comment regex
|
# 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'
|
self.data_path = os.path.splitext(self.config['journal'])[0] + '_data'
|
||||||
# TODO: maybe move to setup in jrnl.py
|
# TODO: maybe move to setup in jrnl.py
|
||||||
if not os.path.exists(self.data_path):
|
if not os.path.exists(self.data_path):
|
||||||
|
@ -244,14 +244,12 @@ class Journal:
|
||||||
return date
|
return date
|
||||||
|
|
||||||
def parse_for_images(self, body):
|
def parse_for_images(self, body):
|
||||||
print 'in parse for images'
|
|
||||||
# TODO: parse also for name (like used in markdown [name](url))
|
# 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():
|
for word in body.split():
|
||||||
res = path_start.match(word)
|
res = self.path_search.match(word)
|
||||||
if res and len(res.groups()) == 2 and os.path.exists(word.strip('()[]')):
|
if res and os.path.exists(res.groups()[0]):
|
||||||
word = word.strip('()[]')
|
word = word.strip('()[]')
|
||||||
print word
|
|
||||||
ext = os.path.splitext(os.path.basename(word))[1]
|
ext = os.path.splitext(os.path.basename(word))[1]
|
||||||
random_name = 'img_' + self._random_string() + ext
|
random_name = 'img_' + self._random_string() + ext
|
||||||
shutil.copyfile(word, os.path.join(self.data_path, random_name))
|
shutil.copyfile(word, os.path.join(self.data_path, random_name))
|
||||||
|
|
|
@ -6,6 +6,8 @@ import os
|
||||||
from jrnl.Journal import Journal
|
from jrnl.Journal import Journal
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
# TODO: move test data to a data folder
|
||||||
|
|
||||||
class TestClasses(unittest.TestCase):
|
class TestClasses(unittest.TestCase):
|
||||||
"""Test the behavior of the classes.
|
"""Test the behavior of the classes.
|
||||||
|
|
||||||
|
@ -40,13 +42,14 @@ class TestClasses(unittest.TestCase):
|
||||||
def test_rendering_md(self):
|
def test_rendering_md(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_rendering_htm(self):
|
def test_rendering_html(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_open_in_browser(self):
|
def test_open_in_browser(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_pathsearch_regex(self):
|
def test_pathsearch_regex(self):
|
||||||
|
# TODO: test for windows path
|
||||||
true_positive = ['/Volumes/dedan/bla.png',
|
true_positive = ['/Volumes/dedan/bla.png',
|
||||||
'/Users/dedan/projects/jrnl/tests/golden.jpg',
|
'/Users/dedan/projects/jrnl/tests/golden.jpg',
|
||||||
'/Volumes/dedan/test.png']
|
'/Volumes/dedan/test.png']
|
||||||
|
@ -55,7 +58,7 @@ class TestClasses(unittest.TestCase):
|
||||||
self.config['journal'] = os.path.join(self.test_data_path, 'empty.txt')
|
self.config['journal'] = os.path.join(self.test_data_path, 'empty.txt')
|
||||||
journal = Journal(config=self.config)
|
journal = Journal(config=self.config)
|
||||||
with open(os.path.join(self.test_data_path, 'url_test.txt')) as f:
|
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:
|
for tp in true_positive:
|
||||||
self.assertIn(tp, results)
|
self.assertIn(tp, results)
|
||||||
for fp in false_positive:
|
for fp in false_positive:
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
ein normalser satz.
|
ein normalser satz.
|
||||||
|
|
||||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, (/Volumes/dedan/bla.png
|
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)
|
||||||
) 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
|
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.
|
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.
|
||||||
|
|
Loading…
Add table
Reference in a new issue