PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Document/src/converters/element_visitor/docbook/odt/styler/pcss/preprocessor/list.php

https://github.com/Yannix/zetacomponents
PHP | 198 lines | 97 code | 8 blank | 93 comment | 9 complexity | ea6d2ccb3af3b15faa986f6341e5cd00 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezcDocumentOdtPcssListStylePreprocessor class.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @access private
  23. * @package Document
  24. * @version //autogen//
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * List style pre-processor.
  29. *
  30. * Pre-processes list styles, since DocBook stores list bullet and numbering
  31. * format in an attribute. An instance of this class creates custom PCSS
  32. * properties for this information as follows:
  33. *
  34. * - list-type = "bullet" / "number"
  35. * - list-bullet = bullet character to use
  36. * - list-number = number representative format
  37. *
  38. * @access private
  39. * @package Document
  40. * @version //autogen//
  41. */
  42. class ezcDocumentOdtPcssListStylePreprocessor
  43. {
  44. /**
  45. * List bullet character guesser.
  46. *
  47. * @var ezcDocumentListBulletGuesser
  48. */
  49. protected $bulletGuesser;
  50. /**
  51. * Mapping of CSS list-style-type values to representative numbers.
  52. *
  53. * @var array(string=>string)
  54. */
  55. protected $cssNumberMap = array(
  56. 'decimal' => '1',
  57. 'lower-roman' => 'i',
  58. 'upper-roman' => 'I',
  59. 'lower-latin' => 'a',
  60. 'upper-latin' => 'A',
  61. // not supported
  62. 'decimal-leading-zero' => '1',
  63. 'lower-greek' => 'a',
  64. 'armenian' => 'A',
  65. 'georgian' => 'A',
  66. );
  67. /**
  68. * Mapping of DocBook numeration values to representative numbers.
  69. *
  70. * @var array(string=>string)
  71. */
  72. protected $docBookNumberMap = array(
  73. 'arabic' => '1',
  74. 'loweralpha' => 'a',
  75. 'lowerroman' => 'i',
  76. 'upperalpha' => 'A',
  77. 'upperroman' => 'I'
  78. );
  79. /**
  80. * Creates a new list style processor.
  81. */
  82. public function __construct()
  83. {
  84. $this->bulletGuesser = new ezcDocumentListBulletGuesser();
  85. }
  86. /**
  87. * Pre-process styles and return them.
  88. *
  89. * Performs some detection of list styles in the $docBookElement and its
  90. * document and sets according PCSS properties in $styles.
  91. *
  92. * @param ezcDocumentOdtStyleInformation $styleInfo
  93. * @param DOMElement $docBookElement
  94. * @param DOMElement $odtElement
  95. * @param array $styles
  96. * @return array
  97. */
  98. public function process( ezcDocumentOdtStyleInformation $styleInfo, DOMElement $docBookElement, DOMElement $odtElement, array $styles )
  99. {
  100. switch ( $docBookElement->localName )
  101. {
  102. case 'itemizedlist':
  103. $styles['list-type'] = new ezcDocumentPcssStyleStringValue( 'bullet' );
  104. $styles = $this->processListBullet( $docBookElement, $styles );
  105. break;
  106. case 'orderedlist':
  107. $styles['list-type'] = new ezcDocumentPcssStyleStringValue( 'number' );
  108. $styles = $this->processListEnumeration( $docBookElement, $styles );
  109. break;
  110. }
  111. return $styles;
  112. }
  113. /**
  114. * Detects the list bullet to be used and applies a special PCSS setting
  115. * for it.
  116. *
  117. * This method tries to detect the list bullet to be used for bullet-lists
  118. * and sets the special "list-bullet" PCSS property. The new $styles array
  119. * is returned. Note: "list-bullet" is not a standard CSS property and
  120. * therefore not supported by any other application using CSS. It is also
  121. * possible that this property name changes in future.
  122. *
  123. * @param DOMElement $docBookElement
  124. * @param array $styles
  125. * @return array
  126. */
  127. protected function processListBullet( DOMElement $docBookElement, array $styles )
  128. {
  129. if ( !isset( $styles['list-bullet'] ) )
  130. {
  131. if ( $docBookElement->hasAttribute( 'mark' ) )
  132. {
  133. $styles['list-bullet'] = new ezcDocumentPcssStyleStringValue(
  134. $this->bulletGuesser->markToChar(
  135. $docBookElement->getAttribute( 'mark' )
  136. )
  137. );
  138. }
  139. else if ( isset( $styles['list-style-type'] ) )
  140. {
  141. $styles['list-bullet'] = new ezcDocumentPcssStyleStringValue(
  142. $this->bulletGuesser->markToChar(
  143. $styles['list-style-type']->value
  144. )
  145. );
  146. }
  147. else
  148. {
  149. $styles['list-bullet'] = new ezcDocumentPcssStyleStringValue(
  150. '⚫'
  151. );
  152. }
  153. }
  154. return $styles;
  155. }
  156. /**
  157. * Detects the list numbering to use and applies a special PCSS setting for
  158. * it.
  159. *
  160. * @param DOMElement $docBookElement
  161. * @param array $styles
  162. * @return void
  163. */
  164. protected function processListEnumeration( DOMElement $docBookElement, array $styles )
  165. {
  166. if ( !isset( $styles['list-number'] ) )
  167. {
  168. if ( $docBookElement->hasAttribute( 'numeration' ) )
  169. {
  170. $styles['list-number'] = new ezcDocumentPcssStyleStringValue(
  171. $this->docBookNumberMap[$docBookElement->getAttribute( 'numeration' )]
  172. );
  173. }
  174. else if ( isset( $styles['list-style-type'] ) )
  175. {
  176. $styles['list-number'] = new ezcDocumentPcssStyleStringValue(
  177. $this->cssNumberMap[$styles['list-style-type']->value]
  178. );
  179. }
  180. else
  181. {
  182. $styles['list-number'] = new ezcDocumentPcssStyleStringValue(
  183. '1'
  184. );
  185. }
  186. }
  187. return $styles;
  188. }
  189. }
  190. ?>