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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
# -*- coding: utf-8 -*-
# Copyright (c) 2012 Roberto Alsina y otros.
# 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.
import codecs
import os
import lxml.html
from . import utils
__all__ = ['Post']
class Post(object):
"""Represents a blog post or web page."""
def __init__(self, source_path, cache_folder, destination, use_in_feeds,
translations, default_lang, blog_url, messages):
"""Initialize post.
The base path is the .txt post file. From it we calculate
the meta file, as well as any translations available, and
the .html fragment file path.
`compile_html` is a function that knows how to compile this Post to
html.
"""
self.prev_post = None
self.next_post = None
self.blog_url = blog_url
self.is_draft = False
self.source_path = source_path # posts/blah.txt
self.post_name = os.path.splitext(source_path)[0] # posts/blah
# cache/posts/blah.html
self.base_path = os.path.join(cache_folder, self.post_name + ".html")
self.metadata_path = self.post_name + ".meta" # posts/blah.meta
self.folder = destination
self.translations = translations
self.default_lang = default_lang
self.messages = messages
if os.path.isfile(self.metadata_path):
with codecs.open(self.metadata_path, "r", "utf8") as meta_file:
meta_data = meta_file.readlines()
while len(meta_data) < 6:
meta_data.append("")
(default_title, default_pagename, self.date, self.tags,
self.link, default_description) = \
[x.strip() for x in meta_data][:6]
else:
(default_title, default_pagename, self.date, self.tags,
self.link, default_description) = \
utils.get_meta(self.source_path)
if not default_title or not default_pagename or not self.date:
raise OSError("You must set a title and slug and date!")
self.date = utils.to_datetime(self.date)
self.tags = [x.strip() for x in self.tags.split(',')]
self.tags = [_f for _f in self.tags if _f]
# While draft comes from the tags, it's not really a tag
self.use_in_feeds = use_in_feeds and "draft" not in self.tags
self.is_draft = 'draft' in self.tags
self.tags = [t for t in self.tags if t != 'draft']
self.pagenames = {}
self.titles = {}
self.descriptions = {}
# Load internationalized titles
# TODO: this has gotten much too complicated. Rethink.
for lang in translations:
if lang == default_lang:
self.titles[lang] = default_title
self.pagenames[lang] = default_pagename
self.descriptions[lang] = default_description
else:
metadata_path = self.metadata_path + "." + lang
source_path = self.source_path + "." + lang
try:
if os.path.isfile(metadata_path):
with codecs.open(
metadata_path, "r", "utf8") as meta_file:
meta_data = [x.strip() for x in
meta_file.readlines()]
while len(meta_data) < 6:
meta_data.append("")
self.titles[lang] = meta_data[0] or default_title
self.pagenames[lang] = meta_data[1] or\
default_pagename
self.descriptions[lang] = meta_data[5] or\
default_description
else:
ttitle, ppagename, tmp1, tmp2, tmp3, ddescription = \
utils.get_meta(source_path)
self.titles[lang] = ttitle or default_title
self.pagenames[lang] = ppagename or default_pagename
self.descriptions[lang] = ddescription or\
default_description
except:
self.titles[lang] = default_title
self.pagenames[lang] = default_pagename
self.descriptions[lang] = default_description
def title(self, lang):
"""Return localized title."""
return self.titles[lang]
def description(self, lang):
"""Return localized description."""
return self.descriptions[lang]
def deps(self, lang):
"""Return a list of dependencies to build this post's page."""
deps = [self.base_path]
if lang != self.default_lang:
deps += [self.base_path + "." + lang]
deps += self.fragment_deps(lang)
return deps
def fragment_deps(self, lang):
"""Return a list of dependencies to build this post's fragment."""
deps = [self.source_path]
if os.path.isfile(self.metadata_path):
deps.append(self.metadata_path)
if lang != self.default_lang:
lang_deps = list(filter(os.path.exists, [x + "." + lang for x in deps]))
deps += lang_deps
return deps
def text(self, lang, teaser_only=False):
"""Read the post file for that language and return its contents"""
file_name = self.base_path
if lang != self.default_lang:
file_name_lang = file_name + ".%s" % lang
if os.path.exists(file_name_lang):
file_name = file_name_lang
with codecs.open(file_name, "r", "utf8") as post_file:
data = post_file.read()
if data:
data = lxml.html.make_links_absolute(data, self.permalink())
if data and teaser_only:
e = lxml.html.fromstring(data)
teaser = []
flag = False
for elem in e:
elem_string = lxml.html.tostring(elem)
if '<!-- TEASER_END -->' in elem_string.upper():
flag = True
break
teaser.append(elem_string)
if flag:
teaser.append('<p><a href="%s">%s...</a></p>' %
(self.permalink(lang), self.messages[lang]["Read more"]))
data = ''.join(teaser)
return data
def destination_path(self, lang, extension='.html'):
path = os.path.join(self.translations[lang],
self.folder, self.pagenames[lang] + extension)
return path
def permalink(self, lang=None, absolute=False, extension='.html'):
if lang is None:
lang = self.default_lang
pieces = list(os.path.split(self.translations[lang]))
pieces += list(os.path.split(self.folder))
pieces += [self.pagenames[lang] + extension]
pieces = [_f for _f in pieces if _f]
if absolute:
pieces = [self.blog_url] + pieces
else:
pieces = [""] + pieces
link = "/".join(pieces)
return link
def source_ext(self):
return os.path.splitext(self.source_path)[1]
|