Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative representation #2897

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,41 @@ protected function processFrontmatter()
}
}

/**
* Return a different representation of the same page.
* Place a new file named _<representation>.md next to the current
* markdown file.
*
* @param string $representation the name of the alternative representation.
*
* @return Page A new instance of Page or null if the representation does not
* exists or is not published.
*/
public function alternativeRepresentation($representation)
{
$returnValue;

$path = $this->path . DS . $this->folder . DS . '_' . $representation . '.md';
if (file_exists($path)) {
$aPage = new Page();
$aPage->init(new \SplFileInfo($path), '.md');

if (!$aPage->published()) {
$returnValue = null;
}
else {
// Removes the '_'
$aPage->template(($this->modular() ? 'modular/' : '') . $representation);
$returnValue = $aPage;
}
}
else {
$returnValue = null;
}

return $returnValue;
}

/**
* Return an array with the routes of other translated languages
*
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Page/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ protected function recurse($directory, PageInterface $parent = null)

// Build regular expression for all the allowed page extensions.
$page_extensions = $language->getFallbackPageExtensions();
$regex = '/^[^\.]*(' . implode('|', array_map(
$regex = '/^[^\._]*(' . implode('|', array_map(
function ($str) {
return preg_quote($str, '/');
},
Expand Down