public function register(Application $app) { $app['publisher'] = $app->share(function ($app) { $outputFormat = $app->edition('format'); switch (strtolower($outputFormat)) { case 'pdf': $publisher = new PdfPublisher($app); break; case 'html': $publisher = new HtmlPublisher($app); break; case 'html_chunked': $publisher = new HtmlChunkedPublisher($app); break; case 'epub': $publisher = new Epub2Publisher($app); break; case 'mobi': $publisher = new MobiPublisher($app); break; default: throw new \RuntimeException(sprintf('Unknown "%s" format for "%s" edition (allowed: "pdf", "html", "html_chunked", "epub", "mobi")', $outputFormat, $app['publishing.edition'])); } if (true != $publisher->checkIfThisPublisherIsSupported()) { throw new \RuntimeException(sprintf("Your system doesn't support publishing books with the '%s' format\n" . "Check the easybook documentation to know the dependencies required by this format.", $outputFormat)); } return $publisher; }); }
public function register(Application $app) { $app['slugger.options'] = array('separator' => '-', 'prefix' => ''); // stores all the generated slugs to ensure slug uniqueness $app['slugger.generated_slugs'] = array(); $app['slugger'] = $app->share(function () use($app) { return new Slugger($app['slugger.options']); }); }
private function getApplication($configFileName) { $app = new Application(); $configurator = $this->getMock('Easybook\\Configurator\\BookConfigurator', array('loadBookFileConfiguration'), array($app)); $configurator->expects($this->any())->method('loadBookFileConfiguration')->will($this->returnValue(Yaml::parse($this->fixturesDir . '/' . $configFileName) ?: array())); $app['configurator'] = $configurator; $app->loadEasybookConfiguration(); return $app; }
public function register(Application $app) { $app['parser.options'] = array('markdown_syntax' => 'easybook', 'code_block_type' => 'markdown'); $app['parser'] = $app->share(function ($app) { $format = strtolower($app['publishing.active_item']['config']['format']); if (in_array($format, array('md', 'mdown', 'markdown'))) { return new MarkdownParser($app); } throw new \RuntimeException(sprintf('Unknown "%s" format for "%s" content (easybook only supports Markdown)', $format, $app['publishing.active_item']['config']['content'])); }); }
public function testBookWithNoConfigFile() { $app = new Application(); $app['publishing.dir.book'] = uniqid('this-path-does-not-exist'); $app['publishing.book.slug'] = 'book_with_no_config_file'; try { $app->loadBookConfiguration(); } catch (\RuntimeException $e) { $this->assertInstanceOf('\\RuntimeException', $e); $this->assertContains("There is no 'config.yml' configuration file", $e->getMessage()); } }
public function __construct() { $this->fileCount = 0; $this->filesystem = new Filesystem(); $this->rootDir = realpath(__DIR__ . '/../../../'); // needed to get easybook version $app = new Application(); $this->version = $app->getVersion(); // temp directory to copy the essential easybook files $this->packageDir = $this->rootDir . '/app/Cache/easybook'; // delete the directory if it existed previously if (file_exists($this->packageDir)) { $this->filesystem->remove($this->packageDir); } $this->filesystem->mkdir($this->packageDir); }
public function register(Application $app) { $app['prince.path'] = null; // the common installation dirs for PrinceXML in several OS $app['prince.default_paths'] = array('/usr/local/bin/prince', '/usr/bin/prince', 'C:\\Program Files\\Prince\\engine\\bin\\prince.exe'); $app['prince'] = $app->share(function () use($app) { $princePath = $app['prince.path'] ?: $app->findPrinceXmlExecutable(); // ask the user about the location of the executable if (null == $princePath) { $princePath = $app->askForPrinceXMLExecutablePath(); if (!file_exists($princePath)) { throw new \RuntimeException(sprintf("We couldn't find the PrinceXML executable in the given directory (%s)", $princePath)); } } $prince = new Prince($princePath); $prince->setHtml(true); return $prince; }); }
public function register(Application $app) { $app['twig.options'] = array('autoescape' => false, 'charset' => $app['app.charset'], 'debug' => $app['app.debug'], 'strict_variables' => $app['app.debug']); $app['twig.loader'] = $app->share(function () use($app) { $theme = ucfirst($app->edition('theme')); $format = Toolkit::camelize($app->edition('format'), true); $loader = new \Twig_Loader_Filesystem($app['app.dir.themes']); // Base theme (common styles per edition type) // <easybook>/app/Resources/Themes/Base/<edition-type>/Templates/<template-name>.twig $baseThemeDir = sprintf('%s/Base/%s/Templates', $app['app.dir.themes'], $format); $loader->addPath($baseThemeDir); $loader->addPath($baseThemeDir, 'theme'); $loader->addPath($baseThemeDir, 'theme_base'); // Book theme (configured per edition in 'config.yml') // <easybook>/app/Resources/Themes/<theme>/<edition-type>/Templates/<template-name>.twig $bookThemeDir = sprintf('%s/%s/%s/Templates', $app['app.dir.themes'], $theme, $format); $loader->prependPath($bookThemeDir); $loader->prependPath($bookThemeDir, 'theme'); $userTemplatePaths = array($app['publishing.dir.templates'], sprintf('%s/%s', $app['publishing.dir.templates'], strtolower($format)), sprintf('%s/%s', $app['publishing.dir.templates'], $app['publishing.edition'])); foreach ($userTemplatePaths as $path) { if (file_exists($path)) { $loader->prependPath($path); } } $defaultContentPaths = array(sprintf('%s/Base/%s/Contents', $app['app.dir.themes'], $format), sprintf('%s/%s/%s/Contents', $app['app.dir.themes'], $theme, $format)); foreach ($defaultContentPaths as $path) { if (file_exists($path)) { $loader->prependPath($path, 'content'); } } return $loader; }); $app['twig'] = $app->share(function () use($app) { $twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']); $twig->addExtension(new TwigCssExtension()); $twig->addGlobal('app', $app); if (null != ($bookConfig = $app['publishing.book.config'])) { $twig->addGlobal('book', $bookConfig['book']); $publishingEdition = $app['publishing.edition']; $editions = $app->book('editions'); $twig->addGlobal('edition', $editions[$publishingEdition]); } return $twig; }); }
/** * It highlights the given code using the given programming language syntax * and decorates the result with the Twig template associated with the * code fragments. * * @param string $code The source code to highlight and decorate * @param string $language The programming language associated with the code * @param Application $app The application object needed to highlight and decorate * * @return string The resulting code after the highlight and rendering process */ public function highlightAndDecorateCode($code, $language, Application $app) { if ($app->edition('highlight_code')) { // highlight code if the edition wants to $code = $app->highlight($code, $language); } else { // escape code to show it instead of interpreting it // yaml-style comments could be interpreted as Markdown headings // replace any starting # character by its HTML entity (#) $code = '<pre>' . preg_replace('/^# (.*)/', "# \$1", htmlspecialchars($code)) . '</pre>'; } $code = $app->render('code.twig', array('item' => array('content' => $code, 'language' => $language, 'number' => '', 'slug' => ''))); return $code; }
public function testTwoSidedPrintedBookIncludeBlankPages() { $app = new Application(); $app['publishing.book.config'] = $this->getBookConfig(true); $app['publishing.edition'] = 'print'; $bookCss = $app->render('@theme/style.css.twig'); $this->assertContains(".item {\n page-break-before: right;", $bookCss, "Two-sided books include blank pages when needed."); }
/** * @dataProvider getValuesForGetAndSetMethods */ public function testSetMethod($key, $expectedValue) { $app = new Application(); $app->set($key, $expectedValue); $this->assertEquals($expectedValue, $app[$key]); }