def __init__(self, app): self._appconf = app.conf self._conf = Conf(self._default_conf, app.conf.get('ext.feed', {})) self._url = normalize_url(app.conf['site.url']) + self._conf['save_as'] app.add_generator(self) app.add_theme_ctx(feedurl=self._url)
def generate(self, documents): posts_number = self._conf['posts_number'] save_as = self._conf['save_as'] # we are interested only in 'post' documents because 'pages' # usually are not a part of feed since they are not intended # to deliver content with regular updates posts = (doc for doc in documents if isinstance(doc, Post)) # since we could have a lot of posts, it's a common practice to # use in feed only last N posts posts = sorted(posts, key=lambda d: d.published, reverse=True) posts = posts[:posts_number] credentials = { 'siteurl_self': self._url, 'siteurl_alt': normalize_url(self._appconf['site.url']), 'site': self._appconf['site'], 'date': datetime.datetime.utcnow().replace(microsecond=0), } save_as = os.path.join(self._appconf['paths.output'], save_as) mkdir(os.path.dirname(save_as)) encoding = self._appconf['encoding.output'] with open(save_as, 'w', encoding=encoding) as f: f.write(self._template.render( documents=posts, credentials=credentials, encoding=encoding))
def generate(self, documents): posts_number = self._conf['posts_number'] save_as = self._conf['save_as'] # we are interested only in 'post' documents because 'pages' # usually are not a part of feed since they are not intended # to deliver content with regular updates posts = (doc for doc in documents if isinstance(doc, Post)) # since we could have a lot of posts, it's a common practice to # use in feed only last N posts posts = sorted(posts, key=lambda d: d.published, reverse=True) posts = posts[:posts_number] credentials = { 'siteurl_self': self._url, 'siteurl_alt': normalize_url(self._appconf['site.url']), 'site': self._appconf['site'], 'date': datetime.datetime.utcnow().replace(microsecond=0), } save_as = os.path.join(self._appconf['paths.output'], save_as) mkdir(os.path.dirname(save_as)) encoding = self._appconf['encoding.output'] with open(save_as, 'w', encoding=encoding) as f: f.write( self._template.render(documents=posts, credentials=credentials, encoding=encoding))
def test_default(self): """ Tests that func works correctly with default parameters. """ corner_cases = ( ('test.com', 'http://test.com/'), ('http://test.com', 'http://test.com/'), ('https://test.com', 'https://test.com/'), ) for url, expected in corner_cases: self.assertEqual(normalize_url(url), expected)
def test_trailing_slash(self): """ Tests that trailing slash works correctly: ensures that present or not. """ self.assertEqual(normalize_url("http://test.com", trailing_slash=True), "http://test.com/") self.assertEqual(normalize_url("http://test.com/", trailing_slash=True), "http://test.com/") self.assertEqual(normalize_url("http://test.com", trailing_slash=False), "http://test.com") self.assertEqual(normalize_url("http://test.com/", trailing_slash=False), "http://test.com") self.assertEqual(normalize_url("http://test.com", trailing_slash="keep"), "http://test.com") self.assertEqual(normalize_url("http://test.com/", trailing_slash="keep"), "http://test.com/")
def test_trailing_slash(self): """ Tests that trailing slash works correctly: ensures that present or not. """ self.assertEqual( normalize_url('http://test.com', trailing_slash=True), 'http://test.com/') self.assertEqual( normalize_url('http://test.com/', trailing_slash=True), 'http://test.com/') self.assertEqual( normalize_url('http://test.com', trailing_slash=False), 'http://test.com') self.assertEqual( normalize_url('http://test.com/', trailing_slash=False), 'http://test.com') self.assertEqual( normalize_url('http://test.com', trailing_slash='keep'), 'http://test.com') self.assertEqual( normalize_url('http://test.com/', trailing_slash='keep'), 'http://test.com/')