Skip to content

Commit

Permalink
Merge branch 'master' into fix-2496-second-try
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Apr 2, 2017
2 parents 53e5f6e + 3835c95 commit 70f74f5
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 17 deletions.
12 changes: 12 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,15 @@
New in master
=============

Features
--------

Bugfixes
--------

* Make ``PAGE_INDEX`` work with ``PRETTY_URLS`` (Issue #2705)
* Fix PHP posts/pages not rendering on Windows (Issue #2706)

New in v7.8.4
=============

Expand Down
2 changes: 1 addition & 1 deletion nikola/filters.py
Expand Up @@ -328,7 +328,7 @@ def php_template_injection(data):
phpdata = in_file.read()
_META_SEPARATOR = '(' + os.linesep * 2 + '|' + ('\n' * 2) + '|' + ("\r\n" * 2) + ')'
phpdata = re.split(_META_SEPARATOR, phpdata, maxsplit=1)[-1]
phpdata = re.sub(template.group(0), phpdata, data)
phpdata = data.replace(template.group(0), phpdata)
return phpdata
else:
return data
Expand Down
4 changes: 4 additions & 0 deletions nikola/plugins/task/page_index.py
Expand Up @@ -64,6 +64,10 @@ def is_enabled(self, lang=None):
def classify(self, post, lang):
"""Classify the given post for the given language."""
destpath = post.destination_path(lang, sep='/')
if post.has_pretty_url(lang):
idx = '/index.html'
if destpath.endswith(idx):
destpath = destpath[:-len(idx)]
i = destpath.rfind('/')
return [destpath[:i] if i >= 0 else '']

Expand Down
11 changes: 8 additions & 3 deletions nikola/post.py
Expand Up @@ -279,7 +279,8 @@ def __repr__(self):
m.update(utils.unicode_str(json.dumps(clean_meta, cls=utils.CustomEncoder, sort_keys=True)).encode('utf-8'))
return '<Post: {0!r} {1}>'.format(self.source_path, m.hexdigest())

def _has_pretty_url(self, lang):
def has_pretty_url(self, lang):
"""Check if this page has a pretty URL."""
m = self.meta[lang].get('pretty_url', '')
if m:
# match is a non-empty string, overides anything
Expand All @@ -288,6 +289,10 @@ def _has_pretty_url(self, lang):
# use PRETTY_URLS, unless the slug is 'index'
return self.pretty_urls and self.meta[lang]['slug'] != 'index'

def _has_pretty_url(self, lang):
"""Check if this page has a pretty URL."""
return self.has_pretty_url(lang)

@property
def is_mathjax(self):
"""True if this post has the mathjax tag in the current language or is a python notebook."""
Expand Down Expand Up @@ -796,7 +801,7 @@ def destination_path(self, lang=None, extension='.html', sep=os.sep):
if lang is None:
lang = nikola.utils.LocaleBorg().current_lang
folder = self.folders[lang]
if self._has_pretty_url(lang):
if self.has_pretty_url(lang):
path = os.path.join(self.translations[lang],
folder, self.meta[lang]['slug'], 'index' + extension)
else:
Expand Down Expand Up @@ -874,7 +879,7 @@ def permalink(self, lang=None, absolute=False, extension='.html', query=None):

pieces = self.translations[lang].split(os.sep)
pieces += self.folders[lang].split(os.sep)
if self._has_pretty_url(lang):
if self.has_pretty_url(lang):
pieces += [self.meta[lang]['slug'], 'index' + extension]
else:
pieces += [self.meta[lang]['slug'] + extension]
Expand Down
2 changes: 1 addition & 1 deletion requirements-extras.txt
Expand Up @@ -11,5 +11,5 @@ webassets>=0.10.1
notebook>=4.0.0
ipykernel>=4.0.0
ghp-import2>=1.0.0
ws4py==0.4.0
ws4py==0.4.2
watchdog==0.8.3
2 changes: 1 addition & 1 deletion snapcraft/edge/requirements.txt
Expand Up @@ -7,7 +7,7 @@ typogrify>=2.0.4
phpserialize>=1.3
webassets>=0.10.1
ghp-import2>=1.0.0
ws4py==0.4.0
ws4py==0.4.2
watchdog==0.8.3
doit>=0.30.0
Pygments>=1.6
Expand Down
2 changes: 1 addition & 1 deletion snapcraft/requirements.txt
Expand Up @@ -7,7 +7,7 @@ typogrify>=2.0.4
phpserialize>=1.3
webassets>=0.10.1
ghp-import2>=1.0.0
ws4py==0.4.0
ws4py==0.4.2
watchdog==0.8.3
doit>=0.30.0
Pygments>=1.6
Expand Down
2 changes: 1 addition & 1 deletion snapcraft/stable/requirements.txt
Expand Up @@ -7,7 +7,7 @@ typogrify>=2.0.4
phpserialize>=1.3
webassets>=0.10.1
ghp-import2>=1.0.0
ws4py==0.4.0
ws4py==0.4.2
watchdog==0.8.3
doit>=0.30.0
Pygments>=1.6
Expand Down
63 changes: 54 additions & 9 deletions tests/test_integration.py
Expand Up @@ -544,15 +544,16 @@ def fill_site(self):
with io.open(target_path, "w+", encoding="utf8") as outf:
outf.write("foo")


class PageIndexTest(EmptyBuildTest):
"""Test if PAGE_INDEX works."""
"""Test if PAGE_INDEX works, with PRETTY_URLS disabled."""

@classmethod
def patch_site(self):
"""Enable PAGE_INDEX."""
conf_path = os.path.join(self.target_dir, "conf.py")
with io.open(conf_path, "a", encoding="utf8") as outf:
outf.write("""\n\nPAGE_INDEX = True\n\n""")
outf.write("""\n\nPAGE_INDEX = True\nPRETTY_URLS = False\nPAGES = PAGES + (('pages/*.php', 'pages', 'story.tmpl'),)\n\n""")

@classmethod
def fill_site(self):
Expand All @@ -563,8 +564,11 @@ def fill_site(self):
pages = os.path.join(self.target_dir, "pages")
subdir1 = os.path.join(self.target_dir, "pages", "subdir1")
subdir2 = os.path.join(self.target_dir, "pages", "subdir2")
subdir3 = os.path.join(self.target_dir, "pages", "subdir3")

nikola.utils.makedirs(subdir1)
nikola.utils.makedirs(subdir2)
nikola.utils.makedirs(subdir3)

with io.open(os.path.join(pages, 'page0.txt'), "w+", encoding="utf8") as outf:
outf.write(".. title: Page 0\n.. slug: page0\n\nThis is page 0.\n")
Expand All @@ -575,24 +579,38 @@ def fill_site(self):
outf.write(".. title: Page 2\n.. slug: page2\n\nThis is page 2.\n")

with io.open(os.path.join(subdir2, 'page3.txt'), "w+", encoding="utf8") as outf:
outf.write(".. title: Page3\n.. slug: page3\n\nThis is page 3.\n")
outf.write(".. title: Page 3\n.. slug: page3\n\nThis is page 3.\n")
with io.open(os.path.join(subdir2, 'foo.txt'), "w+", encoding="utf8") as outf:
outf.write(".. title: Not the page index\n.. slug: index\n\nThis is not the page index.\n")

with io.open(os.path.join(subdir3, 'page4.txt'), "w+", encoding="utf8") as outf:
outf.write(".. title: Page 4\n.. slug: page4\n\nThis is page 4.\n")
with io.open(os.path.join(subdir3, 'bar.php'), "w+", encoding="utf8") as outf:
outf.write(".. title: Still not the page index\n.. slug: index\n\nThis is not the page index either.\n")

def _make_output_path(self, dir, name):
"""Make a file path to the output."""
return os.path.join(dir, name + '.html')

def test_page_index(self):
"""Test PAGE_INDEX."""
pages = os.path.join(self.target_dir, "output", "pages")
subdir1 = os.path.join(self.target_dir, "output", "pages", "subdir1")
subdir2 = os.path.join(self.target_dir, "output", "pages", "subdir2")
subdir3 = os.path.join(self.target_dir, "output", "pages", "subdir3")

# Do all files exist?
self.assertTrue(os.path.isfile(os.path.join(pages, 'page0.html')))
self.assertTrue(os.path.isfile(self._make_output_path(pages, 'page0')))
self.assertTrue(os.path.isfile(self._make_output_path(subdir1, 'page1')))
self.assertTrue(os.path.isfile(self._make_output_path(subdir1, 'page2')))
self.assertTrue(os.path.isfile(self._make_output_path(subdir2, 'page3')))
self.assertTrue(os.path.isfile(self._make_output_path(subdir3, 'page4')))

self.assertTrue(os.path.isfile(os.path.join(pages, 'index.html')))
self.assertTrue(os.path.isfile(os.path.join(subdir1, 'page1.html')))
self.assertTrue(os.path.isfile(os.path.join(subdir1, 'page2.html')))
self.assertTrue(os.path.isfile(os.path.join(subdir1, 'index.html')))
self.assertTrue(os.path.isfile(os.path.join(subdir2, 'page3.html')))
self.assertTrue(os.path.isfile(os.path.join(subdir2, 'index.html')))
self.assertTrue(os.path.isfile(os.path.join(subdir3, 'index.php')))
self.assertFalse(os.path.isfile(os.path.join(subdir3, 'index.html')))

# Do the indexes only contain the pages the should?
with io.open(os.path.join(pages, 'index.html'), 'r', encoding='utf-8') as fh:
Expand All @@ -601,24 +619,51 @@ def test_page_index(self):
self.assertTrue('Page 1' not in pages_index)
self.assertTrue('Page 2' not in pages_index)
self.assertTrue('Page 3' not in pages_index)
self.assertTrue('This is not the page index.' not in pages_index)
self.assertTrue('Page 4' not in pages_index)
self.assertTrue('This is not the page index' not in pages_index)

with io.open(os.path.join(subdir1, 'index.html'), 'r', encoding='utf-8') as fh:
subdir1_index = fh.read()
self.assertTrue('Page 0' not in subdir1_index)
self.assertTrue('Page 1' in subdir1_index)
self.assertTrue('Page 2' in subdir1_index)
self.assertTrue('Page 3' not in subdir1_index)
self.assertTrue('This is not the page index.' not in subdir1_index)
self.assertTrue('Page 4' not in subdir1_index)
self.assertTrue('This is not the page index' not in subdir1_index)

with io.open(os.path.join(subdir2, 'index.html'), 'r', encoding='utf-8') as fh:
subdir2_index = fh.read()
self.assertTrue('Page 0' not in subdir2_index)
self.assertTrue('Page 1' not in subdir2_index)
self.assertTrue('Page 2' not in subdir2_index)
self.assertTrue('Page 3' not in subdir2_index)
self.assertTrue('Page 4' not in subdir2_index)
self.assertTrue('This is not the page index.' in subdir2_index)

with io.open(os.path.join(subdir3, 'index.php'), 'r', encoding='utf-8') as fh:
subdir3_index = fh.read()
self.assertTrue('Page 0' not in subdir3_index)
self.assertTrue('Page 1' not in subdir3_index)
self.assertTrue('Page 2' not in subdir3_index)
self.assertTrue('Page 3' not in subdir3_index)
self.assertTrue('Page 4' not in subdir3_index)
self.assertTrue('This is not the page index either.' in subdir3_index)


class PageIndexPrettyUrlsTest(PageIndexTest):
"""Test if PAGE_INDEX works, with PRETTY_URLS enabled."""

@classmethod
def patch_site(self):
"""Enable PAGE_INDEX."""
conf_path = os.path.join(self.target_dir, "conf.py")
with io.open(conf_path, "a", encoding="utf8") as outf:
outf.write("""\n\nPAGE_INDEX = True\nPRETTY_URLS = True\nPAGES = PAGES + (('pages/*.php', 'pages', 'story.tmpl'),)\n\n""")

def _make_output_path(self, dir, name):
"""Make a file path to the output."""
return os.path.join(dir, name + '/index.html')


if __name__ == "__main__":
unittest.main()

0 comments on commit 70f74f5

Please sign in to comment.