Use Mako instead of Pystache

This commit is contained in:
Samuel Mehrbrodt 2017-06-12 19:43:24 +02:00
parent ac2f92f8ac
commit a1e1092b10
5 changed files with 76 additions and 86 deletions

View File

@ -664,26 +664,3 @@ def transpose_chord(chord, transpose_value, notation):
else:
transposed_chord += note + rest
return transposed_chord
def make_list(array):
"""
converts an ordinary list into a mustache ready dict construct augmented with some information to enable special
formatting features with the first, second to last and last element.
:param array: input list
:return: mustache ready and augmented dict
"""
if len(array) < 0:
return False
result = []
for i in range(len(array)):
result.append({'entry': array[i]})
if i == 0:
result[i]['first'] = True
if i == len(array) - 1:
result[i]['last'] = True
if i == len(array) - 1 or i == len(array) - 2:
result[i]['last_or_penultimate'] = True
return result

View File

@ -23,7 +23,7 @@
import logging
import os
import shutil
import pystache
import mako
from PyQt5 import QtCore, QtWidgets
from sqlalchemy.sql import and_, or_
@ -31,13 +31,13 @@ from sqlalchemy.sql import and_, or_
from openlp.core.common import Registry, AppLocation, Settings, check_directory_exists, UiStrings, translate
from openlp.core.lib import MediaManagerItem, ItemCapabilities, PluginStatus, ServiceItemContext, \
check_item_selected, create_separated_list
from openlp.core.lib.ui import create_widget_action
from openlp.core.lib.ui import create_widget_action, critical_error_message_box
from openlp.core.common.languagemanager import get_natural_key
from openlp.plugins.songs.forms.editsongform import EditSongForm
from openlp.plugins.songs.forms.songmaintenanceform import SongMaintenanceForm
from openlp.plugins.songs.forms.songimportform import SongImportForm
from openlp.plugins.songs.forms.songexportform import SongExportForm
from openlp.plugins.songs.lib import VerseType, clean_string, delete_song, make_list
from openlp.plugins.songs.lib import VerseType, clean_string, delete_song
from openlp.plugins.songs.lib.db import Author, AuthorType, Song, Book, MediaFile, SongBookEntry, Topic
from openlp.plugins.songs.lib.ui import SongStrings
from openlp.plugins.songs.lib.openlyricsxml import OpenLyrics, SongXML
@ -663,30 +663,37 @@ class SongMediaItem(MediaManagerItem):
item.raw_footer.append(translate('SongsPlugin.MediaItem', 'CCLI License: ') +
Settings().value('core/ccli number'))
footer_template = Settings().value('songs/footer template').replace('\n', '').replace(' ', '')
footer_template = Settings().value('songs/footer template')
# Keep this in sync with the list in songstab.py
item.footer_html = pystache.render(footer_template, {
vars = {
'title': song.title,
'alternate_title': song.alternate_title,
'authors_none_label': translate('OpenLP.Ui', 'Written by'),
'authors_none': make_list(authors_none),
'authors_none': authors_none,
'authors_words_label': AuthorType.Types[AuthorType.Words],
'authors_words': make_list(authors_words),
'authors_words': authors_words,
'authors_music_label': AuthorType.Types[AuthorType.Music],
'authors_music': make_list(authors_music),
'authors_music': authors_music,
'authors_words_music_label': AuthorType.Types[AuthorType.WordsAndMusic],
'authors_words_music': make_list(authors_words_music),
'authors_words_music': authors_words_music,
'authors_translation_label': AuthorType.Types[AuthorType.Translation],
'authors_translation': make_list(authors_translation),
'authors_words_all': make_list(authors_words + authors_words_music),
'authors_music_all': make_list(authors_music + authors_words_music),
'authors_translation': authors_translation,
'authors_words_all': authors_words + authors_words_music,
'authors_music_all': authors_music + authors_words_music,
'copyright': song.copyright,
'songbook_entries': make_list(songbooks),
'songbook_entries': songbooks,
'ccli_license': Settings().value('core/ccli number'),
'ccli_license_label': translate('SongsPlugin.MediaItem', 'CCLI License'),
'ccli_number': song.ccli_number,
'topics': make_list([topic.name for topic in song.topics])
})
'topics': [topic.name for topic in song.topics]
}
try:
item.footer_html = mako.template.Template(footer_template).render_unicode(**vars).replace('\n', '')
except mako.exceptions.SyntaxException:
log.error('Failed to render Song footer html:\n' + mako.exceptions.text_error_template().render())
critical_error_message_box(message=translate('SongsPlugin.MediaItem',
'Failed to render Song footer html.\nSee log for details'))
return authors_all

View File

@ -135,7 +135,7 @@ class SongsTab(SettingsTab):
placeholders = [
# placeholder, description, can be empty, is a list
['title', translate('SongsPlugin.SongsTab', 'Song Title'), False, False],
['alternate_title', translate('SongsPlugin.SongsTab', 'Alternate Title'), False, False],
['alternate_title', translate('SongsPlugin.SongsTab', 'Alternate Title'), True, False],
['written_by', const.format(translate('SongsPlugin.SongsTab', 'Written By')), True, False],
['authors_none', translate('SongsPlugin.SongsTab', 'Authors when type is not set'), False, True],
['authors_words_label', const.format(AuthorType.Types[AuthorType.Words]), False, False],
@ -168,20 +168,14 @@ class SongsTab(SettingsTab):
('&nbsp;²' if placeholder[3] else ''))
placeholder_info += '</table>'
placeholder_info += '\n<br/>¹ {}'.format(translate('SongsPlugin.SongsTab', 'can be empty'))
placeholder_info += '\n<br/>² {}:<pre>{{#first}} <i>True</i> {}<br/>{{entry}} {}<br/>' \
'{{#last}} <i>True</i> {}<br/>{{#last_or_penultimate}} <i>True</i> {}</pre>'\
.format(translate('SongsPlugin.SongsTab', 'list of entries, can be empty'),
translate('SongsPlugin.SongsTab', 'for first element of list'),
translate('SongsPlugin.SongsTab', 'iterates over all entries'),
translate('SongsPlugin.SongsTab', 'for last element'),
translate('SongsPlugin.SongsTab', 'for last and second to last element'))
placeholder_info += '\n<br/>² {}'.format(translate('SongsPlugin.SongsTab', 'list of entries, can be empty'))
self.footer_placeholder_info.setHtml(placeholder_info)
self.footer_placeholder_info.setReadOnly(True)
self.footer_info_label.setText(translate('SongsPlugin.SongsTab', 'How to use Footers:'))
self.footer_desc_label.setText('{} (<a href="http://mustache.github.io/mustache.5.html">{}</a>):'
self.footer_desc_label.setText('{} (<a href="http://docs.makotemplates.org">{}</a>):'
.format(translate('SongsPlugin.SongsTab', 'Footer Template'),
translate('SongsPlugin.SongsTab', 'Mustache Syntax')))
translate('SongsPlugin.SongsTab', 'Mako Syntax')))
self.footer_reset_button.setText(translate('SongsPlugin.SongsTab', 'Reset Template'))
def on_search_as_type_check_box_changed(self, check_state):

View File

@ -68,46 +68,59 @@ __default_settings__ = {
'songs/chord notation': 'english', # Can be english, german or neo-latin
'songs/mainview chords': False,
'songs/disable chords import': False,
'songs/footer template': """{{title}}<br/>
'songs/footer template': """\
${title}<br/>
{{#authors_none}}
{{#first}}{{authors_none_label}}:&nbsp;{{/first}}
{{entry}}{{^last}},&nbsp;{{/last}}
{{#last}}<br/>{{/last}}
{{/authors_none}}
{{#authors_words_music}}
{{#first}}{{authors_words_music_label}}:&nbsp;{{/first}}
{{entry}}{{^last}},&nbsp;{{/last}}
{{#last}}<br/>{{/last}}
{{/authors_words_music}}
{{#authors_words}}
{{#first}}{{authors_words_label}}:&nbsp;{{/first}}
{{entry}}{{^last}},&nbsp;{{/last}}
{{#last}}<br/>{{/last}}
{{/authors_words}}
{{#authors_music}}
{{#first}}{{authors_music_label}}:&nbsp;{{/first}}
{{entry}}{{^last}},&nbsp;{{/last}}
{{#last}}<br/>{{/last}}
{{/authors_music}}
{{#authors_translation}}
{{#first}}{{authors_translation_label}}:&nbsp;{{/first}}
{{entry}}{{^last}},&nbsp;{{/last}}
{{#last}}<br/>{{/last}}
{{/authors_translation}}
%if authors_none:
<%
authors = ", ".join(authors_none)
%>
${authors_none_label}:&nbsp;${authors}<br/>
%endif
{{#copyright}}
&copy;&nbsp;{{copyright}}<br/>
{{/copyright}}
%if authors_words_music:
<%
authors = ", ".join(authors_words_music)
%>
${authors_words_music_label}:&nbsp;${authors}<br/>
%endif
{{#songbook_entries}}
{{entry}}{{^last}},&nbsp;{{/last}}
{{#last}}<br/>{{/last}}
{{/songbook_entries}}
%if authors_words:
<%
authors = ", ".join(authors_words)
%>
${authors_words_label}:&nbsp;${authors}<br/>
%endif
{{#ccli_license}}
{{ccli_license_label}}:&nbsp;{{ccli_license}}<br/>
{{/ccli_license}}""",
%if authors_music:
<%
authors = ", ".join(authors_music)
%>
${authors_music_label}:&nbsp;${authors}<br/>
%endif
%if authors_translation:
<%
authors = ", ".join(authors_translation)
%>
${authors_translation_label}:&nbsp;${authors}<br/>
%endif
%if copyright:
&copy;&nbsp;${copyright}<br/>
%endif
%if songbook_entries:
<%
entries = ", ".join(songbook_entries)
%>
${entries}<br/>
%endif
%if ccli_license:
${ccli_license_label}&nbsp;${ccli_license}<br/>
%endif
""",
}

View File

@ -93,8 +93,7 @@ MODULES = [
'bs4',
'mako',
'uno',
'six',
'pystache'
'six'
]