import tags from xml (part 1)

This commit is contained in:
Andreas Preikschat 2011-08-27 16:00:24 +02:00
parent 65630c6853
commit f3691516fd
3 changed files with 78 additions and 32 deletions

View File

@ -27,6 +27,9 @@
""" """
Provide HTML Tag management and Formatting Tag access class Provide HTML Tag management and Formatting Tag access class
""" """
import cPickle
from PyQt4 import QtCore
from openlp.core.lib import translate from openlp.core.lib import translate
@ -136,13 +139,30 @@ class FormattingTags(object):
FormattingTags.add_html_tags(temporary_tags) FormattingTags.add_html_tags(temporary_tags)
@staticmethod @staticmethod
def add_html_tags(tags): def save_html_tags():
"""
Saves all formatting tags except protected ones.
"""
tags = []
for tag in FormattingTags.get_html_tags():
if not tag[u'protected'] and not tag[u'temporary']:
tags.append(tag)
# Formatting Tags were also known as display tags.
QtCore.QSettings().setValue(u'displayTags/html_tags',
QtCore.QVariant(cPickle.dumps(tags) if tags else u''))
@staticmethod
def add_html_tags(tags, save=False):
""" """
Add a list of tags to the list. Add a list of tags to the list.
``tags`` ``tags``
The list with tags to add. The list with tags to add.
``save``
Defaults to ``False``. If set to ``True`` the given ``tags`` are
saved to the config.
Each **tag** has to be a ``dict`` and should have the following keys: Each **tag** has to be a ``dict`` and should have the following keys:
* desc * desc
@ -170,6 +190,8 @@ class FormattingTags(object):
displaying text containing the tag. It has to be a ``boolean``. displaying text containing the tag. It has to be a ``boolean``.
""" """
FormattingTags.html_expands.extend(tags) FormattingTags.html_expands.extend(tags)
if save:
FormattingTags.save_html_tags()
@staticmethod @staticmethod
def remove_html_tag(tag_id): def remove_html_tag(tag_id):

View File

@ -150,7 +150,7 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
FormattingTags.remove_html_tag(self.selected) FormattingTags.remove_html_tag(self.selected)
self.selected = -1 self.selected = -1
self._resetTable() self._resetTable()
self._saveTable() FormattingTags.save_html_tags()
def onSavedPushed(self): def onSavedPushed(self):
""" """
@ -177,19 +177,7 @@ class FormattingTagForm(QtGui.QDialog, Ui_FormattingTagDialog):
html[u'temporary'] = False html[u'temporary'] = False
self.selected = -1 self.selected = -1
self._resetTable() self._resetTable()
self._saveTable() FormattingTags.save_html_tags()
def _saveTable(self):
"""
Saves all formatting tags except protected ones.
"""
tags = []
for tag in FormattingTags.get_html_tags():
if not tag[u'protected'] and not tag[u'temporary']:
tags.append(tag)
# Formatting Tags were also known as display tags.
QtCore.QSettings().setValue(u'displayTags/html_tags',
QtCore.QVariant(cPickle.dumps(tags) if tags else u''))
def _resetTable(self): def _resetTable(self):
""" """

View File

@ -67,6 +67,7 @@ import re
from lxml import etree, objectify from lxml import etree, objectify
from openlp.core.lib import FormattingTags
from openlp.plugins.songs.lib import clean_song, VerseType from openlp.plugins.songs.lib import clean_song, VerseType
from openlp.plugins.songs.lib.db import Author, Book, Song, Topic from openlp.plugins.songs.lib.db import Author, Book, Song, Topic
from openlp.core.utils import get_application_version from openlp.core.utils import get_application_version
@ -265,7 +266,9 @@ class OpenLyrics(object):
application_name = u'OpenLP ' + get_application_version()[u'version'] application_name = u'OpenLP ' + get_application_version()[u'version']
song_xml.set(u'createdIn', application_name) song_xml.set(u'createdIn', application_name)
song_xml.set(u'modifiedIn', application_name) song_xml.set(u'modifiedIn', application_name)
song_xml.set(u'modifiedDate', datetime.datetime.now().isoformat()) # "Convert" 2011-08-27 11:49:15 to 2011-08-27T11:49:15.
song_xml.set(u'modifiedDate',
unicode(song.last_modified).replace(u' ', u'T'))
properties = etree.SubElement(song_xml, u'properties') properties = etree.SubElement(song_xml, u'properties')
titles = etree.SubElement(properties, u'titles') titles = etree.SubElement(properties, u'titles')
self._add_text_to_element(u'title', titles, song.title) self._add_text_to_element(u'title', titles, song.title)
@ -299,6 +302,10 @@ class OpenLyrics(object):
themes = etree.SubElement(properties, u'themes') themes = etree.SubElement(properties, u'themes')
for topic in song.topics: for topic in song.topics:
self._add_text_to_element(u'theme', themes, topic.name) self._add_text_to_element(u'theme', themes, topic.name)
# Process the formatting tags.
format = etree.SubElement(song_xml, u'format')
tags = etree.SubElement(format, u'tags')
tags.set(u'application', u'OpenLP')
# Process the song's lyrics. # Process the song's lyrics.
lyrics = etree.SubElement(song_xml, u'lyrics') lyrics = etree.SubElement(song_xml, u'lyrics')
verse_list = sxml.get_verses(song.lyrics) verse_list = sxml.get_verses(song.lyrics)
@ -345,8 +352,11 @@ class OpenLyrics(object):
properties = song_xml.properties properties = song_xml.properties
else: else:
return None return None
if float(song_xml.get(u'version')) > 0.6:
self._process_formatting_tags(song_xml, only_process_format_tags)
if only_process_format_tags:
return
song = Song() song = Song()
if not only_process_format_tags:
# Values will be set when cleaning the song. # Values will be set when cleaning the song.
song.search_lyrics = u'' song.search_lyrics = u''
song.verse_order = u'' song.verse_order = u''
@ -356,7 +366,6 @@ class OpenLyrics(object):
self._process_titles(properties, song) self._process_titles(properties, song)
# The verse order is processed with the lyrics! # The verse order is processed with the lyrics!
self._process_lyrics(properties, song_xml, song) self._process_lyrics(properties, song_xml, song)
if not only_process_format_tags:
self._process_comments(properties, song) self._process_comments(properties, song)
self._process_authors(properties, song) self._process_authors(properties, song)
self._process_songbooks(properties, song) self._process_songbooks(properties, song)
@ -463,6 +472,33 @@ class OpenLyrics(object):
if hasattr(properties, u'copyright'): if hasattr(properties, u'copyright'):
song.copyright = self._text(properties.copyright) song.copyright = self._text(properties.copyright)
def _process_formatting_tags(self, song_xml, temporary):
"""
Process the formatting tags from the song and either add missing tags
temporary or permanently to the formatting tag list.
"""
if not hasattr(song_xml, u'format'):
return
found_tags = []
for tag in song_xml.format.tags.getchildren():
name = tag.get(u'name')
if name is None:
continue
openlp_tag = {
u'desc': name,
u'start tag': u'{%s}' % name[:5],
u'end tag': u'{/%s}' % name[:5],
u'start html': tag.open.text,
u'end html': tag.close.text,
u'protected': False,
u'temporary': temporary
}
found_tags.append(openlp_tag)
existing_tag_ids = [tag[u'start tag']
for tag in FormattingTags.get_html_tags()]
FormattingTags.add_html_tags([tag for tag in found_tags
if tag[u'start tag'] not in existing_tag_ids], True)
def _process_lyrics(self, properties, song_xml, song_obj): def _process_lyrics(self, properties, song_xml, song_obj):
""" """
Processes the verses and search_lyrics for the song. Processes the verses and search_lyrics for the song.