Files
2026-etpgrf-site/etpgrf_site/typograph/views.py

47 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from django.shortcuts import render
from django.http import HttpResponse
# Импортируем напрямую из пакета etpgrf, который лежит в корне проекта
from etpgrf.typograph import Typographer
def index(request):
return render(request, template_name='typograph/index.html')
def process_text(request):
if request.method == 'POST':
text = request.POST.get(key='text', default='')
# Собираем настройки из формы
options = {
# Выпадающие списки
'langs': request.POST.get(key='langs', default='ru'),
'hanging_punctuation': request.POST.get(key='hanging_punctuation', default='both'),
'mode': request.POST.get(key='mode', default='mixed'),
'process_html': True,
# Чекбоксы
'quotes': request.POST.get('quotes') == 'on',
'layout': request.POST.get('layout') == 'on',
'unbreakables': request.POST.get('unbreakables') == 'on',
'hyphenation': request.POST.get('hyphenation') == 'on',
'symbols': request.POST.get('symbols') == 'on',
}
if options['hanging_punctuation'] == 'none':
options['hanging_punctuation'] = None
# Создаем экземпляр типографа
typo = Typographer(**options)
# Обрабатываем текст
processed = typo.process(text)
# Возвращаем фрагмент с явным указанием аргументов
return render(
request,
template_name='typograph/result_fragment.html',
context={'processed_text': processed}
)
return HttpResponse(status=405)