PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/scala/de/lecturedoc/parser/Decorator.scala

https://bitbucket.org/delors/lecturedoc
Scala | 194 lines | 84 code | 30 blank | 80 comment | 0 complexity | b2574667aea797422b2b01dd5c02b174 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. /*
  2. * (c) 2013 Michael Eichberg et al.
  3. * https://bitbucket.org/delors/lecturedoc
  4. *
  5. * See LICENSE and Actuarius-LICENSE for license details.
  6. */
  7. package de.lecturedoc
  8. package parser
  9. /**
  10. * This trait influences the behavior of the LectureDoc output of inline and block parsers
  11. * and the complete transformer.
  12. *
  13. * Mix in this trait and override methods to change the behavior and override the "decorateXXX()" method
  14. * in the respective parser/transformer to return your modified instances to change the output they create.
  15. *
  16. * Inline element decoration methods always get passed the spanned text, so you have to
  17. * prepend and append the opening/closing tags. For block elements there is always a method
  18. * for the opening and closing tags. This is to make block
  19. * processing more efficient to prevent unnecessary String building of whole blocks just to
  20. * add tags. (The block building uses a StringBuilder internally and just appends the returned tags)
  21. *
  22. * If you want line breaks after opening/closing block level tags, you have to add the newline yourself.
  23. */
  24. trait Decorator {
  25. /**
  26. * The string used to indent one level. Defaults to the empty string.
  27. */
  28. def indentation() = ""
  29. /**
  30. * If true, inline xml tags and verbatim xml blocks are allowed,
  31. * otherwise they are escaped and included as plain text.
  32. */
  33. def allowVerbatimXml(): Boolean = true
  34. /**
  35. * Used to print out manual line breaks (default: <br />).
  36. */
  37. def decorateBreak(): String = "<br />"
  38. /**
  39. * Used to print out inline code (default: <code>...</code>).
  40. */
  41. def decorateCode(code: String): String = {
  42. "<code>" + code + "</code>"
  43. }
  44. /**
  45. * Used to print out emphasized text (default <em>...</em>).
  46. */
  47. def decorateEmphasis(text: String): String = "<em>"+text+"</em>"
  48. /**
  49. * Used to print out strong text (default: <strong>...</strong>).
  50. */
  51. def decorateStrong(text: String): String = "<strong>"+text+"</strong>"
  52. /**
  53. * Used to print link elements (default: <a href...).
  54. */
  55. def decorateLink(text: String, url: String, title: Option[String]): String = title match {
  56. case None "<a href=\""+url+"\">"+text+"</a>"
  57. case Some(t) "<a href=\""+url+"\" title=\""+t+"\">"+text+"</a>"
  58. }
  59. /**
  60. * Used to print image elements (default: <img ...)
  61. */
  62. def decorateImg(alt: String, src: String, title: Option[String]): String = title match {
  63. case None "<img src=\""+src+"\" alt=\""+alt+"\" />"
  64. case Some(t) "<img src=\""+src+"\" alt=\""+alt+"\" title=\""+t+"\" />"
  65. }
  66. /**
  67. * Used to print a horizontal ruler (default: to "<hr />\n").
  68. */
  69. def decorateRuler(): String = "<hr />\n"
  70. /**
  71. * Used to print the beginning of a header (default: "<h[HEADER_NO]>").
  72. */
  73. def decorateHeaderOpen(headerNo: Int, customDataAttributes: Option[List[CustomDataAttribute]]): String = {
  74. "<h" +
  75. headerNo +
  76. customDataAttributes.map(cdas " "+cdas.map(_.toHTML5DataAttribute).mkString(" ")).getOrElse("") +
  77. ">"
  78. }
  79. /**
  80. * Used to print the end of a header (default: "</h[HEADER_NO]\n>").
  81. */
  82. def decorateHeaderClose(headerNo: Int): String = "</h"+headerNo+">\n"
  83. /**
  84. * Used to print the beginning of a code block (default: "<pre><code[ class="LANGUAGE"]>").
  85. */
  86. def decorateCodeBlockOpen(language: Option[String], customDataAttributes: Option[List[CustomDataAttribute]]): String = {
  87. language match {
  88. case Some(language) {
  89. "<pre" +
  90. customDataAttributes.map(cdas " "+cdas.map(_.toHTML5DataAttribute).mkString(" ")).getOrElse("") +
  91. "><code class=\""+language+"\">"
  92. }
  93. case None {
  94. "<pre" +
  95. customDataAttributes.map(cdas " "+cdas.map(_.toHTML5DataAttribute).mkString(" ")).getOrElse("") +
  96. "><code>"
  97. }
  98. }
  99. }
  100. /**
  101. * Used to print the end of a code block (default: "</code></pre>\n").
  102. */
  103. def decorateCodeBlockClose(): String = "</code></pre>\n"
  104. /**
  105. * Used to print the beginning of a section.
  106. */
  107. def decorateSectionOpen(
  108. sectionType: String,
  109. sectionTitle: Option[String],
  110. sectionClass: Option[String],
  111. customDataAttributes: Option[List[CustomDataAttribute]]): String = {
  112. "<"+sectionType +
  113. sectionClass.map(" class=\""+_+"\"").getOrElse("") +
  114. sectionTitle.map(" data-title=\""+_+"\"").getOrElse("") +
  115. customDataAttributes.map(cdas " "+cdas.map(_.toHTML5DataAttribute).mkString(" ")).getOrElse("") +
  116. "><div class=\""+sectionType+"-body\">"
  117. }
  118. /** used to print the end of a section */
  119. def decorateSectionClose(sectionType: String): String = "</div></"+sectionType+">\n"
  120. /** used to print the beginning of a paragraph, defaults to "<p>" */
  121. def decorateParagraphOpen(customDataAttributes: Option[List[CustomDataAttribute]]): String = {
  122. "<p" +
  123. customDataAttributes.map(cdas " "+cdas.map(_.toHTML5DataAttribute).mkString(" ")).getOrElse("") +
  124. ">"
  125. }
  126. /** used to print the end of a paragraph, defaults to "</p>\n" */
  127. def decorateParagraphClose(): String = "</p>\n"
  128. /** used to print the beginning of a statement, defaults to "<div>" */
  129. def decorateStatementOpen(customDataAttributes: Option[List[CustomDataAttribute]]): String = {
  130. "<div class=\"statement\"" +
  131. customDataAttributes.map(cdas " "+cdas.map(_.toHTML5DataAttribute).mkString(" ")).getOrElse("") +
  132. ">"
  133. }
  134. /** used to print the end of a statement, defaults to "</div>\n" */
  135. def decorateStatementClose(): String = "</div>\n"
  136. /** used to print the beginning of a blockquote, defaults to "<blockquote>" */
  137. def decorateBlockQuoteOpen(customDataAttributes: Option[List[CustomDataAttribute]]): String = {
  138. "<blockquote" +
  139. customDataAttributes.map(cdas " "+cdas.map(_.toHTML5DataAttribute).mkString(" ")).getOrElse("") +
  140. ">"
  141. }
  142. /** used to print the end of a blockquote, defaults to "</blockquote>\n" */
  143. def decorateBlockQuoteClose(): String = "</blockquote>\n"
  144. /** used to print the beginning of a list item, defaults to "<li>" */
  145. def decorateItemOpen(customDataAttributes: Option[List[CustomDataAttribute]]): String = {
  146. "<li" +
  147. customDataAttributes.map(cdas " "+cdas.map(_.toHTML5DataAttribute).mkString(" ")).getOrElse("") +
  148. ">"
  149. }
  150. /** used to print the end of a list item, defaults to "</li>" */
  151. def decorateItemClose(): String = "</li>\n"
  152. /** used to print the beginning of an unordered list, defaults to "<ul>\n" */
  153. def decorateUListOpen(): String = "<ul>\n"
  154. /** used to print the end of an unordered list, defaults to "</ul>\n" */
  155. def decorateUListClose(): String = "</ul>\n"
  156. /** used to print the beginning of an ordered list, defaults to <ol>\n */
  157. def decorateOListOpen(): String = "<ol>\n"
  158. /** used to print the end of an ordered list, defaults to </ol>\n */
  159. def decorateOListClose(): String = "</ol>\n"
  160. }
  161. /**
  162. * Default instance of Decorator with the standard Markdown behavior.
  163. */
  164. object Decorator extends Decorator