xref: /plugin/dw2pdf/src/Styles.php (revision f2000117f688f925c0f5fdc1ec614c297f3d0398)
1<?php
2
3namespace dokuwiki\plugin\dw2pdf\src;
4
5use dokuwiki\StyleUtils;
6
7class Styles
8{
9    protected Config $config;
10
11    /**
12     * @param Config $config
13     */
14    public function __construct(Config $config)
15    {
16        $this->config = $config;
17    }
18
19    /**
20     * Get the full CSS to include in the PDF
21     *
22     * Gathers all relevant CSS files, applies style replacements and parses LESS.
23     *
24     * @return string
25     */
26    public function getCSS(): string
27    {
28        //reuse the CSS dispatcher functions without triggering the main function
29        if (!defined('SIMPLE_TEST')) {
30            define('SIMPLE_TEST', 1);
31        }
32        require_once(DOKU_INC . 'lib/exe/css.php');
33
34        // prepare CSS files
35        $files = $this->getStyleFiles();
36        $css = '';
37        foreach ($files as $file => $location) {
38            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
39            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
40            $css .= css_loadfile($file, $location);
41        }
42
43        // apply style replacements
44        $styleUtils = new StyleUtils();
45        $styleini = $styleUtils->cssStyleini();
46        $css = css_applystyle($css, $styleini['replacements']);
47
48        // parse less
49        return css_parseless($css);
50    }
51
52
53    /**
54     * Returns the list of style files to include in the PDF
55     *
56     * The array keys are the file paths on disk, the values are the
57     * paths as used inside the Styles (for resolving relative links).
58     *
59     * @return array<string,string>
60     */
61    protected function getStyleFiles(): array
62    {
63        $tpl = $this->config->getTemplateName();
64
65        return array_merge(
66            [
67                DOKU_INC . 'lib/styles/screen.css' => DOKU_BASE . 'lib/styles/',
68                DOKU_INC . 'lib/styles/print.css' => DOKU_BASE . 'lib/styles/',
69            ],
70            $this->getExtensionStyles(),
71            [
72                DOKU_PLUGIN . 'dw2pdf/conf/style.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
73                DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/style.css' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $tpl . '/',
74                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
75            ]
76        );
77    }
78
79    /**
80     * Returns a list of possible Plugin and Template PDF Styles
81     *
82     * Checks for a pdf.css, falls back to print.css. For configured usestyles plugins
83     * the screen.css and style.css are also included.
84     */
85    protected function getExtensionStyles()
86    {
87        $list = [];
88        $plugins = plugin_list();
89
90        $usestyle = $this->config->getStyledExtensions();
91        foreach ($plugins as $p) {
92            if (in_array($p, $usestyle)) {
93                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
94                $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/";
95
96                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
97                $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/";
98            }
99
100            $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/";
101            $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/";
102
103            if (file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) {
104                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
105                $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/";
106            } else {
107                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
108                $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/";
109            }
110        }
111
112        // template support
113        foreach (
114            [
115                'pdf.css',
116                'pdf.less',
117                'css/pdf.css',
118                'css/pdf.less',
119                'styles/pdf.css',
120                'styles/pdf.less'
121            ] as $file
122        ) {
123            if (file_exists(tpl_incdir() . $file)) {
124                $list[tpl_incdir() . $file] = tpl_basedir() . $file;
125            }
126        }
127
128        return $list;
129    }
130}
131