Ticket #3608: 0001-Export-content-as-HTML.patch

File 0001-Export-content-as-HTML.patch, 4.7 KB (added by humitos, 12 years ago)
  • new file article.html

    From 67983d5998e9d2c953e6590ef01bed241eac8e23 Mon Sep 17 00:00:00 2001
    Message-Id: <67983d5998e9d2c953e6590ef01bed241eac8e23.1337787495.git.humitos@gmail.com>
    From: Manuel Kaufmann <humitos@gmail.com>
    Date: Wed, 23 May 2012 12:31:25 -0300
    Subject: [PATCH InfoSlicer] Export content as HTML
    
    Added HTML files to the bundle so this content can be opened with Browse
    Activity. The old dita/ditamap files are not removed because they are parsed to
    generate the new HTML files.
    
    Besides, the HTML generation uses these dita files to not be too aggressive
    with the code in this cycle. We will improve this code in the future and we
    will remove completely the dita generation.
    
    Signed-off-by: Manuel Kaufmann <humitos@gmail.com>
    ---
     article.html |    9 +++++++++
     parse.py     |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     xol.py       |    7 ++++++-
     3 files changed, 76 insertions(+), 1 deletion(-)
     create mode 100644 article.html
     create mode 100644 parse.py
    
    diff --git a/article.html b/article.html
    new file mode 100644
    index 0000000..f57266a
    - +  
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     2<html xmlns="http://www.w3.org/1999/xhtml">
     3  <head>
     4    <title>%(title)s</title>
     5    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     6    <link rel="stylesheet" href="ditastyle.css" type="text/css" />
     7  </head>
     8  <body>%(body)s</body>
     9</html>
  • new file parse.py

    diff --git a/parse.py b/parse.py
    new file mode 100644
    index 0000000..96383ac
    - +  
     1#!/usr/bin/python
     2# -*- coding: utf-8 -*-
     3from gettext import gettext as _
     4from BeautifulSoup import BeautifulSoup
     5
     6
     7def parse_dita(dita_str):
     8    soup = BeautifulSoup(dita_str)
     9
     10    html = open('article.html', 'r').read().decode('utf-8')
     11
     12    html_tags = []
     13
     14    title = soup.find('title').string.strip()
     15    h1_title = '<h1>%(title)s</h1>' % \
     16        {'title': title}
     17    index_link = '<p><a href="librarymap.html">' + \
     18        _('Return to index') + '</a></p>'
     19
     20    html_tags.append(index_link)
     21    html_tags.append(h1_title)
     22
     23    for section in soup.findAll('section'):
     24        for p in section.findAll('p'):
     25            images = p.findAll('image')
     26            for img in images:
     27                html_tags.append('<img src="%(src)s" />' % \
     28                                      {'src': img.get('href')})
     29            html_tags.append('<p>')
     30            for ph in p.findAll('ph'):
     31                html_tags.append(ph.string.strip())
     32            html_tags.append('</p>')
     33
     34    html = html % {'title': title,
     35                   'body': '\n'.join(html_tags)}
     36    return html
     37
     38
     39def parse_ditamap(ditamap_str):
     40    soup = BeautifulSoup(ditamap_str)
     41    html = open('article.html', 'r').read().decode('utf-8')
     42
     43    html_tags = []
     44
     45    title = soup.find('map').get('title')
     46
     47    h1_title = '<h1>%(title)s</h1>' % \
     48        {'title': title}
     49    html_tags.append(h1_title)
     50
     51    html_tags.append('<li>')
     52    for topic in soup.findAll('topicref'):
     53        dita_path = topic.get('href')
     54        html_tags.append('<ul><a href="%(href)s">%(name)s</a></ul>' % \
     55                             {'href': dita_path.replace('.dita', '.html'),
     56                              'name': topic.get('navtitle')})
     57    html_tags.append('</li>')
     58
     59    html = html % {'title': title,
     60                   'body': '\n'.join(html_tags)}
     61    return html
  • xol.py

    diff --git a/xol.py b/xol.py
    index b5aa1df..6b2a34f 100644
    a b import gtk 
    1919import zipfile
    2020import uuid
    2121import logging
     22import parse
    2223from glob import glob
    2324from gettext import gettext as _
    2425
    def _dita_management(zip, uid, title): 
    152153                'href="ditastylesheet.xsl"?>')
    153154        zipstr(zip, os.path.join(uid, 'slicecontent', '%s.dita' % auid),
    154155                content.prettify())
     156        zipstr(zip, os.path.join(uid, 'slicecontent', '%s.html' % auid),
     157                parse.parse_dita(content.prettify()))
    155158
    156159        map.append('<topicref href="%s.dita" navtitle="%s">' % (auid, atitle))
    157160        map.append('</topicref>')
    def _dita_management(zip, uid, title): 
    159162    map.append('</map>')
    160163    zipstr(zip, os.path.join(uid, 'slicecontent', 'librarymap.ditamap'),
    161164            "\n".join(map))
     165    zipstr(zip, os.path.join(uid, 'slicecontent', 'librarymap.html'),
     166            parse.parse_ditamap("\n".join(map)))
    162167
    163168def _index_redirect(zip, uid):
    164169    """
    165170        Creates the redirecting index.html
    166171    """
    167     redirect_loc = 'slicecontent/librarymap.ditamap'
     172    redirect_loc = 'slicecontent/librarymap.html'
    168173
    169174    html = ['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">',\
    170175            '<html>',\