Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The application allows you to automate the process of generating a bibliography

Supported citation styles:
- ГОСТ Р 7.0.5-2008
- APA
- MLA

## Installation

Expand Down Expand Up @@ -53,10 +55,18 @@ to fill it without changing the original template:

5. Now it is possible to run the command inside the Docker container
as usual, passing needed arguments to the console application:
For GOST:
```shell
docker compose run app python main.py --citation gost --path_input /media/input.xlsx --path_output /media/output.docx
```

For APA:
```shell
docker compose run app python main.py --citation apa --path_input /media/input.xlsx --path_output /media/output.docx
```
For MLA:
```shell
docker compose run app python main.py --citation mla --path_input /media/input.xlsx --path_output /media/output.docx
```
Also, it is possible to omit the arguments to use their defaults:
```shell
docker compose run app python main.py
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def format(self) -> list[BaseCitationStyle]:

logger.info("Общее форматирование ...")

return sorted(self.formatted_items, key=lambda item: item.formatted)
return sorted(self.formatted_items, key=lambda item: item.formatted, reverse=False)
54 changes: 52 additions & 2 deletions src/formatters/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BookModel(BaseModel):
city="СПб.",
publishing_house="Просвещение",
year=2020,
pages=999,
pages=999hjrj,
)
"""

Expand All @@ -42,7 +42,7 @@ class InternetResourceModel(BaseModel):
InternetResourceModel(
article="Наука как искусство",
website="Ведомости",
link="https://www.vedomosti.ru/",
link="https://www.vedomosti.ru",
access_date="01.01.2021",
)
"""
Expand Down Expand Up @@ -78,3 +78,53 @@ class ArticlesCollectionModel(BaseModel):
publishing_house: str
year: int = Field(..., gt=0)
pages: str


class DissertationModel(BaseModel):
"""
Модель диссертации:

.. code-block::

DissertationModel(
author="Иванов И.М.",
title="Наука как искусство",
publisher_status="д-р. / канд.",
branch_of_sciences="экон.",
speciality_code="01.01.01",
city="СПб.",
year=2020,
pages=199,
)
"""
author: str
title: str
publisher_status: str
branch_of_sciences: str
speciality_code: str
city: str
year: int = Field(..., gt=0)
pages: int = Field(..., gt=0)


class ArticleFromTheJournalModel(BaseModel):
"""
Модель статьи из журнала:

.. code-block::

ArticleFromTheJournalModel(
authors="Иванов И.М., Петров С.Н.",
article_title="Наука как искусство",
collection_title="Образование и наука",
year=2020,
number_of_journal="10",
pages="25-30",
)
"""
authors: str
article_title: str
collection_title: str
year: int = Field(..., gt=0)
number_of_journal: str
pages: str
184 changes: 184 additions & 0 deletions src/formatters/styles/APA.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
"""
Стиль цитирования по APA
"""
from string import Template
from pydantic import BaseModel
from formatters.models import BookModel, InternetResourceModel, ArticlesCollectionModel, DissertationModel, ArticleFromTheJournalModel
from formatters.styles.base import BaseCitationStyle
from logger import get_logger

logger = get_logger(__name__)

class APABook(BaseCitationStyle):
"""
Форматирование для книг.
"""
data: BookModel

@property
def template(self) -> Template:
return Template(
"$authors ($year). $title. ($edition с.$pages). $city: $publishing_house."
)

def substitute(self) -> str:
logger.info('Форматирование книги "%s" ...', self.data.title)
return self.template.substitute(
authors=self.data.authors,
title=self.data.title,
edition=self.get_edition(),
city=self.data.city,
publishing_house=self.data.publishing_house,
year=self.data.year,
pages=self.data.pages,
)

def get_edition(self) -> str:
"""
Получение отформатированной информации об издательстве.

:return: Информация об издательстве.
"""

return f"{self.data.edition} изд." if self.data.edition else ""


class APAInternetResource(BaseCitationStyle):
"""
Форматирование для интернет-ресурсов.
"""
data: InternetResourceModel

@property
def template(self) -> Template:
return Template(
"$website. ($access_date). $article. Получено из $link"
)

def substitute(self) -> str:

logger.info('Форматирование интернет-ресурса "%s" ...', self.data.article)

return self.template.substitute(
article=self.data.article,
website=self.data.website,
link=self.data.link,
access_date=self.data.access_date,
)


class APACollectionArticle(BaseCitationStyle):
"""
Форматирование для статьи из сборника.
"""

data: ArticlesCollectionModel

@property
def template(self) -> Template:
return Template(
"$authors ($year). $article_title. $collection_title,$pages."
)

def substitute(self) -> str:

logger.info('Форматирование сборника статей "%s" ...', self.data.article_title)

return self.template.substitute(
authors=self.data.authors,
article_title=self.data.article_title,
collection_title=self.data.collection_title,
city=self.data.city,
publishing_house=self.data.publishing_house,
year=self.data.year,
pages=self.data.pages,
)


class APADissertation(BaseCitationStyle):
"""
Форматирование для диссертации.
"""
data: DissertationModel

@property
def template(self) -> Template:
return Template(
"$author ($year). $title. $publisher_status $branch_of_sciences наук. $city"
)

def substitute(self) -> str:
logger.info('Форматирование диссертаций "%s" ...', self.data.title)

return self.template.substitute(
author=self.data.author,
title=self.data.title,
publisher_status=self.data.publisher_status,
branch_of_sciences=self.data.branch_of_sciences,
speciality_code=self.data.speciality_code,
city=self.data.city,
year=self.data.year,
pages=self.data.pages,
)



class APAArticleFromTheJournal(BaseCitationStyle):
"""
Форматирование для статьи из журнала.
"""
data: ArticleFromTheJournalModel

@property
def template(self) -> Template:
return Template(
"$authors ($year). $article_title. $collection_title, ($number_of_journal), $pages."
)

def substitute(self) -> str:
logger.info('Форматирование статьи из журнала "%s"...', self.data.article_title)

return self.template.substitute(
authors=self.data.authors,
article_title=self.data.article_title,
collection_title=self.data.collection_title,
year=self.data.year,
number_of_journal=self.data.number_of_journal,
pages=self.data.pages,
)


class APACitationFormatter:
"""
Базовый класс для итогового форматирования списка источников.
"""

formatters_map = {
BookModel.__name__: APABook,
InternetResourceModel.__name__: APAInternetResource,
ArticlesCollectionModel.__name__: APACollectionArticle,
DissertationModel.__name__: APADissertation,
ArticleFromTheJournalModel.__name__: APAArticleFromTheJournal,
}

def __init__(self, models: list[BaseModel]) -> None:
"""
Конструктор.

:param models: Список объектов для форматирования
"""

formatted_items = []
for model in models:
formatted_items.append(self.formatters_map.get(type(model).__name__)(model)) # type: ignore

self.formatted_items = formatted_items

def format(self) -> list[BaseCitationStyle]:
"""
Форматирование списка источников.

:return:
"""

return sorted(self.formatted_items, key=lambda item: item.formatted)
Loading