From 91dd534f02121c74f22fd7ee8d4d1007333fafcd Mon Sep 17 00:00:00 2001 From: Aneesh Dogra Date: Tue, 25 Dec 2012 13:52:58 +0000 Subject: Add functionality to export wiki pages to Journal. --- diff --git a/JournalExport.py b/JournalExport.py new file mode 100644 index 0000000..3c71d00 --- /dev/null +++ b/JournalExport.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012 Aneesh Dogra +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +# Journal Export +from sugar3.datastore import datastore +from sugar3.activity import activity +import dataretriever +import platform +import StringIO +import os +from server import WPWikiDB +from server import HTMLOutputBuffer +from mwlib import parser, scanner, expander, rendermath, htmlwriter + +system_id = "%s%s" % (platform.system().lower(), + platform.architecture()[0][0:2]) + +instance_dir = os.path.join(activity.get_activity_root(), 'instance') + +class JournalExport: + def __init__(self, confvars): + self.wikidb = WPWikiDB(confvars['path'], confvars['lang'], + confvars['templateprefix'], confvars['templateblacklist']) + self.dataretriever = dataretriever.DataRetriever(system_id, confvars['path']) + self.confvars = confvars + + def export_all(self, progressbar): + article_list = self.search('') # get all articles + for index in range(0, len(article_list)): + + article_text = self.wikidb.getRawArticle(article_list[index]).encode('utf8') + article_text = self.wikidb.expandArticle(article_text, article_list[index]) + tokens = scanner.tokenize(article_text, article_list[index]) + wiki_parsed = parser.Parser(tokens, article_list[index]).parse() + wiki_parsed.caption = article_list[index] + out = StringIO.StringIO() + w = htmlwriter.HTMLWriter(out) + w.write(wiki_parsed) + htmloutput = out.getvalue() + filename = os.path.join(instance_dir, article_list[index] + '.html') + print filename + fp = open(filename, 'w') + fp.write(htmloutput) + fp.close() + + journal_entry = datastore.create() + journal_entry.metadata['title'] = article_list[index] + journal_entry.metadata['title_set_by_user'] = '1' + journal_entry.metadata['mime_type'] = 'text/html' + journal_entry.file_path = filename + datastore.write(journal_entry) + + progressbar.set_fraction((index + 1) / len(article_list)) + os.remove(filename) + + def search(self, article_title): + return self.wikidb.dataretriever.search(article_title) + + def getRawArticle(self, title): + + # Retrieve article text, recursively following #redirects. + if title == '': + return '' + + article_text = \ + self.dataretriever.get_text_article(title).decode('utf-8') + + # Stripping leading & trailing whitespace fixes template expansion. + article_text = article_text.lstrip() + article_text = article_text.rstrip() diff --git a/activity.py b/activity.py index 43b430a..d4fc79e 100644 --- a/activity.py +++ b/activity.py @@ -16,6 +16,8 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA from gettext import gettext as _ +from JournalExport import JournalExport +from gi.repository import Gtk # for progressbar import os import sys @@ -43,7 +45,7 @@ else: import webactivity from searchtoolbar import SearchToolbar - +from sugar3.graphics.toolbutton import ToolButton # Activity class, extends WebActivity. class WikipediaActivity(webactivity.WebActivity): @@ -83,12 +85,31 @@ class WikipediaActivity(webactivity.WebActivity): search_toolbar_button.props.label = _('Search') self.get_toolbar_box().toolbar.insert(search_toolbar_button, 1) search_toolbar_button.show() + + # add journal export button + export_to_journal_button = ToolButton('search-wiki') + export_to_journal_button.set_tooltip(_('Export wiki to Journal')) + export_to_journal_button.connect('clicked', self._export_to_journal_cb) + self.get_toolbar_box().toolbar.insert(export_to_journal_button, 2) + export_to_journal_button.show() + # Hide add-tabs button if hasattr(self._primary_toolbar, '_add_tab'): self._primary_toolbar._add_tab.hide() + #XXX: Find a way to display this on the screen. + self.progressbar = Gtk.ProgressBar() # used with Journal Exporter + self.progressbar.set_fraction(0.0) + self.searchtoolbar.show() + def _export_to_journal_cb(self, widget): + je = JournalExport(self.confvars) + # As this takes a hell lot of time, its preferable to display a + # progressbar, let's create that + self.progressbar.show() + je.export_all(self.progressbar) + def _get_browser(self): if hasattr(self, '_browser'): # Browse < 109 diff --git a/setup.py b/setup.py index 77c1b6e..93d4971 100755 --- a/setup.py +++ b/setup.py @@ -141,4 +141,4 @@ if sys.argv[1] == 'fix_manifest': prepare_ok = False if prepare_ok: - bundlebuilder.start('Wikipedia') + bundlebuilder.start() -- cgit v0.9.1