1# vim: fileencoding=utf-8
2
3# Copyright (c) 2006-2021  Andrey Golovizin
4#
5# Permission is hereby granted, free of charge, to any person obtaining
6# a copy of this software and associated documentation files (the
7# "Software"), to deal in the Software without restriction, including
8# without limitation the rights to use, copy, modify, merge, publish,
9# distribute, sublicense, and/or sell copies of the Software, and to
10# permit persons to whom the Software is furnished to do so, subject to
11# the following conditions:
12#
13# The above copyright notice and this permission notice shall be
14# included in all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24
25"""
26HTML output backend.
27
28>>> from pybtex.richtext import Tag, HRef
29>>> html = Backend()
30>>> print(Tag('em', '').render(html))
31<BLANKLINE>
32>>> print(Tag('em', 'Hard &', ' heavy').render(html))
33<em>Hard &amp; heavy</em>
34>>> print(HRef('/', '').render(html))
35<BLANKLINE>
36>>> print(HRef('/', 'Hard & heavy').render(html))
37<a href="/">Hard &amp; heavy</a>
38"""
39from __future__ import unicode_literals
40
41from xml.sax.saxutils import escape
42
43import pybtex.io
44from pybtex.backends import BaseBackend
45
46
47PROLOGUE = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
48<html>
49<head><meta name="generator" content="Pybtex">
50<meta http-equiv="Content-Type" content="text/html; charset=%s">
51<title>Bibliography</title>
52</head>
53<body>
54<dl>
55"""
56
57class Backend(BaseBackend):
58    u"""
59    >>> from pybtex.richtext import Text, Tag, Symbol
60    >>> print(Tag('em', Text(u'Л.:', Symbol('nbsp'), u'<<Химия>>')).render(Backend()))
61    <em>Л.:&nbsp;&lt;&lt;Химия&gt;&gt;</em>
62
63    """
64
65    default_suffix = '.html'
66    symbols = {
67        'ndash': u'&ndash;',
68        'newblock': u'\n',
69        'nbsp': u'&nbsp;'
70    }
71
72    def format_str(self, text):
73        return escape(text)
74
75    def format_protected(self, text):
76        return r'<span class="bibtex-protected">{}</span>'.format(text)
77
78    def format_tag(self, tag, text):
79        return r'<{0}>{1}</{0}>'.format(tag, text) if text else u''
80
81    def format_href(self, url, text):
82        return r'<a href="{0}">{1}</a>'.format(url, text) if text else u''
83
84    def write_prologue(self):
85        encoding = self.encoding or pybtex.io.get_default_encoding()
86        self.output(PROLOGUE % encoding)
87
88    def write_epilogue(self):
89        self.output(u'</dl></body></html>\n')
90
91    def write_entry(self, key, label, text):
92        self.output(u'<dt>%s</dt>\n' % label)
93        self.output(u'<dd>%s</dd>\n' % text)
94