add: настройки типографа (01)
This commit is contained in:
@@ -1,22 +1,46 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
from etpgrf import Typographer
|
||||
|
||||
# Инициализируем типограф один раз (как рекомендовано в спеке)
|
||||
# Можно вынести настройки в settings.py, но пока так
|
||||
typo = Typographer(langs='ru', process_html=True, hanging_punctuation='both')
|
||||
# Импортируем напрямую из пакета 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)
|
||||
|
||||
# Возвращаем только фрагмент для HTMX
|
||||
return render(request, template_name='typograph/result_fragment.html', context={'processed_text': processed})
|
||||
# Возвращаем фрагмент с явным указанием аргументов
|
||||
return render(
|
||||
request,
|
||||
template_name='typograph/result_fragment.html',
|
||||
context={'processed_text': processed}
|
||||
)
|
||||
|
||||
return HttpResponse(status=405)
|
||||
|
||||
Reference in New Issue
Block a user