Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export citation popup #1375

Merged
merged 20 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
env:
ANTHOLOGY_PREFIX: https://preview.aclanthology.org/${{ steps.extract_branch.outputs.branch }}
run: |
make ANTHOLOGY_PREFIX=${ANTHOLOGY_PREFIX} NOBIB=true check site preview
make ANTHOLOGY_PREFIX=${ANTHOLOGY_PREFIX} check site preview
- uses: mshick/add-pr-comment@v1
with:
message: |
Expand Down
10 changes: 8 additions & 2 deletions bin/anthology/anthology.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def import_file(self, filename):
collection_id = collection.get("id")
for volume_xml in collection:
volume = Volume.from_xml(
volume_xml, collection_id, self.venues, self.sigs, self.formatter
volume_xml,
collection_id,
self.venues,
self.sigs,
self.formatter,
)

# MJP 2021-05: no longer doing this since it kills branch previews.
Expand Down Expand Up @@ -102,7 +106,9 @@ def import_file(self, filename):

self.volumes[volume.full_id] = volume
for paper_xml in volume_xml.findall("paper"):
parsed_paper = Paper.from_xml(paper_xml, volume, self.formatter)
parsed_paper = Paper.from_xml(
paper_xml, volume, self.formatter, self.venues
)

# MJP 2021-05: no longer doing this since it kills branch previews.
# Don't merge with master prior to ingest date!
Expand Down
28 changes: 27 additions & 1 deletion bin/anthology/papers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@


class Paper:
def __init__(self, paper_id, ingest_date, volume, formatter):
def __init__(self, paper_id, ingest_date, volume, formatter, venue_index=None):
self.parent_volume = volume
self.formatter = formatter
self.venue_index = venue_index
self._id = paper_id
self._ingest_date = ingest_date
self._bibkey = False
Expand Down Expand Up @@ -138,6 +139,7 @@ def from_xml(xml_element, *args):
paper.attrib["thumbnail"] = data.PDF_THUMBNAIL_LOCATION_TEMPLATE.format(
paper.full_id
)
paper.attrib["citation"] = paper.as_markdown()

return paper

Expand Down Expand Up @@ -315,6 +317,30 @@ def as_bibtex(self, concise=False):
# Serialize it
return bibtex_make_entry(bibkey, bibtype, entries)

def as_markdown(self, concise=False):
"""Return a Markdown-formatted entry."""
title = self.get_title(form="text")

authors = ""
for field in ("author", "editor"):
if field in self.attrib:
people = [person[0] for person in self.get(field)]
num_people = len(people)
if num_people == 1:
authors = people[0].last
elif num_people == 2:
authors = f"{people[0].last} & {people[1].last}"
elif num_people == 3:
authors = f"{people[0].last}, {people[1].last}, & {people[2].last}"
elif num_people >= 4:
authors = f"{people[0].last} et al."

year = self.get("year")
venue = self.venue_index.get_main_venue(self.parent_volume_id)

url = self.get("url")
return f"[{title}]({url}) ({authors}, {venue} {year})"

def as_dict(self):
value = self.attrib
value["paper_id"] = self.paper_id
Expand Down
11 changes: 10 additions & 1 deletion bin/anthology/venues.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
class VenueIndex:
def __init__(self, srcdir=None):
self.venues, self.letters, self.joint_map = {}, {}, defaultdict(list)
self.acronyms_by_key = {}
self.acronyms = {} # acronym -> venue
self.acronyms_by_key = {} # slug -> acronym
self.venue_dict = None
if srcdir is not None:
self.load_from_dir(srcdir)
Expand Down Expand Up @@ -96,6 +97,7 @@ def load_from_dir(self, directory):
# encapsulation --MB)
self.venues[val["acronym"]] = val
self.acronyms_by_key[key] = val["acronym"]
self.acronyms[val["acronym"]] = val

if "oldstyle_letter" in val:
if not val["is_toplevel"]:
Expand Down Expand Up @@ -123,6 +125,13 @@ def get_by_letter(self, letter):
"""Get a venue acronym by first letter (e.g., Q -> TACL)."""
return self.letters.get(letter, None)

def get_by_acronym(self, acronym):
"""Get a venue object by its acronym (assumes acronyms are unique)."""
try:
return self.acronyms[acronym]
except KeyError:
log.critical(f"Unknown venue acronym: {acronym}")

def get_main_venue(self, anthology_id):
"""Get a venue acronym by anthology ID (e.g., acl -> ACL)."""
collection_id, volume_id, _ = deconstruct_anthology_id(anthology_id)
Expand Down
7 changes: 5 additions & 2 deletions bin/anthology/volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(
self._id = volume_id
self.ingest_date = ingest_date
self.formatter = formatter
self.venue_index = venue_index
self._set_meta_info(meta_data)
self.attrib["venues"] = venue_index.get_associated_venues(self.full_id)
self.attrib["sigs"] = sig_index.get_associated_sigs(self.full_id)
Expand Down Expand Up @@ -82,11 +83,13 @@ def from_xml(

front_matter_xml = volume_xml.find("frontmatter")
if front_matter_xml is not None:
front_matter = Paper.from_xml(front_matter_xml, volume, formatter)
front_matter = Paper.from_xml(
front_matter_xml, volume, formatter, venue_index
)
else:
# dummy front matter to make sure that editors of
# volume get registered as people in author database
front_matter = Paper("0", ingest_date, volume, formatter)
front_matter = Paper("0", ingest_date, volume, formatter, venue_index)
volume.add_frontmatter(front_matter)

return volume
Expand Down
2 changes: 1 addition & 1 deletion hugo/assets/css/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ $theme-colors: map-merge(
@import 'vendor/bootstrap/scss/list-group';
@import 'vendor/bootstrap/scss/close';
// @import 'vendor/bootstrap/scss/toasts';
// @import 'vendor/bootstrap/scss/modal';
@import 'vendor/bootstrap/scss/modal';
@import 'vendor/bootstrap/scss/tooltip';
// @import 'vendor/bootstrap/scss/popover';
// @import 'vendor/bootstrap/scss/carousel';
Expand Down
103 changes: 94 additions & 9 deletions hugo/layouts/papers/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@
<script>
$( document ).ready(function() {
if (ClipboardJS.isSupported()) {
new ClipboardJS(".btn-clipboard");
var clipboard = new ClipboardJS(".btn-clipboard");
clipboard.on('success', function(e) {
var src = $(e.trigger);
src.toggleClass("btn-primary btn-success");
src.children("i").toggleClass("far fa-clipboard fas fa-clipboard-check");
e.clearSelection();

setTimeout(function() {
src.toggleClass("btn-primary btn-success");
src.children("i").toggleClass("far fa-clipboard fas fa-clipboard-check");
}, 2000);
});
$(".btn-clipboard").removeClass("d-none");
}
});
Expand All @@ -51,6 +62,9 @@
{{ $anthology_id := .Params.anthology_id }}
{{ $volume_id := index (split .Params.anthology_id "-") 0 }}
{{ $paper := index (index .Site.Data.papers $volume_id) .Params.anthology_id }}
{{ $has_bib := fileExists (printf "/data-export/%s.bib" $anthology_id) }}
{{ $has_xml := fileExists (printf "/data-export/%s.xml" $anthology_id) }}
{{ $has_endf := fileExists (printf "/data-export/%s.endf" $anthology_id) }}
<section id="main">
<h2 id="title">
{{ with $paper.pdf }}
Expand Down Expand Up @@ -161,16 +175,16 @@ <h5 class="card-title">Abstract</h5>
-->
<dt class="acl-button-row">Bib Export formats:</dt>
<dd class="acl-button-row">
{{ if (fileExists (printf "/data-export/%s.bib" $anthology_id)) }}
{{ if $has_bib }}
<a class="btn btn-secondary btn-sm" href="{{ (printf "/%s.bib" $anthology_id) | relURL }}">BibTeX</a>
{{ end }}
{{ if (fileExists (printf "/data-export/%s.xml" $anthology_id)) }}
{{ if $has_xml }}
<a class="btn btn-secondary btn-sm" href="{{ (printf "/%s.xml" $anthology_id) | relURL }}">MODS XML</a>
{{ end }}
{{ if (fileExists (printf "/data-export/%s.endf" $anthology_id)) }}
{{ if $has_endf }}
<a class="btn btn-secondary btn-sm" href="{{ (printf "/%s.endf" $anthology_id) | relURL }}">EndNote</a>
{{ end }}
{{ if (fileExists (printf "/data-export/%s.bib" $anthology_id)) }}
{{ if $has_bib }}
<button type="button" class="btn btn-clipboard btn-secondary btn-sm d-none" data-clipboard-text="{{ readFile (printf "/data-export/%s.bib" $anthology_id) }}"><i class="far fa-clipboard pr-2"></i>Copy BibTeX to Clipboard</button>
{{ end }}
</dd>
Expand Down Expand Up @@ -206,11 +220,9 @@ <h5 class="card-title">Abstract</h5>
</a>
{{ end }}
{{ end }}
{{ if (fileExists (printf "/data-export/%s.bib" $anthology_id)) }}
<a class="btn btn-secondary" href="{{ (printf "/%s.bib" $anthology_id) | relURL }}" title="Export '{{ $paper.title | htmlEscape }}' to bib format">
<i class="fas fa-file-export"></i><span class="pl-2 transform-lower-sm">Bib</span><span class="d-none d-sm-inline">TeX</span>
<a type="button" class="btn btn-secondary" title="Open dialog for exporting citations" data-toggle="modal" data-target="#citeModal">
<i class="fas fa-file-export"></i><span class="pl-2">Cite</span>
</a>
{{ end }}
<a class="btn btn-secondary" href="https://scholar.google.com/scholar?{{ (querify "q" $paper.title) | safeURL }}" title="Search for '{{ $paper.title | htmlEscape }}' on Google Scholar">
<i class="ai ai-google-scholar"></i><span class="pl-sm-2 d-none d-sm-inline">Search</span>
</a>
Expand All @@ -223,5 +235,78 @@ <h5 class="card-title">Abstract</h5>
</div>
</div>
<hr />

<div class="modal fade" id="citeModal" tabindex="-1" role="dialog" aria-labelledby="citeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="citeModalLabel">Export citation</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<ul class="nav nav-tabs mb-2" id="citeFormats" role="tablist">
<li class="nav-item">
<a class="nav-link {{ if not $has_bib }}active{{ end }}" data-toggle="list" href="#citeMarkdown" role="tab" aria-controls="citeMarkdown" aria-selected="{{ if $has_bib }}false{{ else }}true{{ end }}">Markdown</a>
</li>
<li class="nav-item">
<a class="nav-link {{ if not $has_bib }}disabled{{ else }}active{{ end }}" data-toggle="list" href="#citeBibtex" role="tab" aria-controls="citeBibtex" aria-selected="{{ if $has_bib }}true{{ else }}false{{ end }}">BibTeX</a>
</li>
<li class="nav-item">
<a class="nav-link {{ if not $has_xml }}disabled{{ end }}" data-toggle="list" href="#citeMods" role="tab" aria-controls="citeMods" aria-selected="false">MODS XML</a>
</li>
<li class="nav-item">
<a class="nav-link {{ if not $has_endf }}disabled{{ end }}" data-toggle="list" href="#citeEndnote" role="tab" aria-controls="citeEndnote" aria-selected="false">Endnote</a>
</li>
</ul>

<div class="tab-content" id="citeFormatsContent">
<div class="tab-pane" id="citeMarkdown" role="tabpanel">
<p id="citeMarkdownContent" class="text-monospace small bg-light border p-2">
{{- $paper.citation -}}
</p>
<div class="modal-footer pb-1">
<button type="button" class="btn btn-clipboard btn-primary d-none" data-clipboard-target="#citeMarkdownContent"><i class="far fa-clipboard pr-2"></i>Copy to Clipboard</button>
</div>
</div>
<div class="tab-pane active" id="citeBibtex" role="tabpanel">
{{- if $has_bib -}}
<pre id="citeBibtexContent" class="bg-light border p-2" style="max-height: 50vh;">
{{- readFile (printf "/data-export/%s.bib" $anthology_id) -}}
</pre>
<div class="modal-footer pb-1">
<a class="btn btn-secondary" href="{{ (printf "/%s.bib" $anthology_id) | relURL }}"><i class="fas fa-download pr-2"></i>Download as File</a>
<button type="button" class="btn btn-clipboard btn-primary d-none" data-clipboard-target="#citeBibtexContent"><i class="far fa-clipboard pr-2"></i>Copy to Clipboard</button>
</div>
{{- end -}}
</div>
<div class="tab-pane" id="citeMods" role="tabpanel">
{{- if $has_xml -}}
<pre id="citeModsContent" class="bg-light border p-2" style="max-height: 50vh;">
{{- readFile (printf "/data-export/%s.xml" $anthology_id) -}}
</pre>
<div class="modal-footer pb-1">
<a class="btn btn-secondary" href="{{ (printf "/%s.xml" $anthology_id) | relURL }}"><i class="fas fa-download pr-2"></i>Download as File</a>
<button type="button" class="btn btn-clipboard btn-primary d-none" data-clipboard-target="#citeModsContent"><i class="far fa-clipboard pr-2"></i>Copy to Clipboard</button>
</div>
{{- end -}}
</div>
<div class="tab-pane" id="citeEndnote" role="tabpanel">
{{- if $has_endf -}}
<pre id="citeEndnoteContent" class="bg-light border p-2" style="max-height: 50vh;">
{{- readFile (printf "/data-export/%s.endf" $anthology_id) -}}
</pre>
<div class="modal-footer pb-1">
<a class="btn btn-secondary" href="{{ (printf "/%s.endf" $anthology_id) | relURL }}"><i class="fas fa-download pr-2"></i>Download as File</a>
<button type="button" class="btn btn-clipboard btn-primary d-none" data-clipboard-target="#citeEndnoteContent"><i class="far fa-clipboard pr-2"></i>Copy to Clipboard</button>
</div>
{{- end -}}
</div>
</div>
</div>
</div>
</div>
</div>
</section>
{{ end }}