Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix flake8 warning
  • Loading branch information
ivanteoh committed Mar 8, 2017
1 parent 44b55bd commit 8464f06
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
20 changes: 15 additions & 5 deletions tests/test_publication_list.py
Expand Up @@ -31,16 +31,26 @@ def test_default(self):
expected = (
'<div class="publication-list">'
'<h3>2015</h3><ul>'
'<li class="publication".*>.*One article in 2015.*<a href="https://example.com/papers/a2015.html">details</a>.*<a href="/pdf/a2015.pdf">full text</a>.*</li>'
'<li class="publication".*>.*One conference in 2015.*<a href="https://example.com/papers/p2015.html">details</a>.*</li>'
'<li class="publication".*>.*One article in 2015.*'
'<a href="https://example.com/papers/a2015.html">details</a>.*'
'<a href="/pdf/a2015.pdf">full text</a>.*</li>'
'<li class="publication".*>.*One conference in 2015.*'
'<a href="https://example.com/papers/p2015.html">details</a>.*'
'</li>'
'</ul><h3>2010</h3><ul>'
'<li class="publication".*>.*One Book in 2010.*<a href="https://example.com/papers/b2010.html">details</a>.*<a href="http://example.org/b2010.pdf">full text</a>.*</li>'
'<li class="publication".*>.*One Book in 2010.*'
'<a href="https://example.com/papers/b2010.html">details</a>.*'
'<a href="http://example.org/b2010.pdf">full text</a>.*</li>'
'</ul></div>'
)
self.sample = '.. publication_list:: tests/data/publication_list/test.bib\n\t:highlight_author: Nikola Tesla'
self.sample = '.. publication_list:: tests/data/publication_list/' + \
'test.bib\n\t:highlight_author: Nikola Tesla'
self.deps = 'tests/data/publication_list/test.bib'
self.basic_test()
assert re.search(expected.replace('\n', '').strip(), self.html.replace('\n', '').strip())
assert re.search(
expected.replace('\n', '').strip(),
self.html.replace('\n', '').strip())


if __name__ == '__main__':
unittest.main()
70 changes: 49 additions & 21 deletions v7/publication_list/publication_list.py
Expand Up @@ -39,17 +39,23 @@


class Style(UnsrtStyle):
"""The style for publication listing. It hyperlinks the title to the detail page if user sets it.
"""The style for publication listing.
It hyperlinks the title to the detail page if user sets it.
"""

def __init__(self, detail_page_url):
super().__init__()
self.detail_page_url = detail_page_url

def format_title(self, e, which_field, as_sentence=True):
"Override the UnsrtStyle format_title(), so we have the title hyperlinked."
"""Override the UnsrtStyle format_title()
so we have the title hyperlinked.
"""

title = tag('strong')[super().format_title(e, which_field, as_sentence)]
title = tag('strong')[
super().format_title(e, which_field, as_sentence)]

if self.detail_page_url:
url = '/'.join((self.detail_page_url, e.label + '.html'))
Expand Down Expand Up @@ -90,7 +96,9 @@ def run(self):
highlight_authors = self.options.get('highlight_author', None)
if highlight_authors:
highlight_authors = highlight_authors.split(';')
style = Style(self.site.config['BASE_URL'] + detail_page_dir if detail_page_dir else None)
style = Style(
self.site.config['BASE_URL'] + detail_page_dir
if detail_page_dir else None)
self.state.document.settings.record_dependencies.add(self.arguments[0])

parser = Parser()
Expand All @@ -104,13 +112,15 @@ def run(self):

if bibtex_dir: # create the bibtex dir if the option is set
try:
os.makedirs(os.path.sep.join((self.output_folder, bibtex_dir)))
os.makedirs(
os.path.sep.join((self.output_folder, bibtex_dir)))
except OSError: # probably because the dir already exists
pass

if detail_page_dir: # create the detail page dir if the option is set
try:
os.makedirs(os.path.sep.join((self.output_folder, detail_page_dir)))
os.makedirs(
os.path.sep.join((self.output_folder, detail_page_dir)))
except OSError: # probably because the dir already exists
pass

Expand All @@ -123,20 +133,27 @@ def run(self):
html += '<h3>{}</h3>\n<ul>'.format(cur_year)

entry.label = label # Pass label to the style.
pub_html = list(style.format_entries((entry,)))[0].text.render_as('html')
if highlight_authors: # highlight one of several authors (usually oneself)
pub_html = list(
style.format_entries((entry,)))[0].text.render_as('html')
# highlight one of several authors (usually oneself)
if highlight_authors:
for highlight_author in highlight_authors:
pub_html = pub_html.replace(
highlight_author.strip(), '<strong>{}</strong>'.format(highlight_author), 1)
html += '<li class="publication" style="padding-bottom: 1em;">' + pub_html
highlight_author.strip(),
'<strong>{}</strong>'.format(highlight_author), 1)
html += '<li class="publication" style="padding-bottom: 1em;">' + \
pub_html

extra_links = ""

if 'fulltext' in entry.fields: # the link to the full text, usually a link to the pdf file
extra_links += '[<a href="{}">full text</a>] '.format(entry.fields['fulltext'])
# the link to the full text, usually a link to the pdf file
if 'fulltext' in entry.fields:
extra_links += '[<a href="{}">full text</a>] '.format(
entry.fields['fulltext'])

bibtex_fields = dict(entry.fields)
# Collect and remove custom links (fields starting with "customlink")
# Collect and remove custom links
# (fields starting with "customlink")
custom_links = dict()
for key, value in bibtex_fields.items():
if key.startswith('customlink'):
Expand All @@ -145,12 +162,14 @@ def run(self):
for key, value in custom_links.items():
extra_links += '[<a href="{}">{}</a>] '.format(value, key)

# Remove some fields for the publicly available BibTeX file since they are mostly only
# Remove some fields for the publicly available BibTeX file
# since they are mostly only
# used by this plugin.
for field_to_remove in ('abstract', 'fulltext'):
if field_to_remove in bibtex_fields:
del bibtex_fields[field_to_remove]
# Prepare for the bib file. Note detail_page_dir may need bib_data later.
# Prepare for the bib file.
# Note detail_page_dir may need bib_data later.
bibtex_entry = Entry(entry.type, bibtex_fields, entry.persons)
bib_data = BibliographyData(dict({label: bibtex_entry}))
bib_string = bib_data.to_string('bibtex')
Expand All @@ -171,7 +190,8 @@ def run(self):
'''.format('bibtex-' + label)
if bibtex_dir: # write bib files to bibtex_dir for downloading
bib_link = '{}/{}.bib'.format(bibtex_dir, label)
bib_data.to_file('/'.join([self.output_folder, bib_link]), 'bibtex')
bib_data.to_file('/'.join(
[self.output_folder, bib_link]), 'bibtex')

if extra_links or detail_page_dir or 'abstract' in entry.fields:
html += '<br>'
Expand All @@ -191,13 +211,18 @@ def run(self):
$('#' + id).show('fast');
$(target).text('abstract&#x25B2;')
}}
}})(this, '{}');">abstract&#x25BC;</a>] '''.format('abstract-' + label)
}})(this, '{}');">abstract&#x25BC;</a>] '''.format(
'abstract-' + label)

bibtex_display='<div id="{}" style="display:none"><pre>{}</pre></div>'.format(
none_display = '<div id="{}" style="display:none">' + \
'<pre>{}</pre></div>'
bibtex_display = none_display.format(
'bibtex-' + label, bib_string)

abstract_text = str(
LaTeXParser(entry.fields['abstract']).parse()) if 'abstract' in entry.fields else ''
LaTeXParser(
entry.fields['abstract']).parse()
) if 'abstract' in entry.fields else ''
if detail_page_dir: # render the details page of a paper
page_url = '/'.join((detail_page_dir, label + '.html'))
html += '[<a href="{}">details</a>] '.format(
Expand All @@ -215,12 +240,15 @@ def run(self):
'extra_links': extra_links + bibtex_display
}

if 'fulltext' in entry.fields and entry.fields['fulltext'].endswith('.pdf'):
if 'fulltext' in entry.fields and \
entry.fields['fulltext'].endswith('.pdf'):
context['pdf'] = entry.fields['fulltext']

self.site.render_template(
'publication.tmpl',
os.path.sep.join((self.output_folder, detail_page_dir, label + '.html')),
os.path.sep.join(
(self.output_folder, detail_page_dir, label + '.html')
),
context,
)

Expand Down

0 comments on commit 8464f06

Please sign in to comment.