/
book_figure.py
171 lines (143 loc) · 5.24 KB
/
book_figure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding: utf-8 -*-
# Copyright © 2014 Ivan Teoh and others.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from docutils import nodes
from docutils.parsers.rst import Directive, directives
from nikola.plugin_categories import RestExtension
class Plugin(RestExtension):
name = "book_figure"
def set_site(self, site):
self.site = site
directives.register_directive('book_figure', BookFigure)
return super(Plugin, self).set_site(site)
CODE_IMAGE = (u"""<div class="book-figure-media">
<a class="book-figure-image" href="{url}" target="_blank">
<img src="{image_url}" alt="{title}" />
</a>
</div>""")
CODE_URL = (u"""<a class="book-figure-title" href="{url}" target="_blank">{title}</a>""")
CODE_TITLE = (u"""<p class="book-figure-title">{title}</p>""")
CODE_AUTHOR = (u"""<p class="book-figure-author">
by {author}
</p>""")
CODE_AUTHOR_WITH_URL = (u"""<p class="book-figure-author">
by <a href="{author_url}" target="_blank">{author}</a>
</p>""")
CODE_ISBN_13 = (u"""<tr>
<th>ISBN-13:</th>
<td>{isbn_13}</td>
</tr>""")
CODE_ISBN_10 = (u"""<tr>
<th>ISBN-10:</th>
<td>{isbn_10}</td>
</tr>""")
CODE_ASIN = (u"""<tr>
<th>ASIN:</th>
<td>{asin}</td>
</tr>""")
CODE_BOOK_NUMBER = (u"""<table class="book-figure-book-number"><tbody>
{isbn_13}
{isbn_10}
{asin}
</tbody></table>""")
CODE_REVIEW = (u"""<div class="book-figure-review">
{review}
</div>""")
CODE_BOOK = (u"""<div class="book-figure-content">
{url}
{author}
{book_number}
{review}
</div>""")
CODE = (u"""<div class="{classes}">
{image_url}
{title}
</div>""")
class BookFigure(Directive):
""" Restructured text extension for inserting book figure
Usage:
.. book_figure:: title
:class: class name
:url: book url
:author: book author
:author_url: book author url
:isbn_13: ISBN13
:isbn_10: ISBN10
:asin: ASIN
:image_url: book cover image url
Your review.
"""
has_content = True
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
option_spec = {
'class': directives.unchanged,
'url': directives.unchanged,
'author': directives.unchanged,
'author_url': directives.unchanged,
'isbn_13': directives.unchanged,
'isbn_10': directives.unchanged,
'asin': directives.unchanged,
'image_url': directives.unchanged,
}
def run(self):
""" Required by the Directive interface. Create docutils nodes """
options = {
'title': ' '.join(self.arguments),
'classes': self.options.get('class', ''),
'url': self.options.get('url', ''),
'author': self.options.get('author', ''),
'author_url': self.options.get('author_url', ''),
'isbn_13': self.options.get('isbn_13', ''),
'isbn_10': self.options.get('isbn_10', ''),
'asin': self.options.get('asin', ''),
'image_url': self.options.get('image_url', ''),
}
if options['image_url']:
options['image_url'] = CODE_IMAGE.format(**options)
if options['author']:
if options['author_url']:
options['author'] = CODE_AUTHOR_WITH_URL.format(**options)
else:
options['author'] = CODE_AUTHOR.format(**options)
if options['isbn_13']:
options['isbn_13'] = CODE_ISBN_13.format(**options)
if options['isbn_10']:
options['isbn_10'] = CODE_ISBN_10.format(**options)
if options['asin']:
options['asin'] = CODE_ASIN.format(**options)
options['book_number'] = ''
if options['isbn_13'] or options['isbn_10'] or options['asin']:
options['book_number'] = CODE_BOOK_NUMBER.format(**options)
options['review'] = ''
for line in self.content:
options['review'] += u'<p>{0}</p>'.format(line)
if options['review']:
options['review'] = CODE_REVIEW.format(**options)
if options['url']:
options['url'] = CODE_URL.format(**options)
else:
options['url'] = CODE_TITLE.format(**options)
options['title'] = CODE_BOOK.format(**options)
return [nodes.raw('', CODE.format(**options), format='html')]