Skip to content

Commit

Permalink
Added citation customization options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alphaharrius committed Nov 9, 2024
1 parent df761db commit 302baf8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 23 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="texmd",
version="0.1.0",
version="0.1.1",
description="A simple library that translates LaTeX to Markdown.",
package_dir={"": "src"},
packages=find_packages(where="src"),
Expand Down
49 changes: 49 additions & 0 deletions src/texmd/bib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from pydantic import BaseModel
from typing import List


class Author(BaseModel):
""" An author of a bibliography entry. """

first_name: str
""" The first name of the author. """

middle_name: str
""" The middle name of the author. """

last_name: str
""" The last name of the author. """


class Entry(BaseModel):
""" A bibliography entry. """

name: str
""" The name of the entry. """

authors: List[Author]
""" The authors of the entry. """

title: str
""" The title of the entry. """

year: str
""" The year of the entry. """

journal: str
""" The journal of the entry. """

volume: str
""" The volume of the entry. """

number: str
""" The number of the entry. """

pages: str
""" The pages of the entry. """

doi: str
""" The DOI of the entry. """

url: str
""" The URL of the entry. """
55 changes: 33 additions & 22 deletions src/texmd/tex.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from multipledispatch import dispatch
from abc import ABC, abstractmethod
from pybtex.database.input import bibtex, BibliographyData
from pybtex.database import Person

from texmd.md import (
MdNode,
Expand All @@ -28,6 +27,8 @@
MdMath,
MdEquation)

import texmd.bib as bib


class TexNode(BaseModel, ABC):
""" Base class for LaTeX nodes. """
Expand Down Expand Up @@ -506,10 +507,20 @@ def __init__(self, parser: 'TexParser'):
super().__init__(parser)

def convert(self, node: TexMacroNode) -> Generator[MdNode]:
def write_author(author: bib.Author) -> str:
first_abbr = author.first_name[0] + '.' if author.first_name else ''
middle_abbr = author.middle_name[0] + '.' if author.middle_name else ''
return f"{first_abbr} {middle_abbr} {author.last_name}"

def write_entry(entry: bib.Entry) -> str:
content: List[str] = [*(write_author(author) for author in entry.authors), entry.title, entry.year]
return ", ".join(content)

def _():
chars: TexTextNode = node.children[0].children[0]
cite_names = chars.text.replace(' ', '').split(',')
citations = (self.parser._get_citation(name) for name in cite_names)
citations = (write_entry(entry) for entry in citations if entry)
yield MdText(text="(*" + ", ".join(citations) + "*)")
return _()

Expand Down Expand Up @@ -538,10 +549,10 @@ def __init__(self):
(TexGroupNode, 'topic'): TopicGroupConverter(self),
(TexGroupNode, 'label:topic'): TopicGroupConverter(self),

(TexEnvNode, 'abstract'): AbstractConverter(self),
(TexMacroNode, 'eqref'): RefConverter(self),
(TexMacroNode, 'ref'): RefConverter(self),

(TexEnvNode, 'abstract'): AbstractConverter(self),
(TexEnvNode, 'equation'): EquationConverter(self),
(TexEnvNode, 'align'): EquationConverter(self),
(TexEnvNode, 'array'): EquationConverter(self),
Expand Down Expand Up @@ -706,25 +717,25 @@ def _get_ref_name(self, node: TexEnvNode) -> str:
""" Get the label of a LaTeX ref node. """
return self.__ref_names.get(id(node), '')

def _get_citation(self, name: str) -> str:
""" Get the citation of a LaTeX cite node, returns `<Unknown citation>` if the name is not found. """
def _get_citation(self, name: str) -> bib.Entry:
""" Get a citation by its name, returns `None` if the citation is not found. """
if not self.__citations or name not in self.__citations.entries:
return '<Unknown citation>'
return None

entry = self.__citations.entries[name]
citation = []
authors: List[Person] = entry.persons['author']
for author in authors:
first_name = " ".join(author.first_names)
middle_name = " ".join(author.middle_names)
last_name = " ".join(author.last_names)
first_abbrev = (first_name[0] + '.' if first_name else '')
middle_abbrev = (middle_name[0] + '.' if middle_name else '')
name = f"{first_abbrev} {middle_abbrev} {last_name}"
citation.append(name)
citation.append(', ')
title = entry.fields['title']
citation.append(title)
citation.append(', ')
year = entry.fields['year'] if 'year' in entry.fields else '<Unknown date>'
citation.append(year)
return "".join(citation)
get_author = lambda v: bib.Author(
first_name=" ".join(v.first_names),
middle_name=" ".join(v.middle_names),
last_name=" ".join(v.last_names))
authors: List[bib.Author] = [get_author(v) for v in entry.persons['author']]
return bib.Entry(
name=name,
authors=authors,
title=entry.fields['title'] if 'title' in entry.fields else '',
year=entry.fields['year'] if 'year' in entry.fields else '',
journal=entry.fields['journal'] if 'journal' in entry.fields else '',
volume=entry.fields['volume'] if 'volume' in entry.fields else '',
number=entry.fields['number'] if 'number' in entry.fields else '',
pages=entry.fields['pages'] if 'pages' in entry.fields else '',
doi=entry.fields['doi'] if 'doi' in entry.fields else '',
url=entry.fields['url'] if 'url' in entry.fields else '')

0 comments on commit 302baf8

Please sign in to comment.