Skip to content

Commit

Permalink
Refactored for Kirby 3
Browse files Browse the repository at this point in the history
  • Loading branch information
splorp committed May 9, 2022
1 parent 520ba15 commit d27a8ce
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 84 deletions.
63 changes: 63 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Kirby Paperback Export
*
* @version 2.0.0
* @author Grant Hutchinson <grant@splorp.com>
* @copyright Grant Hutchinson <grant@splorp.com>
* @link https://github.com/splorp/kirby-paperback-export
* @license MIT
*/

Kirby::plugin('splorp/paperback-export', [
'snippets' => [
'paperback-export/content' => __DIR__ . '/snippets/content.php',
],
'routes' => [
[
'pattern' => 'export/paperback',
'action' => function () {

$includeUnlisted = option('splorp.paperback-export.includeUnlisted', true);
$includeChildren = option('splorp.paperback-export.includeChildren', []);
$excludeTemplate = option('splorp.paperback-export.excludeTemplate', []);

if (! is_array($includeChildren)) {
throw new Exception('The option "splorp.paperback-export.includeChildren" must be an array.');
}
if (! is_array($excludeTemplate)) {
throw new Exception('The option "splorp.paperback-export.excludeTemplate" must be an array.');
}

$languages = site()->languages();
$pages = site()->index();
$title = site()->title();
$description = site()->description();
$version = site()->version();
$filename = str::slug($title);

/* Check whether to include unlisted pages */

if (! $includeUnlisted) {
$pages = $pages->listed();
}

/* Include only the children of the specified page */

if ($includeChildren) {
$pages = $pages->find($includeChildren)->children();
}

/* Exclude pages using certain templates */

$pages = $pages->filterBy('intendedTemplate', 'not in', $excludeTemplate);

$template = __DIR__ . '/snippets/export.php';
$paperback = tpl::load($template, compact('languages', 'pages', 'title', 'description', 'version', 'filename'));

return new response($paperback, 'txt');
}
]
]
]);
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{
"name": "paperback-export",
"author": "Grant Hutchinson <grant@splorp.com>",
"version": "1.0.1",
"description": "Kirby Paperback Export",
"type": "kirby-plugin",
"license": "MIT"
"name": "splorp/paperback-export",
"description": "Export Kirby 3 CMS content for use with the PaperBack Book Maker",
"version": "2.0.0",
"type": "kirby-plugin",
"license": "MIT",
"authors": [
{
"name": "Grant Hutchinson",
"email": "grant@splorp.com",
"homepage": "https://splorp.com/"
}
],
"require": {
"getkirby/composer-installer": "^1.1"
},
"extra": {
"installer-name": "paperback-export"
}
}
58 changes: 0 additions & 58 deletions paperback-export.php

This file was deleted.

36 changes: 20 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,15 @@ Paperback is a simple cross-platform utility created by [David Fedor](https://we

## Installation

After installing the plugin using one of the methods listed below, visiting `yoursite.com/export/paperback` should automatically download a text file without any additional configuration.
After installing the plugin using one of the methods below, visiting `yoursite.com/export/paperback` should automatically download a text file without any additional configuration.

### Download

To install the plugin manually, [download the current release](https://github.com/splorp/kirby-paperback-export/releases), decompress the archive, and put the files in:

`site/plugins/paperback-export`

### Kirby CLI

Installing the plugin using the Kirby [command line interface](https://github.com/getkirby/cli):

$ kirby plugin:install splorp/kirby-paperback-export

Updating the plugin using the Kirby CLI:

$ kirby plugin:update splorp/kirby-paperback-export
For [Kirby 2](https://github.com/getkirby-v2), download version [1.0.1](https://github.com/splorp/kirby-paperback-export/releases/tag/1.0.1) of the plugin.

### Git Submodule

Expand All @@ -53,17 +45,23 @@ Updating the plugin as a Git submodule:

## Options

By default, Kirby Paperback Export will include the text from the title and description fields for every page on your Kirby site, including invisible pages. The following options allow you to select and filter which pages are included.
By default, Kirby Paperback Export will include the text from the title and description fields for every page on your Kirby site, including unlisted pages. The following options allow you to filter which pages are included in the exported data.

```php
// Include invisible pages
c::set('paperback.include.invisible', true);
// Include unlisted pages
return [
'splorp.paperback-export.includeUnlisted' => true,
];

// Include only the children of a specific page
c::set('paperback.include.children', []);
return [
'splorp.paperback-export.includeChildren' => [],
];

// Exclude specific templates
c::set('paperback.exclude.template', []);
return [
'splorp.paperback-export.excludeTemplate' => [],
];
```

## Known Issues
Expand All @@ -74,6 +72,12 @@ c::set('paperback.exclude.template', []);

## Release Notes

### 2.0.0
+ Refactored and updated for Kirby 3
+ Tweaked the option names to be more consistent and self explanatory
+ Moved the file export code into `snippets/export.php`
+ Renamed `snippets/page.php` to `snippets/content.php`

### 1.0.1
+ Refactored filtering options
+ Fixed formatting of paragraph breaks in `$page->text()`
Expand All @@ -87,7 +91,7 @@ A tip of the hat to [Pedro Borges](https://pedroborg.es/) and his [Kirby XML Sit

## License

Copyright © 2017 Grant Hutchinson
Copyright © 2017–2021 Grant Hutchinson

This project is licensed under the short and sweet [MIT License](https://opensource.org/licenses/MIT). This license allows you to do anything pretty much anything you want with the contents of the repository, as long as you provide proper attribution and don’t hold anyone liable.

Expand Down
6 changes: 3 additions & 3 deletions snippets/page.php → snippets/content.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php echo '@@TOC ' . $page->title() . PHP_EOL . PHP_EOL ?>
<?php $buffer = str_replace(array("</p>\r<p>", "</p>\r <p>", "</p> \r<p>", "</p>\n<p>", "</p>\n <p>", "</p> \n<p>", "</p>\r\n<p>", "</p>\r\n <p>", "</p> \r\n<p>"), '</p>' . PHP_EOL . PHP_EOL . '<p>', $page->text()->kirbytext()); ?>
<?php echo html_entity_decode(strip_tags($buffer)) . PHP_EOL . PHP_EOL ?>
<?php if($page->content()->has('Source')): ?>
<?php if($page->source()->pages()->count() > 1) { echo 'Sources: '; } else { echo 'Source: '; } ?>
<?php $n=0; foreach($page->source()->pages() as $source): $n++; ?>
<?php if($page->source()->exists()): ?>
<?php if($page->source()->toPages()->count() > 1) { echo 'Sources: '; } else { echo 'Source: '; } ?>
<?php $n=0; foreach($page->source()->toPages() as $source): $n++; ?>
<?php echo $source->title() . PHP_EOL ?>
<?php endforeach ?>
<?php echo PHP_EOL ?>
Expand Down
2 changes: 1 addition & 1 deletion paperback-export.txt.php → snippets/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<?php if ($description != '') { echo ($description . PHP_EOL . PHP_EOL); } ?>
<?php if ($version != '') { echo ('Version ' . $version . PHP_EOL . PHP_EOL); } ?>
<?php foreach ($pages as $page) : ?>
<?php snippet('paperback.page', compact('languages', 'page')) ?>
<?php snippet('paperback-export/content', compact('languages', 'page')) ?>
<?php endforeach ?>

0 comments on commit d27a8ce

Please sign in to comment.