Example #1
0
 public function testEditionMethodShortcut()
 {
     $app = new Application();
     // needed to simulate the third-party library required to publish PDF books
     $app['prince.path'] = __FILE__;
     $app['publishing.edition'] = 'my_edition';
     $app['publishing.book.config'] = array('book' => array('editions' => array('my_edition' => array('format' => 'pdf'))));
     $this->assertEquals('pdf', $app->edition('format'));
     $app->edition('format', 'epub');
     $this->assertEquals('epub', $app->edition('format'));
 }
 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['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;
     });
 }
Example #4
0
 /**
  * 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 (&#35;)
         $code = '<pre>' . preg_replace('/^# (.*)/', "&#35; \$1", htmlspecialchars($code)) . '</pre>';
     }
     $code = $app->render('code.twig', array('item' => array('content' => $code, 'language' => $language, 'number' => '', 'slug' => '')));
     return $code;
 }