KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smartypants.php 40KB

9 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. <?php
  2. #
  3. # SmartyPants Typographer - Smart typography for web sites
  4. #
  5. # PHP SmartyPants & Typographer
  6. # Copyright (c) 2004-2006 Michel Fortin
  7. # <http://www.michelf.com/>
  8. #
  9. # Original SmartyPants
  10. # Copyright (c) 2003-2004 John Gruber
  11. # <http://daringfireball.net/>
  12. #
  13. define( 'SMARTYPANTS_VERSION', "1.5.1oo2" ); # Unreleased
  14. define( 'SMARTYPANTSTYPOGRAPHER_VERSION', "1.0" ); # Wed 28 Jun 2006
  15. #
  16. # Default configuration:
  17. #
  18. # 1 -> "--" for em-dashes; no en-dash support
  19. # 2 -> "---" for em-dashes; "--" for en-dashes
  20. # 3 -> "--" for em-dashes; "---" for en-dashes
  21. # See docs for more configuration options.
  22. #
  23. define( 'SMARTYPANTS_ATTR', 1 );
  24. # Openning and closing smart double-quotes.
  25. define( 'SMARTYPANTS_SMART_DOUBLEQUOTE_OPEN', "&#8220;" );
  26. define( 'SMARTYPANTS_SMART_DOUBLEQUOTE_CLOSE', "&#8221;" );
  27. # Space around em-dashes. "He_—_or she_—_should change that."
  28. define( 'SMARTYPANTS_SPACE_EMDASH', " " );
  29. # Space around en-dashes. "He_–_or she_–_should change that."
  30. define( 'SMARTYPANTS_SPACE_ENDASH', " " );
  31. # Space before a colon. "He said_: here it is."
  32. define( 'SMARTYPANTS_SPACE_COLON', "&#160;" );
  33. # Space before a semicolon. "That's what I said_; that's what he said."
  34. define( 'SMARTYPANTS_SPACE_SEMICOLON', "&#160;" );
  35. # Space before a question mark and an exclamation mark: "¡_Holà_! What_?"
  36. define( 'SMARTYPANTS_SPACE_MARKS', "&#160;" );
  37. # Space inside french quotes. "Voici la «_chose_» qui m'a attaqué."
  38. define( 'SMARTYPANTS_SPACE_FRENCHQUOTE', "&#160;" );
  39. # Space as thousand separator. "On compte 10_000 maisons sur cette liste."
  40. define( 'SMARTYPANTS_SPACE_THOUSAND', "&#160;" );
  41. # Space before a unit abreviation. "This 12_kg of matter costs 10_$."
  42. define( 'SMARTYPANTS_SPACE_UNIT', "&#160;" );
  43. # SmartyPants will not alter the content of these tags:
  44. define( 'SMARTYPANTS_TAGS_TO_SKIP', 'pre|code|kbd|script|math');
  45. ### Standard Function Interface ###
  46. define( 'SMARTYPANTS_PARSER_CLASS', 'SmartyPantsTypographer_Parser' );
  47. function SmartyPants($text, $attr = SMARTYPANTS_ATTR) {
  48. #
  49. # Initialize the parser and return the result of its transform method.
  50. #
  51. # Setup static parser array.
  52. static $parser = array();
  53. if (!isset($parser[$attr])) {
  54. $parser_class = SMARTYPANTS_PARSER_CLASS;
  55. $parser[$attr] = new $parser_class($attr);
  56. }
  57. # Transform text using parser.
  58. return $parser[$attr]->transform($text);
  59. }
  60. function SmartQuotes($text, $attr = 1) {
  61. switch ($attr) {
  62. case 0: return $text;
  63. case 2: $attr = 'qb'; break;
  64. default: $attr = 'q'; break;
  65. }
  66. return SmartyPants($text, $attr);
  67. }
  68. function SmartDashes($text, $attr = 1) {
  69. switch ($attr) {
  70. case 0: return $text;
  71. case 2: $attr = 'D'; break;
  72. case 3: $attr = 'i'; break;
  73. default: $attr = 'd'; break;
  74. }
  75. return SmartyPants($text, $attr);
  76. }
  77. function SmartEllipsis($text, $attr = 1) {
  78. switch ($attr) {
  79. case 0: return $text;
  80. default: $attr = 'e'; break;
  81. }
  82. return SmartyPants($text, $attr);
  83. }
  84. ### WordPress Plugin Interface ###
  85. /*
  86. Plugin Name: SmartyPants Typographer
  87. Plugin URI: http://www.michelf.com/projects/php-smartypants/
  88. Description: SmartyPants is a web publishing utility that translates plain ASCII punctuation characters into &#8220;smart&#8221; typographic punctuation HTML entities. The Typographer extension will also replace normal spaces with unbrekable ones where appropriate to silently remove unwanted line breaks around punctuation and at some other places. This plugin <strong>replace the default WordPress Texturize algorithm</strong> for the content and the title of your posts, the comments body and author name, and everywhere else Texturize normally apply.
  89. Version: 1.0
  90. Author: Michel Fortin
  91. Author URI: http://www.michelf.com/
  92. */
  93. if (isset($wp_version)) {
  94. # Remove default Texturize filter that would conflict with SmartyPants.
  95. remove_filter('category_description', 'wptexturize');
  96. remove_filter('list_cats', 'wptexturize');
  97. remove_filter('comment_author', 'wptexturize');
  98. remove_filter('comment_text', 'wptexturize');
  99. remove_filter('single_post_title', 'wptexturize');
  100. remove_filter('the_title', 'wptexturize');
  101. remove_filter('the_content', 'wptexturize');
  102. remove_filter('the_excerpt', 'wptexturize');
  103. # Add SmartyPants filter with priority 10 (same as Texturize).
  104. add_filter('category_description', 'SmartyPants', 10);
  105. add_filter('list_cats', 'SmartyPants', 10);
  106. add_filter('comment_author', 'SmartyPants', 10);
  107. add_filter('comment_text', 'SmartyPants', 10);
  108. add_filter('single_post_title', 'SmartyPants', 10);
  109. add_filter('the_title', 'SmartyPants', 10);
  110. add_filter('the_content', 'SmartyPants', 10);
  111. add_filter('the_excerpt', 'SmartyPants', 10);
  112. }
  113. ### Smarty Modifier Interface ###
  114. function smarty_modifier_smartypants($text, $attr = NULL) {
  115. return SmartyPants($text, $attr);
  116. }
  117. #
  118. # SmartyPants Parser Class
  119. #
  120. class SmartyPants_Parser {
  121. # Options to specify which transformations to make:
  122. var $do_nothing = 0;
  123. var $do_quotes = 0;
  124. var $do_backticks = 0;
  125. var $do_dashes = 0;
  126. var $do_ellipses = 0;
  127. var $do_stupefy = 0;
  128. var $convert_quot = 0; # should we translate &quot; entities into normal quotes?
  129. function SmartyPants_Parser($attr = SMARTYPANTS_ATTR) {
  130. #
  131. # Initialize a SmartyPants_Parser with certain attributes.
  132. #
  133. # Parser attributes:
  134. # 0 : do nothing
  135. # 1 : set all
  136. # 2 : set all, using old school en- and em- dash shortcuts
  137. # 3 : set all, using inverted old school en and em- dash shortcuts
  138. #
  139. # q : quotes
  140. # b : backtick quotes (``double'' only)
  141. # B : backtick quotes (``double'' and `single')
  142. # d : dashes
  143. # D : old school dashes
  144. # i : inverted old school dashes
  145. # e : ellipses
  146. # w : convert &quot; entities to " for Dreamweaver users
  147. #
  148. if ($attr == "0") {
  149. $this->do_nothing = 1;
  150. }
  151. else if ($attr == "1") {
  152. # Do everything, turn all options on.
  153. $this->do_quotes = 1;
  154. $this->do_backticks = 1;
  155. $this->do_dashes = 1;
  156. $this->do_ellipses = 1;
  157. }
  158. else if ($attr == "2") {
  159. # Do everything, turn all options on, use old school dash shorthand.
  160. $this->do_quotes = 1;
  161. $this->do_backticks = 1;
  162. $this->do_dashes = 2;
  163. $this->do_ellipses = 1;
  164. }
  165. else if ($attr == "3") {
  166. # Do everything, turn all options on, use inverted old school dash shorthand.
  167. $this->do_quotes = 1;
  168. $this->do_backticks = 1;
  169. $this->do_dashes = 3;
  170. $this->do_ellipses = 1;
  171. }
  172. else if ($attr == "-1") {
  173. # Special "stupefy" mode.
  174. $this->do_stupefy = 1;
  175. }
  176. else {
  177. $chars = preg_split('//', $attr);
  178. foreach ($chars as $c){
  179. if ($c == "q") { $this->do_quotes = 1; }
  180. else if ($c == "b") { $this->do_backticks = 1; }
  181. else if ($c == "B") { $this->do_backticks = 2; }
  182. else if ($c == "d") { $this->do_dashes = 1; }
  183. else if ($c == "D") { $this->do_dashes = 2; }
  184. else if ($c == "i") { $this->do_dashes = 3; }
  185. else if ($c == "e") { $this->do_ellipses = 1; }
  186. else if ($c == "w") { $this->convert_quot = 1; }
  187. else {
  188. # Unknown attribute option, ignore.
  189. }
  190. }
  191. }
  192. }
  193. function transform($text) {
  194. if ($this->do_nothing) {
  195. return $text;
  196. }
  197. $tokens = $this->tokenizeHTML($text);
  198. $result = '';
  199. $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags.
  200. $prev_token_last_char = ""; # This is a cheat, used to get some context
  201. # for one-character tokens that consist of
  202. # just a quote char. What we do is remember
  203. # the last character of the previous text
  204. # token, to use as context to curl single-
  205. # character quote tokens correctly.
  206. foreach ($tokens as $cur_token) {
  207. if ($cur_token[0] == "tag") {
  208. # Don't mess with quotes inside tags.
  209. $result .= $cur_token[1];
  210. if (preg_match('@<(/?)(?:'.SMARTYPANTS_TAGS_TO_SKIP.')[\s>]@', $cur_token[1], $matches)) {
  211. $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
  212. }
  213. } else {
  214. $t = $cur_token[1];
  215. $last_char = substr($t, -1); # Remember last char of this token before processing.
  216. if (! $in_pre) {
  217. $t = $this->educate($t, $prev_token_last_char);
  218. }
  219. $prev_token_last_char = $last_char;
  220. $result .= $t;
  221. }
  222. }
  223. return $result;
  224. }
  225. function educate($t, $prev_token_last_char) {
  226. $t = $this->processEscapes($t);
  227. if ($this->convert_quot) {
  228. $t = preg_replace('/&quot;/', '"', $t);
  229. }
  230. if ($this->do_dashes) {
  231. if ($this->do_dashes == 1) $t = $this->educateDashes($t);
  232. if ($this->do_dashes == 2) $t = $this->educateDashesOldSchool($t);
  233. if ($this->do_dashes == 3) $t = $this->educateDashesOldSchoolInverted($t);
  234. }
  235. if ($this->do_ellipses) $t = $this->educateEllipses($t);
  236. # Note: backticks need to be processed before quotes.
  237. if ($this->do_backticks) {
  238. $t = $this->educateBackticks($t);
  239. if ($this->do_backticks == 2) $t = $this->educateSingleBackticks($t);
  240. }
  241. if ($this->do_quotes) {
  242. if ($t == "'") {
  243. # Special case: single-character ' token
  244. if (preg_match('/\S/', $prev_token_last_char)) {
  245. $t = "&#8217;";
  246. }
  247. else {
  248. $t = "&#8216;";
  249. }
  250. }
  251. else if ($t == '"') {
  252. # Special case: single-character " token
  253. if (preg_match('/\S/', $prev_token_last_char)) {
  254. $t = "&#8221;";
  255. }
  256. else {
  257. $t = "&#8220;";
  258. }
  259. }
  260. else {
  261. # Normal case:
  262. $t = $this->educateQuotes($t);
  263. }
  264. }
  265. if ($this->do_stupefy) $t = $this->stupefyEntities($t);
  266. return $t;
  267. }
  268. function educateQuotes($_) {
  269. #
  270. # Parameter: String.
  271. #
  272. # Returns: The string, with "educated" curly quote HTML entities.
  273. #
  274. # Example input: "Isn't this fun?"
  275. # Example output: &#8220;Isn&#8217;t this fun?&#8221;
  276. #
  277. # Make our own "punctuation" character class, because the POSIX-style
  278. # [:PUNCT:] is only available in Perl 5.6 or later:
  279. $punct_class = "[!\"#\\$\\%'()*+,-.\\/:;<=>?\\@\\[\\\\\]\\^_`{|}~]";
  280. # Special case if the very first character is a quote
  281. # followed by punctuation at a non-word-break. Close the quotes by brute force:
  282. $_ = preg_replace(
  283. array("/^'(?=$punct_class\\B)/", "/^\"(?=$punct_class\\B)/"),
  284. array('&#8217;', '&#8221;'), $_);
  285. # Special case for double sets of quotes, e.g.:
  286. # <p>He said, "'Quoted' words in a larger quote."</p>
  287. $_ = preg_replace(
  288. array("/\"'(?=\w)/", "/'\"(?=\w)/"),
  289. array('&#8220;&#8216;', '&#8216;&#8220;'), $_);
  290. # Special case for decade abbreviations (the '80s):
  291. $_ = preg_replace("/'(?=\\d{2}s)/", '&#8217;', $_);
  292. $close_class = '[^\ \t\r\n\[\{\(\-]';
  293. $dec_dashes = '&\#8211;|&\#8212;';
  294. # Get most opening single quotes:
  295. $_ = preg_replace("{
  296. (
  297. \\s | # a whitespace char, or
  298. &nbsp; | # a non-breaking space entity, or
  299. -- | # dashes, or
  300. &[mn]dash; | # named dash entities
  301. $dec_dashes | # or decimal entities
  302. &\\#x201[34]; # or hex
  303. )
  304. ' # the quote
  305. (?=\\w) # followed by a word character
  306. }x", '\1&#8216;', $_);
  307. # Single closing quotes:
  308. $_ = preg_replace("{
  309. ($close_class)?
  310. '
  311. (?(1)| # If $1 captured, then do nothing;
  312. (?=\\s | s\\b) # otherwise, positive lookahead for a whitespace
  313. ) # char or an 's' at a word ending position. This
  314. # is a special case to handle something like:
  315. # \"<i>Custer</i>'s Last Stand.\"
  316. }xi", '\1&#8217;', $_);
  317. # Any remaining single quotes should be opening ones:
  318. $_ = str_replace("'", '&#8216;', $_);
  319. # Get most opening double quotes:
  320. $_ = preg_replace("{
  321. (
  322. \\s | # a whitespace char, or
  323. &nbsp; | # a non-breaking space entity, or
  324. -- | # dashes, or
  325. &[mn]dash; | # named dash entities
  326. $dec_dashes | # or decimal entities
  327. &\\#x201[34]; # or hex
  328. )
  329. \" # the quote
  330. (?=\\w) # followed by a word character
  331. }x", '\1&#8220;', $_);
  332. # Double closing quotes:
  333. $_ = preg_replace("{
  334. ($close_class)?
  335. \"
  336. (?(1)|(?=\\s)) # If $1 captured, then do nothing;
  337. # if not, then make sure the next char is whitespace.
  338. }x", '\1&#8221;', $_);
  339. # Any remaining quotes should be opening ones.
  340. $_ = str_replace('"', '&#8220;', $_);
  341. return $_;
  342. }
  343. function educateBackticks($_) {
  344. #
  345. # Parameter: String.
  346. # Returns: The string, with ``backticks'' -style double quotes
  347. # translated into HTML curly quote entities.
  348. #
  349. # Example input: ``Isn't this fun?''
  350. # Example output: &#8220;Isn't this fun?&#8221;
  351. #
  352. $_ = str_replace(array("``", "''",),
  353. array('&#8220;', '&#8221;'), $_);
  354. return $_;
  355. }
  356. function educateSingleBackticks($_) {
  357. #
  358. # Parameter: String.
  359. # Returns: The string, with `backticks' -style single quotes
  360. # translated into HTML curly quote entities.
  361. #
  362. # Example input: `Isn't this fun?'
  363. # Example output: &#8216;Isn&#8217;t this fun?&#8217;
  364. #
  365. $_ = str_replace(array("`", "'",),
  366. array('&#8216;', '&#8217;'), $_);
  367. return $_;
  368. }
  369. function educateDashes($_) {
  370. #
  371. # Parameter: String.
  372. #
  373. # Returns: The string, with each instance of "--" translated to
  374. # an em-dash HTML entity.
  375. #
  376. $_ = str_replace('--', '&#8212;', $_);
  377. return $_;
  378. }
  379. function educateDashesOldSchool($_) {
  380. #
  381. # Parameter: String.
  382. #
  383. # Returns: The string, with each instance of "--" translated to
  384. # an en-dash HTML entity, and each "---" translated to
  385. # an em-dash HTML entity.
  386. #
  387. # em en
  388. $_ = str_replace(array("---", "--",),
  389. array('&#8212;', '&#8211;'), $_);
  390. return $_;
  391. }
  392. function educateDashesOldSchoolInverted($_) {
  393. #
  394. # Parameter: String.
  395. #
  396. # Returns: The string, with each instance of "--" translated to
  397. # an em-dash HTML entity, and each "---" translated to
  398. # an en-dash HTML entity. Two reasons why: First, unlike the
  399. # en- and em-dash syntax supported by
  400. # EducateDashesOldSchool(), it's compatible with existing
  401. # entries written before SmartyPants 1.1, back when "--" was
  402. # only used for em-dashes. Second, em-dashes are more
  403. # common than en-dashes, and so it sort of makes sense that
  404. # the shortcut should be shorter to type. (Thanks to Aaron
  405. # Swartz for the idea.)
  406. #
  407. # en em
  408. $_ = str_replace(array("---", "--",),
  409. array('&#8211;', '&#8212;'), $_);
  410. return $_;
  411. }
  412. function educateEllipses($_) {
  413. #
  414. # Parameter: String.
  415. # Returns: The string, with each instance of "..." translated to
  416. # an ellipsis HTML entity. Also converts the case where
  417. # there are spaces between the dots.
  418. #
  419. # Example input: Huh...?
  420. # Example output: Huh&#8230;?
  421. #
  422. $_ = str_replace(array("...", ". . .",), '&#8230;', $_);
  423. return $_;
  424. }
  425. function stupefyEntities($_) {
  426. #
  427. # Parameter: String.
  428. # Returns: The string, with each SmartyPants HTML entity translated to
  429. # its ASCII counterpart.
  430. #
  431. # Example input: &#8220;Hello &#8212; world.&#8221;
  432. # Example output: "Hello -- world."
  433. #
  434. # en-dash em-dash
  435. $_ = str_replace(array('&#8211;', '&#8212;'),
  436. array('-', '--'), $_);
  437. # single quote open close
  438. $_ = str_replace(array('&#8216;', '&#8217;'), "'", $_);
  439. # double quote open close
  440. $_ = str_replace(array('&#8220;', '&#8221;'), '"', $_);
  441. $_ = str_replace('&#8230;', '...', $_); # ellipsis
  442. return $_;
  443. }
  444. function processEscapes($_) {
  445. #
  446. # Parameter: String.
  447. # Returns: The string, with after processing the following backslash
  448. # escape sequences. This is useful if you want to force a "dumb"
  449. # quote or other character to appear.
  450. #
  451. # Escape Value
  452. # ------ -----
  453. # \\ &#92;
  454. # \" &#34;
  455. # \' &#39;
  456. # \. &#46;
  457. # \- &#45;
  458. # \` &#96;
  459. #
  460. $_ = str_replace(
  461. array('\\\\', '\"', "\'", '\.', '\-', '\`'),
  462. array('&#92;', '&#34;', '&#39;', '&#46;', '&#45;', '&#96;'), $_);
  463. return $_;
  464. }
  465. function tokenizeHTML($str) {
  466. #
  467. # Parameter: String containing HTML markup.
  468. # Returns: An array of the tokens comprising the input
  469. # string. Each token is either a tag (possibly with nested,
  470. # tags contained therein, such as <a href="<MTFoo>">, or a
  471. # run of text between tags. Each element of the array is a
  472. # two-element array; the first is either 'tag' or 'text';
  473. # the second is the actual value.
  474. #
  475. #
  476. # Regular expression derived from the _tokenize() subroutine in
  477. # Brad Choate's MTRegex plugin.
  478. # <http://www.bradchoate.com/past/mtregex.php>
  479. #
  480. $index = 0;
  481. $tokens = array();
  482. $match = '(?s:<!(?:--.*?--\s*)+>)|'. # comment
  483. '(?s:<\?.*?\?>)|'. # processing instruction
  484. # regular tags
  485. '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)';
  486. $parts = preg_split("{($match)}", $str, -1, PREG_SPLIT_DELIM_CAPTURE);
  487. foreach ($parts as $part) {
  488. if (++$index % 2 && $part != '')
  489. $tokens[] = array('text', $part);
  490. else
  491. $tokens[] = array('tag', $part);
  492. }
  493. return $tokens;
  494. }
  495. }
  496. #
  497. # SmartyPants Typographer Parser Class
  498. #
  499. class SmartyPantsTypographer_Parser extends SmartyPants_Parser {
  500. # Options to specify which transformations to make:
  501. var $do_comma_quotes = 0;
  502. var $do_guillemets = 0;
  503. var $do_space_emdash = 0;
  504. var $do_space_endash = 0;
  505. var $do_space_colon = 0;
  506. var $do_space_semicolon = 0;
  507. var $do_space_marks = 0;
  508. var $do_space_frenchquote = 0;
  509. var $do_space_thousand = 0;
  510. var $do_space_unit = 0;
  511. # Smart quote characters:
  512. var $smart_doublequote_open = SMARTYPANTS_SMART_DOUBLEQUOTE_OPEN;
  513. var $smart_doublequote_close = SMARTYPANTS_SMART_DOUBLEQUOTE_CLOSE;
  514. var $smart_singlequote_open = '&#8216;';
  515. var $smart_singlequote_close = '&#8217;'; # Also apostrophe.
  516. # Space characters for different places:
  517. var $space_emdash = SMARTYPANTS_SPACE_EMDASH;
  518. var $space_endash = SMARTYPANTS_SPACE_ENDASH;
  519. var $space_colon = SMARTYPANTS_SPACE_COLON;
  520. var $space_semicolon = SMARTYPANTS_SPACE_SEMICOLON;
  521. var $space_marks = SMARTYPANTS_SPACE_MARKS;
  522. var $space_frenchquote = SMARTYPANTS_SPACE_FRENCHQUOTE;
  523. var $space_thousand = SMARTYPANTS_SPACE_THOUSAND;
  524. var $space_unit = SMARTYPANTS_SPACE_UNIT;
  525. # Expression of a space (breakable or not):
  526. var $space = '(?: | |&nbsp;|&#0*160;|&#x0*[aA]0;)';
  527. function SmartyPantsTypographer_Parser($attr = SMARTYPANTS_ATTR) {
  528. #
  529. # Initialize a SmartyPantsTypographer_Parser with certain attributes.
  530. #
  531. # Parser attributes:
  532. # 0 : do nothing
  533. # 1 : set all, except dash spacing
  534. # 2 : set all, except dash spacing, using old school en- and em- dash shortcuts
  535. # 3 : set all, except dash spacing, using inverted old school en and em- dash shortcuts
  536. #
  537. # Punctuation:
  538. # q -> quotes
  539. # b -> backtick quotes (``double'' only)
  540. # B -> backtick quotes (``double'' and `single')
  541. # c -> comma quotes (,,double`` only)
  542. # g -> guillemets (<<double>> only)
  543. # d -> dashes
  544. # D -> old school dashes
  545. # i -> inverted old school dashes
  546. # e -> ellipses
  547. # w -> convert &quot; entities to " for Dreamweaver users
  548. #
  549. # Spacing:
  550. # : -> colon spacing +-
  551. # ; -> semicolon spacing +-
  552. # m -> question and exclamation marks spacing +-
  553. # h -> em-dash spacing +-
  554. # H -> en-dash spacing +-
  555. # f -> french quote spacing +-
  556. # t -> thousand separator spacing -
  557. # u -> unit spacing +-
  558. # (you can add a plus sign after some of these options denoted by + to
  559. # add the space when it is not already present, or you can add a minus
  560. # sign to completly remove any space present)
  561. #
  562. # Initialize inherited SmartyPants parser.
  563. parent::SmartyPants_Parser($attr);
  564. if ($attr == "1" || $attr == "2" || $attr == "3") {
  565. # Do everything, turn all options on.
  566. $this->do_comma_quotes = 1;
  567. $this->do_guillemets = 1;
  568. $this->do_space_emdash = 1;
  569. $this->do_space_endash = 1;
  570. $this->do_space_colon = 1;
  571. $this->do_space_semicolon = 1;
  572. $this->do_space_marks = 1;
  573. $this->do_space_frenchquote = 1;
  574. $this->do_space_thousand = 1;
  575. $this->do_space_unit = 1;
  576. }
  577. else if ($attr == "-1") {
  578. # Special "stupefy" mode.
  579. $this->do_stupefy = 1;
  580. }
  581. else {
  582. $chars = preg_split('//', $attr);
  583. foreach ($chars as $c){
  584. if ($c == "c") { $current =& $this->do_comma_quotes; }
  585. else if ($c == "g") { $current =& $this->do_guillemets; }
  586. else if ($c == ":") { $current =& $this->do_space_colon; }
  587. else if ($c == ";") { $current =& $this->do_space_semicolon; }
  588. else if ($c == "m") { $current =& $this->do_space_marks; }
  589. else if ($c == "h") { $current =& $this->do_space_emdash; }
  590. else if ($c == "H") { $current =& $this->do_space_endash; }
  591. else if ($c == "f") { $current =& $this->do_space_frenchquote; }
  592. else if ($c == "t") { $current =& $this->do_space_thousand; }
  593. else if ($c == "u") { $current =& $this->do_space_unit; }
  594. else if ($c == "+") {
  595. $current = 2;
  596. unset($current);
  597. }
  598. else if ($c == "-") {
  599. $current = -1;
  600. unset($current);
  601. }
  602. else {
  603. # Unknown attribute option, ignore.
  604. }
  605. $current = 1;
  606. }
  607. }
  608. }
  609. function educate($t, $prev_token_last_char) {
  610. $t = parent::educate($t, $prev_token_last_char);
  611. if ($this->do_comma_quotes) $t = $this->educateCommaQuotes($t);
  612. if ($this->do_guillemets) $t = $this->educateGuillemets($t);
  613. if ($this->do_space_emdash) $t = $this->spaceEmDash($t);
  614. if ($this->do_space_endash) $t = $this->spaceEnDash($t);
  615. if ($this->do_space_colon) $t = $this->spaceColon($t);
  616. if ($this->do_space_semicolon) $t = $this->spaceSemicolon($t);
  617. if ($this->do_space_marks) $t = $this->spaceMarks($t);
  618. if ($this->do_space_frenchquote) $t = $this->spaceFrenchQuotes($t);
  619. if ($this->do_space_thousand) $t = $this->spaceThousandSeparator($t);
  620. if ($this->do_space_unit) $t = $this->spaceUnit($t);
  621. return $t;
  622. }
  623. function educateQuotes($_) {
  624. #
  625. # Parameter: String.
  626. #
  627. # Returns: The string, with "educated" curly quote HTML entities.
  628. #
  629. # Example input: "Isn't this fun?"
  630. # Example output: &#8220;Isn&#8217;t this fun?&#8221;
  631. #
  632. $dq_open = $this->smart_doublequote_open;
  633. $dq_close = $this->smart_doublequote_close;
  634. $sq_open = $this->smart_singlequote_open;
  635. $sq_close = $this->smart_singlequote_close;
  636. # Make our own "punctuation" character class, because the POSIX-style
  637. # [:PUNCT:] is only available in Perl 5.6 or later:
  638. $punct_class = "[!\"#\\$\\%'()*+,-.\\/:;<=>?\\@\\[\\\\\]\\^_`{|}~]";
  639. # Special case if the very first character is a quote
  640. # followed by punctuation at a non-word-break. Close the quotes by brute force:
  641. $_ = preg_replace(
  642. array("/^'(?=$punct_class\\B)/", "/^\"(?=$punct_class\\B)/"),
  643. array($sq_close, $dq_close), $_);
  644. # Special case for double sets of quotes, e.g.:
  645. # <p>He said, "'Quoted' words in a larger quote."</p>
  646. $_ = preg_replace(
  647. array("/\"'(?=\w)/", "/'\"(?=\w)/"),
  648. array($dq_open.$sq_open, $sq_open.$dq_open), $_);
  649. # Special case for decade abbreviations (the '80s):
  650. $_ = preg_replace("/'(?=\\d{2}s)/", $sq_close, $_);
  651. $close_class = '[^\ \t\r\n\[\{\(\-]';
  652. $dec_dashes = '&\#8211;|&\#8212;';
  653. # Get most opening single quotes:
  654. $_ = preg_replace("{
  655. (
  656. \\s | # a whitespace char, or
  657. &nbsp; | # a non-breaking space entity, or
  658. -- | # dashes, or
  659. &[mn]dash; | # named dash entities
  660. $dec_dashes | # or decimal entities
  661. &\\#x201[34]; # or hex
  662. )
  663. ' # the quote
  664. (?=\\w) # followed by a word character
  665. }x", '\1'.$sq_open, $_);
  666. # Single closing quotes:
  667. $_ = preg_replace("{
  668. ($close_class)?
  669. '
  670. (?(1)| # If $1 captured, then do nothing;
  671. (?=\\s | s\\b) # otherwise, positive lookahead for a whitespace
  672. ) # char or an 's' at a word ending position. This
  673. # is a special case to handle something like:
  674. # \"<i>Custer</i>'s Last Stand.\"
  675. }xi", '\1'.$sq_close, $_);
  676. # Any remaining single quotes should be opening ones:
  677. $_ = str_replace("'", $sq_open, $_);
  678. # Get most opening double quotes:
  679. $_ = preg_replace("{
  680. (
  681. \\s | # a whitespace char, or
  682. &nbsp; | # a non-breaking space entity, or
  683. -- | # dashes, or
  684. &[mn]dash; | # named dash entities
  685. $dec_dashes | # or decimal entities
  686. &\\#x201[34]; # or hex
  687. )
  688. \" # the quote
  689. (?=\\w) # followed by a word character
  690. }x", '\1'.$dq_open, $_);
  691. # Double closing quotes:
  692. $_ = preg_replace("{
  693. ($close_class)?
  694. \"
  695. (?(1)|(?=\\s)) # If $1 captured, then do nothing;
  696. # if not, then make sure the next char is whitespace.
  697. }x", '\1'.$dq_close, $_);
  698. # Any remaining quotes should be opening ones.
  699. $_ = str_replace('"', $dq_open, $_);
  700. return $_;
  701. }
  702. function educateCommaQuotes($_) {
  703. #
  704. # Parameter: String.
  705. # Returns: The string, with ,,comma,, -style double quotes
  706. # translated into HTML curly quote entities.
  707. #
  708. # Example input: ,,Isn't this fun?,,
  709. # Example output: &#8222;Isn't this fun?&#8222;
  710. #
  711. # Note: this is meant to be used alongside with backtick quotes; there is
  712. # no language that use only lower quotations alone mark like in the example.
  713. #
  714. $_ = str_replace(",,", '&#8222;', $_);
  715. return $_;
  716. }
  717. function educateGuillemets($_) {
  718. #
  719. # Parameter: String.
  720. # Returns: The string, with << guillemets >> -style quotes
  721. # translated into HTML guillemets entities.
  722. #
  723. # Example input: << Isn't this fun? >>
  724. # Example output: &#8222; Isn't this fun? &#8222;
  725. #
  726. $_ = preg_replace("/(?:<|&lt;){2}/", '&#171;', $_);
  727. $_ = preg_replace("/(?:>|&gt;){2}/", '&#187;', $_);
  728. return $_;
  729. }
  730. function spaceFrenchQuotes($_) {
  731. #
  732. # Parameters: String, replacement character, and forcing flag.
  733. # Returns: The string, with appropriates spaces replaced
  734. # inside french-style quotes, only french quotes.
  735. #
  736. # Example input: Quotes in « French », »German« and »Finnish» style.
  737. # Example output: Quotes in «_French_», »German« and »Finnish» style.
  738. #
  739. $opt = ( $this->do_space_frenchquote == 2 ? '?' : '' );
  740. $chr = ( $this->do_space_frenchquote != -1 ? $this->space_frenchquote : '' );
  741. # Characters allowed immediatly outside quotes.
  742. $outside_char = $this->space . '|\s|[.,:;!?\[\](){}|@*~=+-]|¡|¿';
  743. $_ = preg_replace(
  744. "/(^|$outside_char)(&#171;|«|&#8250;|‹)$this->space$opt/",
  745. "\\1\\2$chr", $_);
  746. $_ = preg_replace(
  747. "/$this->space$opt(&#187;|»|&#8249;|›)($outside_char|$)/",
  748. "$chr\\1\\2", $_);
  749. return $_;
  750. }
  751. function spaceColon($_) {
  752. #
  753. # Parameters: String, replacement character, and forcing flag.
  754. # Returns: The string, with appropriates spaces replaced
  755. # before colons.
  756. #
  757. # Example input: Ingredients : fun.
  758. # Example output: Ingredients_: fun.
  759. #
  760. $opt = ( $this->do_space_colon == 2 ? '?' : '' );
  761. $chr = ( $this->do_space_colon != -1 ? $this->space_colon : '' );
  762. $_ = preg_replace("/$this->space$opt(:)(\\s|$)/m",
  763. "$chr\\1\\2", $_);
  764. return $_;
  765. }
  766. function spaceSemicolon($_) {
  767. #
  768. # Parameters: String, replacement character, and forcing flag.
  769. # Returns: The string, with appropriates spaces replaced
  770. # before semicolons.
  771. #
  772. # Example input: There he goes ; there she goes.
  773. # Example output: There he goes_; there she goes.
  774. #
  775. $opt = ( $this->do_space_semicolon == 2 ? '?' : '' );
  776. $chr = ( $this->do_space_semicolon != -1 ? $this->space_semicolon : '' );
  777. $_ = preg_replace("/$this->space(;)(?=\\s|$)/m",
  778. " \\1", $_);
  779. $_ = preg_replace("/((?:^|\\s)(?>[^&;\\s]+|&#?[a-zA-Z0-9]+;)*)".
  780. " $opt(;)(?=\\s|$)/m",
  781. "\\1$chr\\2", $_);
  782. return $_;
  783. }
  784. function spaceMarks($_) {
  785. #
  786. # Parameters: String, replacement character, and forcing flag.
  787. # Returns: The string, with appropriates spaces replaced
  788. # around question and exclamation marks.
  789. #
  790. # Example input: ¡ Holà ! What ?
  791. # Example output: ¡_Holà_! What_?
  792. #
  793. $opt = ( $this->do_space_marks == 2 ? '?' : '' );
  794. $chr = ( $this->do_space_marks != -1 ? $this->space_marks : '' );
  795. // Regular marks.
  796. $_ = preg_replace("/$this->space$opt([?!]+)/", "$chr\\1", $_);
  797. // Inverted marks.
  798. $imarks = "(?:¡|&iexcl;|&#161;|&#x[Aa]1;|¿|&iquest;|&#191;|&#x[Bb][Ff];)";
  799. $_ = preg_replace("/($imarks+)$this->space$opt/", "\\1$chr", $_);
  800. return $_;
  801. }
  802. function spaceEmDash($_) {
  803. #
  804. # Parameters: String, two replacement characters separated by a hyphen (`-`),
  805. # and forcing flag.
  806. #
  807. # Returns: The string, with appropriates spaces replaced
  808. # around dashes.
  809. #
  810. # Example input: Then — without any plan — the fun happend.
  811. # Example output: Then_—_without any plan_—_the fun happend.
  812. #
  813. $opt = ( $this->do_space_emdash == 2 ? '?' : '' );
  814. $chr = ( $this->do_space_emdash != -1 ? $this->space_emdash : '' );
  815. $_ = preg_replace("/$this->space$opt(&#8212;|—)$this->space$opt/",
  816. "$chr\\1$chr", $_);
  817. return $_;
  818. }
  819. function spaceEnDash($_) {
  820. #
  821. # Parameters: String, two replacement characters separated by a hyphen (`-`),
  822. # and forcing flag.
  823. #
  824. # Returns: The string, with appropriates spaces replaced
  825. # around dashes.
  826. #
  827. # Example input: Then — without any plan — the fun happend.
  828. # Example output: Then_—_without any plan_—_the fun happend.
  829. #
  830. $opt = ( $this->do_space_endash == 2 ? '?' : '' );
  831. $chr = ( $this->do_space_endash != -1 ? $this->space_endash : '' );
  832. $_ = preg_replace("/$this->space$opt(&#8211;|–)$this->space$opt/",
  833. "$chr\\1$chr", $_);
  834. return $_;
  835. }
  836. function spaceThousandSeparator($_) {
  837. #
  838. # Parameters: String, replacement character, and forcing flag.
  839. # Returns: The string, with appropriates spaces replaced
  840. # inside numbers (thousand separator in french).
  841. #
  842. # Example input: Il y a 10 000 insectes amusants dans ton jardin.
  843. # Example output: Il y a 10_000 insectes amusants dans ton jardin.
  844. #
  845. $chr = ( $this->do_space_thousand != -1 ? $this->space_thousand : '' );
  846. $_ = preg_replace('/([0-9]) ([0-9])/', "\\1$chr\\2", $_);
  847. return $_;
  848. }
  849. var $units = '
  850. ### Metric units (with prefixes)
  851. (?:
  852. p |
  853. µ | &micro; | &\#0*181; | &\#[xX]0*[Bb]5; |
  854. [mcdhkMGT]
  855. )?
  856. (?:
  857. [mgstAKNJWCVFSTHBL]|mol|cd|rad|Hz|Pa|Wb|lm|lx|Bq|Gy|Sv|kat|
  858. Ω | Ohm | &Omega; | &\#0*937; | &\#[xX]0*3[Aa]9;
  859. )|
  860. ### Computers units (KB, Kb, TB, Kbps)
  861. [kKMGT]?(?:[oBb]|[oBb]ps|flops)|
  862. ### Money
  863. ¢ | &cent; | &\#0*162; | &\#[xX]0*[Aa]2; |
  864. M?(?:
  865. £ | &pound; | &\#0*163; | &\#[xX]0*[Aa]3; |
  866. ¥ | &yen; | &\#0*165; | &\#[xX]0*[Aa]5; |
  867. € | &euro; | &\#0*8364; | &\#[xX]0*20[Aa][Cc]; |
  868. $
  869. )|
  870. ### Other units
  871. (?: ° | &deg; | &\#0*176; | &\#[xX]0*[Bb]0; ) [CF]? |
  872. %|pt|pi|M?px|em|en|gal|lb|[NSEOW]|[NS][EOW]|ha|mbar
  873. '; //x
  874. function spaceUnit($_) {
  875. #
  876. # Parameters: String, replacement character, and forcing flag.
  877. # Returns: The string, with appropriates spaces replaced
  878. # before unit symbols.
  879. #
  880. # Example input: Get 3 mol of fun for 3 $.
  881. # Example output: Get 3_mol of fun for 3_$.
  882. #
  883. $opt = ( $this->do_space_unit == 2 ? '?' : '' );
  884. $chr = ( $this->do_space_unit != -1 ? $this->space_unit : '' );
  885. $_ = preg_replace('/
  886. (?:([0-9])[ ]'.$opt.') # Number followed by space.
  887. ('.$this->units.') # Unit.
  888. (?![a-zA-Z0-9]) # Negative lookahead for other unit characters.
  889. /x',
  890. "\\1$chr\\2", $_);
  891. return $_;
  892. }
  893. function spaceAbbr($_) {
  894. #
  895. # Parameters: String, replacement character, and forcing flag.
  896. # Returns: The string, with appropriates spaces replaced
  897. # around abbreviations.
  898. #
  899. # Example input: Fun i.e. something pleasant.
  900. # Example output: Fun i.e._something pleasant.
  901. #
  902. $opt = ( $this->do_space_abbr == 2 ? '?' : '' );
  903. $_ = preg_replace("/(^|\s)($this->abbr_after) $opt/m",
  904. "\\1\\2$this->space_abbr", $_);
  905. $_ = preg_replace("/( )$opt($this->abbr_sp_before)(?![a-zA-Z'])/m",
  906. "\\1$this->space_abbr\\2", $_);
  907. return $_;
  908. }
  909. function stupefyEntities($_) {
  910. #
  911. # Adding angle quotes and lower quotes to SmartyPants's stupefy mode.
  912. #
  913. $_ = parent::stupefyEntities($_);
  914. $_ = str_replace(array('&#8222;', '&#171;', '&#187'), '"', $_);
  915. return $_;
  916. }
  917. function processEscapes($_) {
  918. #
  919. # Adding a few more escapes to SmartyPants's escapes:
  920. #
  921. # Escape Value
  922. # ------ -----
  923. # \, &#44;
  924. # \< &#60;
  925. # \> &#62;
  926. #
  927. $_ = parent::processEscapes($_);
  928. $_ = str_replace(
  929. array('\,', '\<', '\>', '\&lt;', '\&gt;'),
  930. array('&#44;', '&#60;', '&#62;', '&#60;', '&#62;'), $_);
  931. return $_;
  932. }
  933. }
  934. /*
  935. PHP SmartyPants Typographer
  936. ===========================
  937. Version History
  938. ---------------
  939. 1.0 (28 Jun 2006)
  940. * First public release of PHP SmartyPants Typographer.
  941. Bugs
  942. ----
  943. To file bug reports or feature requests (other than topics listed in the
  944. Caveats section above) please send email to:
  945. <michel.fortin@michelf.com>
  946. If the bug involves quotes being curled the wrong way, please send example
  947. text to illustrate.
  948. ### Algorithmic Shortcomings ###
  949. One situation in which quotes will get curled the wrong way is when
  950. apostrophes are used at the start of leading contractions. For example:
  951. 'Twas the night before Christmas.
  952. In the case above, SmartyPants will turn the apostrophe into an opening
  953. single-quote, when in fact it should be a closing one. I don't think
  954. this problem can be solved in the general case -- every word processor
  955. I've tried gets this wrong as well. In such cases, it's best to use the
  956. proper HTML entity for closing single-quotes (`&#8217;`) by hand.
  957. Copyright and License
  958. ---------------------
  959. PHP SmartyPants & Typographer
  960. Copyright (c) 2004-2006 Michel Fortin
  961. <http://www.michelf.com>
  962. All rights reserved.
  963. Original SmartyPants
  964. Copyright (c) 2003-2004 John Gruber
  965. <http://daringfireball.net/>
  966. All rights reserved.
  967. Redistribution and use in source and binary forms, with or without
  968. modification, are permitted provided that the following conditions are met:
  969. * Redistributions of source code must retain the above copyright
  970. notice, this list of conditions and the following disclaimer.
  971. * Redistributions in binary form must reproduce the above copyright
  972. notice, this list of conditions and the following disclaimer in the
  973. documentation and/or other materials provided with the distribution.
  974. * Neither the name "SmartyPants" nor the names of its contributors may
  975. be used to endorse or promote products derived from this software
  976. without specific prior written permission.
  977. This software is provided by the copyright holders and contributors "as is"
  978. and any express or implied warranties, including, but not limited to, the
  979. implied warranties of merchantability and fitness for a particular purpose
  980. are disclaimed. In no event shall the copyright owner or contributors be
  981. liable for any direct, indirect, incidental, special, exemplary, or
  982. consequential damages (including, but not limited to, procurement of
  983. substitute goods or services; loss of use, data, or profits; or business
  984. interruption) however caused and on any theory of liability, whether in
  985. contract, strict liability, or tort (including negligence or otherwise)
  986. arising in any way out of the use of this software, even if advised of the
  987. possibility of such damage.
  988. */
  989. ?>