Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from typing import List 

2from pybtex import PybtexEngine 

3from rich.console import Console 

4from unittest.mock import patch 

5import pybtex.richtext 

6 

7 

8console = Console() 

9 

10 

11def from_latex(latex): 

12 """  

13 Temporary patch until this issue isresolved: 

14 https://bitbucket.org/pybtex-devs/pybtex/issues/443/decoding-issue-in-from_latex-method-in-the 

15 """ 

16 import codecs 

17 import latexcodec # noqa 

18 from pybtex.markup import LaTeXParser 

19 

20 if not isinstance(latex, str): 

21 latex = codecs.decode(latex, 'ulatex') 

22 

23 return LaTeXParser(latex).parse() 

24 

25 

26class Citable: 

27 bibtex_files = None 

28 

29 def __init__(self): 

30 self.set_bibtex_files() 

31 

32 def get_bibtex_files(self) -> List: 

33 return [] 

34 

35 def add_bibtex_file(self, file): 

36 if not self.bibtex_files: 

37 self.set_bibtext_files() 

38 

39 if file not in self.bibtex_files: 

40 self.bibtex_files.append(file) 

41 

42 def set_bibtex_files(self): 

43 self.bibtex_files = self.get_bibtex_files() 

44 

45 def bibtex(self) -> str: 

46 bibtex_strings = [] 

47 for bibtex_file in self.bibtex_files: 

48 with open(bibtex_file, 'r') as f: 

49 bibtex_strings.append(f.read()) 

50 return "\n".join(bibtex_strings) 

51 

52 @patch.object(pybtex.richtext.Text, 'from_latex', from_latex) 

53 def bibliography(self, style="plain", output_backend="plaintext", **kwargs) -> str: 

54 engine = PybtexEngine() 

55 return engine.format_from_files( 

56 bib_files_or_filenames=self.bibtex_files, style=style, output_backend=output_backend, **kwargs 

57 ) 

58 

59 def print_bibliography(self, verbose=False, style="plain", output_backend="plaintext", **kwargs): 

60 bibliography = self.bibliography(style=style, output_backend=output_backend, **kwargs) 

61 if verbose: 

62 bibliography_style = "red bold" 

63 console.print( 

64 "--------------------------------------------------------------------------", style=bibliography_style 

65 ) 

66 console.print( 

67 "Please cite these references if using this app in an academic publication:", style=bibliography_style 

68 ) 

69 print(bibliography) 

70 if verbose: 

71 console.print( 

72 "--------------------------------------------------------------------------", style=bibliography_style 

73 ) 

74 

75 def print_bibtex(self): 

76 bibtex = self.bibtex() 

77 print(bibtex)