1<?php
2
3// must be run within Dokuwiki
4if(!defined('DOKU_INC')) die();
5
6require_once DOKU_PLUGIN . 'latexport/implementation/decorator.php';
7
8/**
9 * Maps the dokuwiki heading structure into a latex document structure
10 * The root document:
11 *
12 *     H1: The first H1 opens the main matter. The text of header is ignored.
13 *         The second H1 opens the appendix. The text of header is ignored.
14 *         The third and next H1 are considered chapters in the appendix. Text of header is title of the chapter.
15 *     H2: Opens a part.
16 *         The text of header is placed as title of the part.
17 *         Also, H2 following the third or next H1 are considered chapters in the appendix.
18 *     H3: Opens a chapter.
19 *         The text of header is placed as title of the chapter.
20 *     H4: Opens a section.
21 *         The text of header is placed as title of the section.
22 *     H5: Opens a subsection.
23 *         The text of header is placed as title of the part.
24 *
25 *     Unordered list item starting with a link includes the destination page,
26 *     using the current level of heading as the base level.
27 *
28 * In the destination page:
29 *
30 *     - The H1 opens a chapter, section, subsection, etc depending on the level of
31 *       heading in the referring page. Text of header is used as title of the heading.
32 *     - The H1 never opens a level higher than chapter.
33 *     - Lower header levels open a lower level headings.
34 *     - Unordered list item starting with a link includes the destination page,
35 *       using the current level of heading as the base level.
36 *
37 * Latexport Plugin: Exports to latex
38 *
39 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
40 * @author Jean-Michel Gonet <jmgonet@yahoo.com>
41 */
42class DecoratorHeadings extends Decorator {
43	const FRONT_MATTER     = 900;
44	const MAIN_MATTER      = 901;
45	const APPENDIX         = 902;
46
47	/**
48	 * Internal state of this decorator.
49	 */
50	private $state;
51
52	/**
53	 * Class constructor.
54	 * @param decorator The next decorator.
55	 */
56	function __construct($decorator) {
57		parent::__construct($decorator);
58		$this->state = DecoratorHeadings::FRONT_MATTER;
59	}
60
61	/**
62	 * Headers are transformed in part, chapter, section, subsection and subsubsection.
63	 */
64	function header($text, $level, $pos) {
65
66		switch($level) {
67			case 1:
68				$this->h1($text, $pos);
69				break;
70
71			case 2:
72				$this->h2($text, $pos);
73				break;
74
75			case 3:
76				$this->h3($text, $pos);
77				break;
78
79			case 4:
80				$this->h4($text, $pos);
81				break;
82
83			default:
84				$this->h5($text, $pos);
85				break;
86		}
87	}
88
89	/**
90	 * Handles H1 level headers.
91	 * <ul>
92	 *   <li>The first H1 opens the main matter. The text of header is ignored.</li>
93	 *   <li>The second H1 opens the appendix. The text of header is ignored.</li>
94	 *   <li>The third and next H1 are considered chapters in the appendix. The text is the chapter title.</li>
95	 * </ul>
96	 */
97	private function h1($text, $pos) {
98		switch($this->state) {
99			case DecoratorHeadings::FRONT_MATTER:
100				$this->state = DecoratorHeadings::MAIN_MATTER;
101				$this->decorator->header($text, 1, $pos);
102				break;
103
104			case DecoratorHeadings::MAIN_MATTER:
105				$this->state = DecoratorHeadings::APPENDIX;
106				$this->decorator->header($text, 1, $pos);
107				break;
108
109			case DecoratorHeadings::APPENDIX:
110				$this->decorator->header($text, 3, $pos);
111				break;
112
113			default:
114				trigger_error("h1 unexpected $this->state");
115		}
116	}
117
118	/**
119	 * Handles H2 level headers.
120	 * <ul>
121	 *    <li>H2 before the main matters are considered chapters.</li>
122	 *    <li>H2 in the main matter are considered parts.</li>
123	 *    <li>H2 in the appendix are considered chapters.</li>
124	 * </ul>
125	 */
126	private function h2($text, $pos) {
127		switch($this->state) {
128			case DecoratorHeadings::FRONT_MATTER:
129				$this->decorator->header($text, 3, $pos);
130				break;
131
132			case DecoratorHeadings::MAIN_MATTER:
133				$this->decorator->header($text, 2, $pos);
134				break;
135
136			case DecoratorHeadings::APPENDIX:
137				$this->decorator->header($text, 3, $pos);
138				break;
139
140			default:
141				trigger_error("h2 unexpected $this->state");
142		}
143	}
144
145	private function h3($text, $pos) {
146		$this->decorator->header($text, 3, $pos);
147	}
148
149	private function h4($text, $pos) {
150		$this->decorator->header($text, 4, $pos);
151	}
152
153	private function h5($text, $pos) {
154		$this->decorator->header($text, 5, $pos);
155	}
156
157}
158