diff options
author | Timo Richter <timo@iera.de> | 2011-08-07 22:23:21 +0200 |
---|---|---|
committer | Jan Holesovsky <kendy@suse.cz> | 2011-08-26 13:46:41 +0200 |
commit | 4d674531da66d668ff5b6292da849d07d315c60d (patch) | |
tree | d114d757fdb28e75059241b78e02c4e4ec6a645a /helpcontent2/wiki-to-help/metabook_translated.py | |
parent | 4125fb5b27501a1481bf6746d4141d45896a3364 (diff) |
Implemented the language separator
Cleaned up metabook.py
convert now uses parameters.
Try convert.py -h
call_convert.sh calls convert.py as is was without parameters
Diffstat (limited to 'helpcontent2/wiki-to-help/metabook_translated.py')
-rw-r--r-- | helpcontent2/wiki-to-help/metabook_translated.py | 64 |
1 files changed, 61 insertions, 3 deletions
diff --git a/helpcontent2/wiki-to-help/metabook_translated.py b/helpcontent2/wiki-to-help/metabook_translated.py index 07364dd0d4..e9321c2fa5 100644 --- a/helpcontent2/wiki-to-help/metabook_translated.py +++ b/helpcontent2/wiki-to-help/metabook_translated.py @@ -1,5 +1,5 @@ import metabook -import re +import re, os class ArticleTranslated(metabook.Article): lang = "en" # default language code @@ -47,9 +47,67 @@ class MetabookTranslated(metabook.Metabook): ArticleClass=ArticleTranslated artTags = ["title","comment"] - def splitByLanguage(self): +class LanguageSeparator(object): + """ + A translated metabook is a metabook where all titles are in the destination + language. + This class splits a translated metabook into many books with homogenous languages. + """ + books={} # Dict<Str lang, Metabook> + sortedItems={} # Dict<Str lang, List<TranslatedArticle>> + items=[] # List<TranslatedArticle> + + def __init__(self, book): + self.book = book + self.items = book.items + + def splitItemsByLanguage(self): """ @return List of Metabook """ - pass + sortedItems={} + for item in self.items: + if item.lang in sortedItems.keys(): + sortedItems[item.lang].append(item) + else: + sortedItems[item.lang] = [item] + self.sortedItems = sortedItems + #return sortedItems + + def createBooksByLanguage(self): + for lang, items in self.sortedItems.iteritems(): + m = self.book.getClone() + m.items = items + m.lang = lang + self.books[lang] = m + + @staticmethod + def fromFileToFiles(jsonStructFile,xmldump,output): + """ + Creates a Metabook from a file and writes it to one file per language. + Short cut Function. This loads a metabook template file, creates the + metabook content from @xmldump and writes the book to @output. + @jsonStructFile String path to Metabook template + @xmldump String path + @output String path to output directory + @return Dict<String lang, String output> + """ + m = MetabookTranslated() + with open(jsonStructFile,"r") as f: + m.loadTemplate(f) + + m.loadArticles(xmldump) + ls = LanguageSeparator(m) + ls.splitItemsByLanguage() + ls.createBooksByLanguage() + + pathlist={} + + for lang, book in ls.books.iteritems(): + book.createBook() + dest = os.path.join(output,"metabook_%s.json" % lang) + pathlist[lang] = dest + with open(dest,"w") as f: + book.write(f) + return pathlist |