2022-01-20 07:02:55 +01:00
# -*- coding: utf-8 -*-
import os
from pylatexenc . latex2text import LatexNodes2Text
from pybtex . database import parse_file
from lektor . pluginsystem import Plugin
class CitationPlugin ( Plugin ) :
name = ' lektor-citation '
description = u ' This Plugin should extend lektor with APA-style citations using bibtex files. It is based on the known lektor-bibtex-support plugin by arunpersaud. '
def __init__ ( self , env , id ) :
super ( ) . __init__ ( env , id )
config = self . get_config ( )
self . bibfile = config . get ( ' Bibtex.file ' , [ ] ) . strip ( )
self . bib_data = parse_file ( os . path . join ( env . root_path , ' assets ' , self . bibfile ) )
def citation_entries ( self ) :
return self . bib_data . entries
def citation_entry ( self , id ) :
return self . bib_data . entries [ id ]
2022-01-21 13:35:51 +01:00
def citation_short_output ( self , id , link = None ) :
2022-01-20 23:53:50 +01:00
e = self . citation_entry ( id )
2022-01-21 13:35:51 +01:00
if " url " in e . fields . keys ( ) and len ( e . fields [ ' url ' ] ) > 0 :
link = e . fields [ ' url ' ]
else :
link = " ? "
2022-01-20 23:53:50 +01:00
authors = " "
lAuthor = e . persons [ ' author ' ]
n = 1
for author in lAuthor :
2022-01-21 13:35:51 +01:00
prelast = author . prelast_names
if len ( prelast ) > 0 :
for item in prelast :
authors + = " {i} " . format ( i = str ( item ) )
2022-01-20 23:53:50 +01:00
authors + = str ( author . last_names [ 0 ] )
2022-01-21 13:35:51 +01:00
2022-01-20 23:53:50 +01:00
if len ( lAuthor ) > 1 :
if n == ( len ( lAuthor ) - 1 ) :
authors + = " \ & "
elif n < ( len ( lAuthor ) - 1 ) :
authors + = " , "
n = n + 1
year = e . fields [ ' year ' ]
edition = " "
if ' edition ' in e . fields . keys ( ) :
2022-01-21 13:35:51 +01:00
edition = e . fields [ ' edition ' ]
2022-01-20 23:53:50 +01:00
edition = " ( {ed} . Aufl.) " . format ( ed = edition )
else :
edition = " "
if ' publisher ' in e . fields . keys ( ) :
publisher = e . fields [ ' publisher ' ]
if ' address ' in e . fields . keys ( ) :
location = e . fields [ ' address ' ]
2022-01-21 13:35:51 +01:00
publisher = " {location} : {publisher} . " . format ( location = location , publisher = publisher )
2022-01-20 23:53:50 +01:00
elif publisher :
publisher = " {publisher} . " . format ( publisher = publisher )
else :
publisher = " "
output = ' <li id= " {eid} " ><a href= " {link} " class= " litref " > {authors} ( {pubYear} ).</a> <em> {title} </em> {edition} . {publisher} ' . format ( eid = id , link = link , authors = authors , pubYear = year , title = e . fields [ ' title ' ] , edition = edition , publisher = publisher )
return output
2022-01-20 07:02:55 +01:00
2022-01-20 23:53:50 +01:00
2022-01-20 07:02:55 +01:00
def on_setup_env ( self , * * extra ) :
2022-01-20 23:53:50 +01:00
def decode_filter ( value ) :
return LatexNodes2Text ( ) . latex_to_text ( value )
2022-01-20 07:02:55 +01:00
self . env . jinja_env . globals [ ' citation_entries ' ] = self . citation_entries
self . env . jinja_env . globals [ ' citation_entry ' ] = self . citation_entry
2022-01-20 23:53:50 +01:00
self . env . jinja_env . globals [ ' citation_short_output ' ] = self . citation_short_output
self . env . jinja_env . filters [ ' decode ' ] = decode_filter
2022-01-20 07:02:55 +01:00