PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/server/vendor/kramdown/lib/kramdown/parser/kramdown/smart_quotes.rb

https://gitlab.com/hwhelchel/sonic-pi
Ruby | 173 lines | 49 code | 4 blank | 120 comment | 0 complexity | 529bd1e6d906663ef02a602ea9c629aa MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. #
  3. #--
  4. # Copyright (C) 2009-2014 Thomas Leitner <t_leitner@gmx.at>
  5. #
  6. # This file is part of kramdown which is licensed under the MIT.
  7. #++
  8. #
  9. #--
  10. # Parts of this file are based on code from RubyPants:
  11. #
  12. # = RubyPants -- SmartyPants ported to Ruby
  13. #
  14. # Ported by Christian Neukirchen <mailto:chneukirchen@gmail.com>
  15. # Copyright (C) 2004 Christian Neukirchen
  16. #
  17. # Incooporates ideas, comments and documentation by Chad Miller
  18. # Copyright (C) 2004 Chad Miller
  19. #
  20. # Original SmartyPants by John Gruber
  21. # Copyright (C) 2003 John Gruber
  22. #
  23. #
  24. # = RubyPants -- SmartyPants ported to Ruby
  25. #
  26. #
  27. # [snip]
  28. #
  29. # == Authors
  30. #
  31. # John Gruber did all of the hard work of writing this software in
  32. # Perl for Movable Type and almost all of this useful documentation.
  33. # Chad Miller ported it to Python to use with Pyblosxom.
  34. #
  35. # Christian Neukirchen provided the Ruby port, as a general-purpose
  36. # library that follows the *Cloth API.
  37. #
  38. #
  39. # == Copyright and License
  40. #
  41. # === SmartyPants license:
  42. #
  43. # Copyright (c) 2003 John Gruber
  44. # (http://daringfireball.net)
  45. # All rights reserved.
  46. #
  47. # Redistribution and use in source and binary forms, with or without
  48. # modification, are permitted provided that the following conditions
  49. # are met:
  50. #
  51. # * Redistributions of source code must retain the above copyright
  52. # notice, this list of conditions and the following disclaimer.
  53. #
  54. # * Redistributions in binary form must reproduce the above copyright
  55. # notice, this list of conditions and the following disclaimer in
  56. # the documentation and/or other materials provided with the
  57. # distribution.
  58. #
  59. # * Neither the name "SmartyPants" nor the names of its contributors
  60. # may be used to endorse or promote products derived from this
  61. # software without specific prior written permission.
  62. #
  63. # This software is provided by the copyright holders and contributors
  64. # "as is" and any express or implied warranties, including, but not
  65. # limited to, the implied warranties of merchantability and fitness
  66. # for a particular purpose are disclaimed. In no event shall the
  67. # copyright owner or contributors be liable for any direct, indirect,
  68. # incidental, special, exemplary, or consequential damages (including,
  69. # but not limited to, procurement of substitute goods or services;
  70. # loss of use, data, or profits; or business interruption) however
  71. # caused and on any theory of liability, whether in contract, strict
  72. # liability, or tort (including negligence or otherwise) arising in
  73. # any way out of the use of this software, even if advised of the
  74. # possibility of such damage.
  75. #
  76. # === RubyPants license
  77. #
  78. # RubyPants is a derivative work of SmartyPants and smartypants.py.
  79. #
  80. # Redistribution and use in source and binary forms, with or without
  81. # modification, are permitted provided that the following conditions
  82. # are met:
  83. #
  84. # * Redistributions of source code must retain the above copyright
  85. # notice, this list of conditions and the following disclaimer.
  86. #
  87. # * Redistributions in binary form must reproduce the above copyright
  88. # notice, this list of conditions and the following disclaimer in
  89. # the documentation and/or other materials provided with the
  90. # distribution.
  91. #
  92. # This software is provided by the copyright holders and contributors
  93. # "as is" and any express or implied warranties, including, but not
  94. # limited to, the implied warranties of merchantability and fitness
  95. # for a particular purpose are disclaimed. In no event shall the
  96. # copyright owner or contributors be liable for any direct, indirect,
  97. # incidental, special, exemplary, or consequential damages (including,
  98. # but not limited to, procurement of substitute goods or services;
  99. # loss of use, data, or profits; or business interruption) however
  100. # caused and on any theory of liability, whether in contract, strict
  101. # liability, or tort (including negligence or otherwise) arising in
  102. # any way out of the use of this software, even if advised of the
  103. # possibility of such damage.
  104. #
  105. # == Links
  106. #
  107. # John Gruber:: http://daringfireball.net
  108. # SmartyPants:: http://daringfireball.net/projects/smartypants
  109. #
  110. # Chad Miller:: http://web.chad.org
  111. #
  112. # Christian Neukirchen:: http://kronavita.de/chris
  113. #
  114. #++
  115. #
  116. module Kramdown
  117. module Parser
  118. class Kramdown
  119. SQ_PUNCT = '[!"#\$\%\'()*+,\-.\/:;<=>?\@\[\\\\\]\^_`{|}~]'
  120. SQ_CLOSE = %![^\ \\\\\t\r\n\\[{(-]!
  121. SQ_RULES = [
  122. [/("|')(?=[_*]{1,2}\S)/, [:lquote1]],
  123. [/("|')(?=#{SQ_PUNCT}\B)/, [:rquote1]],
  124. # Special case for double sets of quotes, e.g.:
  125. # <p>He said, "'Quoted' words in a larger quote."</p>
  126. [/(\s?)"'(?=\w)/, [1, :ldquo, :lsquo]],
  127. [/(\s?)'"(?=\w)/, [1, :lsquo, :ldquo]],
  128. # Special case for decade abbreviations (the '80s):
  129. [/(\s?)'(?=\d\ds)/, [1, :rsquo]],
  130. # Get most opening single/double quotes:
  131. [/(\s)('|")(?=\w)/, [1, :lquote2]],
  132. # Single/double closing quotes:
  133. [/(#{SQ_CLOSE})('|")/, [1, :rquote2]],
  134. # Special case for e.g. "<i>Custer</i>'s Last Stand."
  135. [/("|')(\s|s\b|$)/, [:rquote1, 2]],
  136. # Any remaining single quotes should be opening ones:
  137. [/(.?)'/m, [1, :lsquo]],
  138. [/(.?)"/m, [1, :ldquo]],
  139. ] #'"
  140. SQ_SUBSTS = {
  141. [:rquote1, '"'] => :rdquo,
  142. [:rquote1, "'"] => :rsquo,
  143. [:rquote2, '"'] => :rdquo,
  144. [:rquote2, "'"] => :rsquo,
  145. [:lquote1, '"'] => :ldquo,
  146. [:lquote1, "'"] => :lsquo,
  147. [:lquote2, '"'] => :ldquo,
  148. [:lquote2, "'"] => :lsquo,
  149. }
  150. SMART_QUOTES_RE = /[^\\]?["']/
  151. # Parse the smart quotes at current location.
  152. def parse_smart_quotes
  153. substs = SQ_RULES.find {|reg, subst| @src.scan(reg)}[1]
  154. substs.each do |subst|
  155. if subst.kind_of?(Integer)
  156. add_text(@src[subst])
  157. else
  158. val = SQ_SUBSTS[[subst, @src[subst.to_s[-1,1].to_i]]] || subst
  159. @tree.children << Element.new(:smart_quote, val)
  160. end
  161. end
  162. end
  163. define_parser(:smart_quotes, SMART_QUOTES_RE, '[^\\\\]?["\']')
  164. end
  165. end
  166. end