Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def assert_rst_equal(self, rstfile, expected, **kwargs):
pelican = Pelican(settings=get_settings(**kwargs))
reader = Readers(pelican.settings)
content = reader.read_file(base_path=RESOURCES_PATH, path=rstfile).content
self.assertEqual(normalize(content), normalize(expected))
test_data/content``
Remember to remove the output/ folder before that.
'''
base_path = os.path.dirname(os.path.abspath(__file__))
base_path = os.path.join(base_path, 'test_data')
content_path = os.path.join(base_path, 'content')
output_path = os.path.join(base_path, 'output')
settings_path = os.path.join(base_path, 'pelicanconf.py')
settings = read_settings(path=settings_path, override={
'PATH': content_path,
'OUTPUT_PATH': self.temp_path,
'CACHE_PATH': self.temp_cache,
'PLUGINS': [i18ns],
}
)
pelican = Pelican(settings)
pelican.run()
# compare output
out, err = subprocess.Popen(
['git', 'diff', '--no-ext-diff', '--exit-code', '-w', output_path,
self.temp_path], env={'PAGER': ''},
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
self.assertFalse(out, 'non-empty `diff` stdout:\n{}'.format(out))
self.assertFalse(err, 'non-empty `diff` stderr:\n{}'.format(out))
<p></p>
<p></p>
<p></p>
<a href=""></a>
<a class="menu" href=""></a>
<table></table>
<table id="bootstrapThis"></table>
<img href="">
"""
settings = get_settings()
settings['BOOTSTRAPIFY'] = {'p:nth-of-type(3)': ['third'],
'a.menu': ['btn', 'btn-lg'],
'table[id]': ['table', 'table-striped']}
content = Page(content=html_doc, settings=settings)
bootstrapify(content)
soup = BeautifulSoup(content._content, 'html.parser')
self.assertEqual(soup.select('p')[0].attrs.keys(), [])
self.assertEqual(soup.select('p')[1].attrs.keys(), [])
self.assertItemsEqual(soup.select('p')[2].attrs['class'], ['third'])
self.assertEqual(soup.select('a')[0].attrs.keys(), ['href'])
self.assertItemsEqual(soup.select('a')[1].attrs['class'],
['menu', 'btn', 'btn-lg'])
self.assertEqual(soup.select('table')[0].attrs.keys(), [])
self.assertItemsEqual(soup.select('table')[1].attrs['class'],
['table', 'table-striped'])
self.assertEqual(soup.select('table')[1].attrs['id'], 'bootstrapThis')
def test_settings(self):
html_doc = """
<p></p>
<a href=""></a>
<table></table>
<img class="testing" href="">
"""
settings = get_settings()
settings['BOOTSTRAPIFY'] = {'p': ['pclass'],
'a': ['aclass1', 'aclass2']}
content = Page(content=html_doc, settings=settings)
bootstrapify(content)
soup = BeautifulSoup(content._content, 'html.parser')
self.assertItemsEqual(soup.select('p')[0].attrs['class'], ['pclass'])
self.assertItemsEqual(soup.select('a')[0].attrs['class'],
['aclass1', 'aclass2'])
self.assertItemsEqual(soup.select('img')[0].attrs['class'],
['testing'])
def test_default(self):
html_doc = """
<p></p>
<a href=""></a>
<table></table>
<img href="">
"""
content = Page(content=html_doc, settings=get_settings())
bootstrapify(content)
soup = BeautifulSoup(content._content, 'html.parser')
self.assertItemsEqual(soup.select('table')[0].attrs['class'],
['table', 'table-striped', 'table-hover'])
self.assertItemsEqual(soup.select('img')[0].attrs['class'],
['img-responsive'])
self.assertEqual(soup.select('p')[0].attrs.keys(), [])
self.assertEqual(soup.select('a')[0].attrs.keys(), ['href'])
def test_append_class(self):
html_doc = """
<p></p>
<a href=""></a>
<table></table>
<img class="testing" href="">
"""
content = Page(content=html_doc, settings=get_settings())
bootstrapify(content)
soup = BeautifulSoup(content._content, 'html.parser')
self.assertItemsEqual(soup.select('table')[0].attrs['class'],
['table', 'table-striped', 'table-hover'])
self.assertItemsEqual(soup.select('img')[0].attrs['class'],
['testing', 'img-responsive'])
self.assertEqual(soup.select('p')[0].attrs.keys(), [])
self.assertEqual(soup.select('a')[0].attrs.keys(), ['href'])
def assert_rst_equal(self, rstfile, expected, **kwargs):
pelican = Pelican(settings=get_settings(**kwargs))
reader = Readers(pelican.settings)
content = reader.read_file(base_path=RESOURCES_PATH, path=rstfile).content
self.assertEqual(normalize(content), normalize(expected))
article['body'] = data.get('content', DEFAULT_BODY)
date = datetime.datetime.now()
article.set_metadata('date', date)
if page is None:
# it's an article
cat_info = dict(app.vars['categories'])[category]
article.set_metadata('category', cat_info['title'])
path = cat_info['path']
else:
# it's a page
path = dict(app.vars['pages'])[page]['path']
# XXX we might want to put it under the year directory
i = 1
filename = slugify(article['title'])
fullfilename = os.path.join(path, filename)
while os.path.exists(fullfilename + '.rst'):
fullfilename += str(i)
i += 1
with open(fullfilename + '.rst', 'w') as f:
f.write(article.render().encode('utf8'))
emit(EVENT_CREATED_CONTENT, article_path=fullfilename)
if page is None:
redirect('/category/%s/%s' % (category, filename + '.rst'))
else:
redirect('/page/%s/%s' % (page, filename + '.rst'))
def content_object_init(instance):
if isinstance(instance, contents.Static):
return
if 'summary' in instance.metadata:
return
if not hasattr(instance, '_summary') and instance._content is not None:
content = instance._content
firstP = FirstParagraphParser()
firstP.feed(content)
endCharA = '。'
endCharB = '.'
endPosA = firstP.data.find(endCharA)
endPosB = firstP.data.find(endCharB)
endPos = min(max(endPosA, endPosB), _MAX_SUMMARY_POS)
instance._summary = firstP.data[:endPos + 1 if endPos > 0 else None]
if endPos == _MAX_SUMMARY_POS:
instance._summary += ' …'
def register():
# TODO: why `lambda generators: AliasGenerator` doesn't work?
signals.get_generators.connect(get_generators)