markdown.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  1. <?php
  2. #
  3. # Markdown Extra - A text-to-HTML conversion tool for web writers
  4. #
  5. # PHP Markdown & Extra
  6. # Copyright (c) 2004-2012 Michel Fortin
  7. # <http://michelf.com/projects/php-markdown/>
  8. #
  9. # Original Markdown
  10. # Copyright (c) 2004-2006 John Gruber
  11. # <http://daringfireball.net/projects/markdown/>
  12. #
  13. #
  14. # Markdown Parser Class
  15. #
  16. class Markdown_Parser {
  17. # Regex to match balanced [brackets].
  18. # Needed to insert a maximum bracked depth while converting to PHP.
  19. public $nested_brackets_depth = 6;
  20. public $nested_brackets_re;
  21. public $nested_url_parenthesis_depth = 4;
  22. public $nested_url_parenthesis_re;
  23. # Table of hash values for escaped characters:
  24. public $escape_chars = '\`*_{}[]()>#+-.!';
  25. public $escape_chars_re;
  26. # Change to ">" for HTML output.
  27. public $empty_element_suffix = ' />';
  28. public $tab_width = 4;
  29. # Change to `true` to disallow markup or entities.
  30. public $no_markup = false;
  31. public $no_entities = false;
  32. # Predefined urls and titles for reference links and images.
  33. public $predef_urls = array();
  34. public $predef_titles = array();
  35. public function __construct() {
  36. #
  37. # Constructor function. Initialize appropriate member variables.
  38. #
  39. $this->_initDetab();
  40. $this->prepareItalicsAndBold();
  41. $this->nested_brackets_re =
  42. str_repeat('(?>[^\[\]]+|\[', $this->nested_brackets_depth).
  43. str_repeat('\])*', $this->nested_brackets_depth);
  44. $this->nested_url_parenthesis_re =
  45. str_repeat('(?>[^()\s]+|\(', $this->nested_url_parenthesis_depth).
  46. str_repeat('(?>\)))*', $this->nested_url_parenthesis_depth);
  47. $this->escape_chars_re = '['.preg_quote($this->escape_chars).']';
  48. # Sort document, block, and span gamut in ascendent priority order.
  49. asort($this->document_gamut);
  50. asort($this->block_gamut);
  51. asort($this->span_gamut);
  52. }
  53. # Internal hashes used during transformation.
  54. public $urls = array();
  55. public $titles = array();
  56. public $html_hashes = array();
  57. # Status flag to avoid invalid nesting.
  58. public $in_anchor = false;
  59. public function setup() {
  60. #
  61. # Called before the transformation process starts to setup parser
  62. # states.
  63. #
  64. # Clear global hashes.
  65. $this->urls = $this->predef_urls;
  66. $this->titles = $this->predef_titles;
  67. $this->html_hashes = array();
  68. $in_anchor = false;
  69. }
  70. public function teardown() {
  71. #
  72. # Called after the transformation process to clear any variable
  73. # which may be taking up memory unnecessarly.
  74. #
  75. $this->urls = array();
  76. $this->titles = array();
  77. $this->html_hashes = array();
  78. }
  79. public function transform($text) {
  80. #
  81. # Main function. Performs some preprocessing on the input text
  82. # and pass it through the document gamut.
  83. #
  84. $this->setup();
  85. # Remove UTF-8 BOM and marker character in input, if present.
  86. $text = preg_replace('{^\xEF\xBB\xBF|\x1A}', '', $text);
  87. # Standardize line endings:
  88. # DOS to Unix and Mac to Unix
  89. $text = preg_replace('{\r\n?}', "\n", $text);
  90. # Make sure $text ends with a couple of newlines:
  91. $text .= "\n\n";
  92. # Convert all tabs to spaces.
  93. $text = $this->detab($text);
  94. # Turn block-level HTML blocks into hash entries
  95. $text = $this->hashHTMLBlocks($text);
  96. # Strip any lines consisting only of spaces and tabs.
  97. # This makes subsequent regexen easier to write, because we can
  98. # match consecutive blank lines with /\n+/ instead of something
  99. # contorted like /[ ]*\n+/ .
  100. $text = preg_replace('/^[ ]+$/m', '', $text);
  101. # Run document gamut methods.
  102. foreach ($this->document_gamut as $method => $priority) {
  103. $text = $this->$method($text);
  104. }
  105. $this->teardown();
  106. return $text . "\n";
  107. }
  108. public $document_gamut = array(
  109. # Strip link definitions, store in hashes.
  110. "stripLinkDefinitions" => 20,
  111. "runBasicBlockGamut" => 30,
  112. );
  113. public function stripLinkDefinitions($text) {
  114. #
  115. # Strips link definitions from text, stores the URLs and titles in
  116. # hash references.
  117. #
  118. $less_than_tab = $this->tab_width - 1;
  119. # Link defs are in the form: ^[id]: url "optional title"
  120. $text = preg_replace_callback('{
  121. ^[ ]{0,'.$less_than_tab.'}\[(.+)\][ ]?: # id = $1
  122. [ ]*
  123. \n? # maybe *one* newline
  124. [ ]*
  125. (?:
  126. <(.+?)> # url = $2
  127. |
  128. (\S+?) # url = $3
  129. )
  130. [ ]*
  131. \n? # maybe one newline
  132. [ ]*
  133. (?:
  134. (?<=\s) # lookbehind for whitespace
  135. ["(]
  136. (.*?) # title = $4
  137. [")]
  138. [ ]*
  139. )? # title is optional
  140. (?:\n+|\Z)
  141. }xm',
  142. array(&$this, '_stripLinkDefinitions_callback'),
  143. $text);
  144. return $text;
  145. }
  146. public function _stripLinkDefinitions_callback($matches) {
  147. $link_id = strtolower($matches[1]);
  148. $url = $matches[2] == '' ? $matches[3] : $matches[2];
  149. $this->urls[$link_id] = $url;
  150. $this->titles[$link_id] =& $matches[4];
  151. return ''; # String that will replace the block
  152. }
  153. public function hashHTMLBlocks($text) {
  154. if ($this->no_markup) return $text;
  155. $less_than_tab = $this->tab_width - 1;
  156. # Hashify HTML blocks:
  157. # We only want to do this for block-level HTML tags, such as headers,
  158. # lists, and tables. That's because we still want to wrap <p>s around
  159. # "paragraphs" that are wrapped in non-block-level tags, such as anchors,
  160. # phrase emphasis, and spans. The list of tags we're looking for is
  161. # hard-coded:
  162. #
  163. # * List "a" is made of tags which can be both inline or block-level.
  164. # These will be treated block-level when the start tag is alone on
  165. # its line, otherwise they're not matched here and will be taken as
  166. # inline later.
  167. # * List "b" is made of tags which are always block-level;
  168. #
  169. $block_tags_a_re = 'ins|del';
  170. $block_tags_b_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|'.
  171. 'script|noscript|form|fieldset|iframe|math';
  172. # Regular expression for the content of a block tag.
  173. $nested_tags_level = 4;
  174. $attr = '
  175. (?> # optional tag attributes
  176. \s # starts with whitespace
  177. (?>
  178. [^>"/]+ # text outside quotes
  179. |
  180. /+(?!>) # slash not followed by ">"
  181. |
  182. "[^"]*" # text inside double quotes (tolerate ">")
  183. |
  184. \'[^\']*\' # text inside single quotes (tolerate ">")
  185. )*
  186. )?
  187. ';
  188. $content =
  189. str_repeat('
  190. (?>
  191. [^<]+ # content without tag
  192. |
  193. <\2 # nested opening tag
  194. '.$attr.' # attributes
  195. (?>
  196. />
  197. |
  198. >', $nested_tags_level). # end of opening tag
  199. '.*?'. # last level nested tag content
  200. str_repeat('
  201. </\2\s*> # closing nested tag
  202. )
  203. |
  204. <(?!/\2\s*> # other tags with a different name
  205. )
  206. )*',
  207. $nested_tags_level);
  208. $content2 = str_replace('\2', '\3', $content);
  209. # First, look for nested blocks, e.g.:
  210. # <div>
  211. # <div>
  212. # tags for inner block must be indented.
  213. # </div>
  214. # </div>
  215. #
  216. # The outermost tags must start at the left margin for this to match, and
  217. # the inner nested divs must be indented.
  218. # We need to do this before the next, more liberal match, because the next
  219. # match will start at the first `<div>` and stop at the first `</div>`.
  220. $text = preg_replace_callback('{(?>
  221. (?>
  222. (?<=\n\n) # Starting after a blank line
  223. | # or
  224. \A\n? # the beginning of the doc
  225. )
  226. ( # save in $1
  227. # Match from `\n<tag>` to `</tag>\n`, handling nested tags
  228. # in between.
  229. [ ]{0,'.$less_than_tab.'}
  230. <('.$block_tags_b_re.')# start tag = $2
  231. '.$attr.'> # attributes followed by > and \n
  232. '.$content.' # content, support nesting
  233. </\2> # the matching end tag
  234. [ ]* # trailing spaces/tabs
  235. (?=\n+|\Z) # followed by a newline or end of document
  236. | # Special version for tags of group a.
  237. [ ]{0,'.$less_than_tab.'}
  238. <('.$block_tags_a_re.')# start tag = $3
  239. '.$attr.'>[ ]*\n # attributes followed by >
  240. '.$content2.' # content, support nesting
  241. </\3> # the matching end tag
  242. [ ]* # trailing spaces/tabs
  243. (?=\n+|\Z) # followed by a newline or end of document
  244. | # Special case just for <hr />. It was easier to make a special
  245. # case than to make the other regex more complicated.
  246. [ ]{0,'.$less_than_tab.'}
  247. <(hr) # start tag = $2
  248. '.$attr.' # attributes
  249. /?> # the matching end tag
  250. [ ]*
  251. (?=\n{2,}|\Z) # followed by a blank line or end of document
  252. | # Special case for standalone HTML comments:
  253. [ ]{0,'.$less_than_tab.'}
  254. (?s:
  255. <!-- .*? -->
  256. )
  257. [ ]*
  258. (?=\n{2,}|\Z) # followed by a blank line or end of document
  259. | # PHP and ASP-style processor instructions (<? and <%)
  260. [ ]{0,'.$less_than_tab.'}
  261. (?s:
  262. <([?%]) # $2
  263. .*?
  264. \2>
  265. )
  266. [ ]*
  267. (?=\n{2,}|\Z) # followed by a blank line or end of document
  268. )
  269. )}Sxmi',
  270. array(&$this, '_hashHTMLBlocks_callback'),
  271. $text);
  272. return $text;
  273. }
  274. public function _hashHTMLBlocks_callback($matches) {
  275. $text = $matches[1];
  276. $key = $this->hashBlock($text);
  277. return "\n\n$key\n\n";
  278. }
  279. public function hashPart($text, $boundary = 'X') {
  280. #
  281. # Called whenever a tag must be hashed when a function insert an atomic
  282. # element in the text stream. Passing $text to through this function gives
  283. # a unique text-token which will be reverted back when calling unhash.
  284. #
  285. # The $boundary argument specify what character should be used to surround
  286. # the token. By convension, "B" is used for block elements that needs not
  287. # to be wrapped into paragraph tags at the end, ":" is used for elements
  288. # that are word separators and "X" is used in the general case.
  289. #
  290. # Swap back any tag hash found in $text so we do not have to `unhash`
  291. # multiple times at the end.
  292. $text = $this->unhash($text);
  293. # Then hash the block.
  294. static $i = 0;
  295. $key = "$boundary\x1A" . ++$i . $boundary;
  296. $this->html_hashes[$key] = $text;
  297. return $key; # String that will replace the tag.
  298. }
  299. public function hashBlock($text) {
  300. #
  301. # Shortcut function for hashPart with block-level boundaries.
  302. #
  303. return $this->hashPart($text, 'B');
  304. }
  305. public $block_gamut = array(
  306. #
  307. # These are all the transformations that form block-level
  308. # tags like paragraphs, headers, and list items.
  309. #
  310. "doHeaders" => 10,
  311. "doHorizontalRules" => 20,
  312. "doLists" => 40,
  313. "doCodeBlocks" => 50,
  314. "doBlockQuotes" => 60,
  315. );
  316. public function runBlockGamut($text) {
  317. #
  318. # Run block gamut tranformations.
  319. #
  320. # We need to escape raw HTML in Markdown source before doing anything
  321. # else. This need to be done for each block, and not only at the
  322. # begining in the Markdown function since hashed blocks can be part of
  323. # list items and could have been indented. Indented blocks would have
  324. # been seen as a code block in a previous pass of hashHTMLBlocks.
  325. $text = $this->hashHTMLBlocks($text);
  326. return $this->runBasicBlockGamut($text);
  327. }
  328. public function runBasicBlockGamut($text) {
  329. #
  330. # Run block gamut tranformations, without hashing HTML blocks. This is
  331. # useful when HTML blocks are known to be already hashed, like in the first
  332. # whole-document pass.
  333. #
  334. foreach ($this->block_gamut as $method => $priority) {
  335. $text = $this->$method($text);
  336. }
  337. # Finally form paragraph and restore hashed blocks.
  338. $text = $this->formParagraphs($text);
  339. return $text;
  340. }
  341. public function doHorizontalRules($text) {
  342. # Do Horizontal Rules:
  343. return preg_replace(
  344. '{
  345. ^[ ]{0,3} # Leading space
  346. ([-*_]) # $1: First marker
  347. (?> # Repeated marker group
  348. [ ]{0,2} # Zero, one, or two spaces.
  349. \1 # Marker character
  350. ){2,} # Group repeated at least twice
  351. [ ]* # Tailing spaces
  352. $ # End of line.
  353. }mx',
  354. "\n".$this->hashBlock("<hr$this->empty_element_suffix")."\n",
  355. $text);
  356. }
  357. public $span_gamut = array(
  358. #
  359. # These are all the transformations that occur *within* block-level
  360. # tags like paragraphs, headers, and list items.
  361. #
  362. # Process character escapes, code spans, and inline HTML
  363. # in one shot.
  364. "parseSpan" => -30,
  365. # Process anchor and image tags. Images must come first,
  366. # because ![foo][f] looks like an anchor.
  367. "doImages" => 10,
  368. "doAnchors" => 20,
  369. # Make links out of things like `<http://example.com/>`
  370. # Must come after doAnchors, because you can use < and >
  371. # delimiters in inline links like [this](<url>).
  372. "doAutoLinks" => 30,
  373. "encodeAmpsAndAngles" => 40,
  374. "doItalicsAndBold" => 50,
  375. "doHardBreaks" => 60,
  376. );
  377. public function runSpanGamut($text) {
  378. #
  379. # Run span gamut tranformations.
  380. #
  381. foreach ($this->span_gamut as $method => $priority) {
  382. $text = $this->$method($text);
  383. }
  384. return $text;
  385. }
  386. public function doHardBreaks($text) {
  387. # Do hard breaks:
  388. return preg_replace_callback('/ {2,}\n/',
  389. array(&$this, '_doHardBreaks_callback'), $text);
  390. }
  391. public function _doHardBreaks_callback($matches) {
  392. return $this->hashPart("<br$this->empty_element_suffix\n");
  393. }
  394. public function doAnchors($text) {
  395. #
  396. # Turn Markdown link shortcuts into XHTML <a> tags.
  397. #
  398. if ($this->in_anchor) return $text;
  399. $this->in_anchor = true;
  400. #
  401. # First, handle reference-style links: [link text] [id]
  402. #
  403. $text = preg_replace_callback('{
  404. ( # wrap whole match in $1
  405. \[
  406. ('.$this->nested_brackets_re.') # link text = $2
  407. \]
  408. [ ]? # one optional space
  409. (?:\n[ ]*)? # one optional newline followed by spaces
  410. \[
  411. (.*?) # id = $3
  412. \]
  413. )
  414. }xs',
  415. array(&$this, '_doAnchors_reference_callback'), $text);
  416. #
  417. # Next, inline-style links: [link text](url "optional title")
  418. #
  419. $text = preg_replace_callback('{
  420. ( # wrap whole match in $1
  421. \[
  422. ('.$this->nested_brackets_re.') # link text = $2
  423. \]
  424. \( # literal paren
  425. [ \n]*
  426. (?:
  427. <(.+?)> # href = $3
  428. |
  429. ('.$this->nested_url_parenthesis_re.') # href = $4
  430. )
  431. [ \n]*
  432. ( # $5
  433. ([\'"]) # quote char = $6
  434. (.*?) # Title = $7
  435. \6 # matching quote
  436. [ \n]* # ignore any spaces/tabs between closing quote and )
  437. )? # title is optional
  438. \)
  439. )
  440. }xs',
  441. array(&$this, '_doAnchors_inline_callback'), $text);
  442. #
  443. # Last, handle reference-style shortcuts: [link text]
  444. # These must come last in case you've also got [link text][1]
  445. # or [link text](/foo)
  446. #
  447. $text = preg_replace_callback('{
  448. ( # wrap whole match in $1
  449. \[
  450. ([^\[\]]+) # link text = $2; can\'t contain [ or ]
  451. \]
  452. )
  453. }xs',
  454. array(&$this, '_doAnchors_reference_callback'), $text);
  455. $this->in_anchor = false;
  456. return $text;
  457. }
  458. public function _doAnchors_reference_callback($matches) {
  459. $whole_match = $matches[1];
  460. $link_text = $matches[2];
  461. $link_id =& $matches[3];
  462. if ($link_id == "") {
  463. # for shortcut links like [this][] or [this].
  464. $link_id = $link_text;
  465. }
  466. # lower-case and turn embedded newlines into spaces
  467. $link_id = strtolower($link_id);
  468. $link_id = preg_replace('{[ ]?\n}', ' ', $link_id);
  469. if (isset($this->urls[$link_id])) {
  470. $url = $this->urls[$link_id];
  471. $url = $this->encodeAttribute($url);
  472. $result = "<a href=\"$url\"";
  473. if ( isset( $this->titles[$link_id] ) ) {
  474. $title = $this->titles[$link_id];
  475. $title = $this->encodeAttribute($title);
  476. $result .= " title=\"$title\"";
  477. }
  478. $link_text = $this->runSpanGamut($link_text);
  479. $result .= ">$link_text</a>";
  480. $result = $this->hashPart($result);
  481. }
  482. else {
  483. $result = $whole_match;
  484. }
  485. return $result;
  486. }
  487. public function _doAnchors_inline_callback($matches) {
  488. $whole_match = $matches[1];
  489. $link_text = $this->runSpanGamut($matches[2]);
  490. $url = $matches[3] == '' ? $matches[4] : $matches[3];
  491. $title =& $matches[7];
  492. $url = $this->encodeAttribute($url);
  493. $result = "<a href=\"$url\"";
  494. if (isset($title)) {
  495. $title = $this->encodeAttribute($title);
  496. $result .= " title=\"$title\"";
  497. }
  498. $link_text = $this->runSpanGamut($link_text);
  499. $result .= ">$link_text</a>";
  500. return $this->hashPart($result);
  501. }
  502. public function doImages($text) {
  503. #
  504. # Turn Markdown image shortcuts into <img> tags.
  505. #
  506. #
  507. # First, handle reference-style labeled images: ![alt text][id]
  508. #
  509. $text = preg_replace_callback('{
  510. ( # wrap whole match in $1
  511. !\[
  512. ('.$this->nested_brackets_re.') # alt text = $2
  513. \]
  514. [ ]? # one optional space
  515. (?:\n[ ]*)? # one optional newline followed by spaces
  516. \[
  517. (.*?) # id = $3
  518. \]
  519. )
  520. }xs',
  521. array(&$this, '_doImages_reference_callback'), $text);
  522. #
  523. # Next, handle inline images: ![alt text](url "optional title")
  524. # Don't forget: encode * and _
  525. #
  526. $text = preg_replace_callback('{
  527. ( # wrap whole match in $1
  528. !\[
  529. ('.$this->nested_brackets_re.') # alt text = $2
  530. \]
  531. \s? # One optional whitespace character
  532. \( # literal paren
  533. [ \n]*
  534. (?:
  535. <(\S*)> # src url = $3
  536. |
  537. ('.$this->nested_url_parenthesis_re.') # src url = $4
  538. )
  539. [ \n]*
  540. ( # $5
  541. ([\'"]) # quote char = $6
  542. (.*?) # title = $7
  543. \6 # matching quote
  544. [ \n]*
  545. )? # title is optional
  546. \)
  547. )
  548. }xs',
  549. array(&$this, '_doImages_inline_callback'), $text);
  550. return $text;
  551. }
  552. public function _doImages_reference_callback($matches) {
  553. $whole_match = $matches[1];
  554. $alt_text = $matches[2];
  555. $link_id = strtolower($matches[3]);
  556. if ($link_id == "") {
  557. $link_id = strtolower($alt_text); # for shortcut links like ![this][].
  558. }
  559. $alt_text = $this->encodeAttribute($alt_text);
  560. if (isset($this->urls[$link_id])) {
  561. $url = $this->encodeAttribute($this->urls[$link_id]);
  562. $result = "<img src=\"$url\" alt=\"$alt_text\"";
  563. if (isset($this->titles[$link_id])) {
  564. $title = $this->titles[$link_id];
  565. $title = $this->encodeAttribute($title);
  566. $result .= " title=\"$title\"";
  567. }
  568. $result .= $this->empty_element_suffix;
  569. $result = $this->hashPart($result);
  570. }
  571. else {
  572. # If there's no such link ID, leave intact:
  573. $result = $whole_match;
  574. }
  575. return $result;
  576. }
  577. public function _doImages_inline_callback($matches) {
  578. $whole_match = $matches[1];
  579. $alt_text = $matches[2];
  580. $url = $matches[3] == '' ? $matches[4] : $matches[3];
  581. $title =& $matches[7];
  582. $alt_text = $this->encodeAttribute($alt_text);
  583. $url = $this->encodeAttribute($url);
  584. $result = "<img src=\"$url\" alt=\"$alt_text\"";
  585. if (isset($title)) {
  586. $title = $this->encodeAttribute($title);
  587. $result .= " title=\"$title\""; # $title already quoted
  588. }
  589. $result .= $this->empty_element_suffix;
  590. return $this->hashPart($result);
  591. }
  592. public function doHeaders($text) {
  593. # Setext-style headers:
  594. # Header 1
  595. # ========
  596. #
  597. # Header 2
  598. # --------
  599. #
  600. $text = preg_replace_callback('{ ^(.+?)[ ]*\n(=+|-+)[ ]*\n+ }mx',
  601. array(&$this, '_doHeaders_callback_setext'), $text);
  602. # atx-style headers:
  603. # # Header 1
  604. # ## Header 2
  605. # ## Header 2 with closing hashes ##
  606. # ...
  607. # ###### Header 6
  608. #
  609. $text = preg_replace_callback('{
  610. ^(\#{1,6}) # $1 = string of #\'s
  611. [ ]*
  612. (.+?) # $2 = Header text
  613. [ ]*
  614. \#* # optional closing #\'s (not counted)
  615. \n+
  616. }xm',
  617. array(&$this, '_doHeaders_callback_atx'), $text);
  618. return $text;
  619. }
  620. public function _doHeaders_callback_setext($matches) {
  621. # Terrible hack to check we haven't found an empty list item.
  622. if ($matches[2] == '-' && preg_match('{^-(?: |$)}', $matches[1]))
  623. return $matches[0];
  624. $level = $matches[2]{0} == '=' ? 1 : 2;
  625. $block = "<h$level>".$this->runSpanGamut($matches[1])."</h$level>";
  626. return "\n" . $this->hashBlock($block) . "\n\n";
  627. }
  628. public function _doHeaders_callback_atx($matches) {
  629. $level = strlen($matches[1]);
  630. $block = "<h$level>".$this->runSpanGamut($matches[2])."</h$level>";
  631. return "\n" . $this->hashBlock($block) . "\n\n";
  632. }
  633. public function doLists($text) {
  634. #
  635. # Form HTML ordered (numbered) and unordered (bulleted) lists.
  636. #
  637. $less_than_tab = $this->tab_width - 1;
  638. # Re-usable patterns to match list item bullets and number markers:
  639. $marker_ul_re = '[*+-]';
  640. $marker_ol_re = '\d+[\.]';
  641. $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)";
  642. $markers_relist = array(
  643. $marker_ul_re => $marker_ol_re,
  644. $marker_ol_re => $marker_ul_re,
  645. );
  646. foreach ($markers_relist as $marker_re => $other_marker_re) {
  647. # Re-usable pattern to match any entirel ul or ol list:
  648. $whole_list_re = '
  649. ( # $1 = whole list
  650. ( # $2
  651. ([ ]{0,'.$less_than_tab.'}) # $3 = number of spaces
  652. ('.$marker_re.') # $4 = first list item marker
  653. [ ]+
  654. )
  655. (?s:.+?)
  656. ( # $5
  657. \z
  658. |
  659. \n{2,}
  660. (?=\S)
  661. (?! # Negative lookahead for another list item marker
  662. [ ]*
  663. '.$marker_re.'[ ]+
  664. )
  665. |
  666. (?= # Lookahead for another kind of list
  667. \n
  668. \3 # Must have the same indentation
  669. '.$other_marker_re.'[ ]+
  670. )
  671. )
  672. )
  673. '; // mx
  674. # We use a different prefix before nested lists than top-level lists.
  675. # See extended comment in _ProcessListItems().
  676. if ($this->list_level) {
  677. $text = preg_replace_callback('{
  678. ^
  679. '.$whole_list_re.'
  680. }mx',
  681. array(&$this, '_doLists_callback'), $text);
  682. }
  683. else {
  684. $text = preg_replace_callback('{
  685. (?:(?<=\n)\n|\A\n?) # Must eat the newline
  686. '.$whole_list_re.'
  687. }mx',
  688. array(&$this, '_doLists_callback'), $text);
  689. }
  690. }
  691. return $text;
  692. }
  693. public function _doLists_callback($matches) {
  694. # Re-usable patterns to match list item bullets and number markers:
  695. $marker_ul_re = '[*+-]';
  696. $marker_ol_re = '\d+[\.]';
  697. $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)";
  698. $list = $matches[1];
  699. $list_type = preg_match("/$marker_ul_re/", $matches[4]) ? "ul" : "ol";
  700. $marker_any_re = ( $list_type == "ul" ? $marker_ul_re : $marker_ol_re );
  701. $list .= "\n";
  702. $result = $this->processListItems($list, $marker_any_re);
  703. $result = $this->hashBlock("<$list_type>\n" . $result . "</$list_type>");
  704. return "\n". $result ."\n\n";
  705. }
  706. var $list_level = 0;
  707. public function processListItems($list_str, $marker_any_re) {
  708. #
  709. # Process the contents of a single ordered or unordered list, splitting it
  710. # into individual list items.
  711. #
  712. # The $this->list_level global keeps track of when we're inside a list.
  713. # Each time we enter a list, we increment it; when we leave a list,
  714. # we decrement. If it's zero, we're not in a list anymore.
  715. #
  716. # We do this because when we're not inside a list, we want to treat
  717. # something like this:
  718. #
  719. # I recommend upgrading to version
  720. # 8. Oops, now this line is treated
  721. # as a sub-list.
  722. #
  723. # As a single paragraph, despite the fact that the second line starts
  724. # with a digit-period-space sequence.
  725. #
  726. # Whereas when we're inside a list (or sub-list), that line will be
  727. # treated as the start of a sub-list. What a kludge, huh? This is
  728. # an aspect of Markdown's syntax that's hard to parse perfectly
  729. # without resorting to mind-reading. Perhaps the solution is to
  730. # change the syntax rules such that sub-lists must start with a
  731. # starting cardinal number; e.g. "1." or "a.".
  732. $this->list_level++;
  733. # trim trailing blank lines:
  734. $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str);
  735. $list_str = preg_replace_callback('{
  736. (\n)? # leading line = $1
  737. (^[ ]*) # leading whitespace = $2
  738. ('.$marker_any_re.' # list marker and space = $3
  739. (?:[ ]+|(?=\n)) # space only required if item is not empty
  740. )
  741. ((?s:.*?)) # list item text = $4
  742. (?:(\n+(?=\n))|\n) # tailing blank line = $5
  743. (?= \n* (\z | \2 ('.$marker_any_re.') (?:[ ]+|(?=\n))))
  744. }xm',
  745. array(&$this, '_processListItems_callback'), $list_str);
  746. $this->list_level--;
  747. return $list_str;
  748. }
  749. public function _processListItems_callback($matches) {
  750. $item = $matches[4];
  751. $leading_line =& $matches[1];
  752. $leading_space =& $matches[2];
  753. $marker_space = $matches[3];
  754. $tailing_blank_line =& $matches[5];
  755. if ($leading_line || $tailing_blank_line ||
  756. preg_match('/\n{2,}/', $item))
  757. {
  758. # Replace marker with the appropriate whitespace indentation
  759. $item = $leading_space . str_repeat(' ', strlen($marker_space)) . $item;
  760. $item = $this->runBlockGamut($this->outdent($item)."\n");
  761. }
  762. else {
  763. # Recursion for sub-lists:
  764. $item = $this->doLists($this->outdent($item));
  765. $item = preg_replace('/\n+$/', '', $item);
  766. $item = $this->runSpanGamut($item);
  767. }
  768. return "<li>" . $item . "</li>\n";
  769. }
  770. public function doCodeBlocks($text) {
  771. #
  772. # Process Markdown `<pre><code>` blocks.
  773. #
  774. $text = preg_replace_callback('{
  775. (?:\n\n|\A\n?)
  776. ( # $1 = the code block -- one or more lines, starting with a space/tab
  777. (?>
  778. [ ]{'.$this->tab_width.'} # Lines must start with a tab or a tab-width of spaces
  779. .*\n+
  780. )+
  781. )
  782. ((?=^[ ]{0,'.$this->tab_width.'}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
  783. }xm',
  784. array(&$this, '_doCodeBlocks_callback'), $text);
  785. return $text;
  786. }
  787. public function _doCodeBlocks_callback($matches) {
  788. $codeblock = $matches[1];
  789. $codeblock = $this->outdent($codeblock);
  790. $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
  791. # trim leading newlines and trailing newlines
  792. $codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
  793. $codeblock = "<pre><code>$codeblock\n</code></pre>";
  794. return "\n\n".$this->hashBlock($codeblock)."\n\n";
  795. }
  796. public function makeCodeSpan($code) {
  797. #
  798. # Create a code span markup for $code. Called from handleSpanToken.
  799. #
  800. $code = htmlspecialchars(trim($code), ENT_NOQUOTES);
  801. return $this->hashPart("<code>$code</code>");
  802. }
  803. public $em_relist = array(
  804. '' => '(?:(?<!\*)\*(?!\*)|(?<!_)_(?!_))(?=\S|$)(?![\.,:;]\s)',
  805. '*' => '(?<=\S|^)(?<!\*)\*(?!\*)',
  806. '_' => '(?<=\S|^)(?<!_)_(?!_)',
  807. );
  808. public $strong_relist = array(
  809. '' => '(?:(?<!\*)\*\*(?!\*)|(?<!_)__(?!_))(?=\S|$)(?![\.,:;]\s)',
  810. '**' => '(?<=\S|^)(?<!\*)\*\*(?!\*)',
  811. '__' => '(?<=\S|^)(?<!_)__(?!_)',
  812. );
  813. public $em_strong_relist = array(
  814. '' => '(?:(?<!\*)\*\*\*(?!\*)|(?<!_)___(?!_))(?=\S|$)(?![\.,:;]\s)',
  815. '***' => '(?<=\S|^)(?<!\*)\*\*\*(?!\*)',
  816. '___' => '(?<=\S|^)(?<!_)___(?!_)',
  817. );
  818. public $em_strong_prepared_relist;
  819. public function prepareItalicsAndBold() {
  820. #
  821. # Prepare regular expressions for searching emphasis tokens in any
  822. # context.
  823. #
  824. foreach ($this->em_relist as $em => $em_re) {
  825. foreach ($this->strong_relist as $strong => $strong_re) {
  826. # Construct list of allowed token expressions.
  827. $token_relist = array();
  828. if (isset($this->em_strong_relist["$em$strong"])) {
  829. $token_relist[] = $this->em_strong_relist["$em$strong"];
  830. }
  831. $token_relist[] = $em_re;
  832. $token_relist[] = $strong_re;
  833. # Construct master expression from list.
  834. $token_re = '{('. implode('|', $token_relist) .')}';
  835. $this->em_strong_prepared_relist["$em$strong"] = $token_re;
  836. }
  837. }
  838. }
  839. public function doItalicsAndBold($text) {
  840. $token_stack = array('');
  841. $text_stack = array('');
  842. $em = '';
  843. $strong = '';
  844. $tree_char_em = false;
  845. while (1) {
  846. #
  847. # Get prepared regular expression for seraching emphasis tokens
  848. # in current context.
  849. #
  850. $token_re = $this->em_strong_prepared_relist["$em$strong"];
  851. #
  852. # Each loop iteration search for the next emphasis token.
  853. # Each token is then passed to handleSpanToken.
  854. #
  855. $parts = preg_split($token_re, $text, 2, PREG_SPLIT_DELIM_CAPTURE);
  856. $text_stack[0] .= $parts[0];
  857. $token =& $parts[1];
  858. $text =& $parts[2];
  859. if (empty($token)) {
  860. # Reached end of text span: empty stack without emitting.
  861. # any more emphasis.
  862. while ($token_stack[0]) {
  863. $text_stack[1] .= array_shift($token_stack);
  864. $text_stack[0] .= array_shift($text_stack);
  865. }
  866. break;
  867. }
  868. $token_len = strlen($token);
  869. if ($tree_char_em) {
  870. # Reached closing marker while inside a three-char emphasis.
  871. if ($token_len == 3) {
  872. # Three-char closing marker, close em and strong.
  873. array_shift($token_stack);
  874. $span = array_shift($text_stack);
  875. $span = $this->runSpanGamut($span);
  876. $span = "<strong><em>$span</em></strong>";
  877. $text_stack[0] .= $this->hashPart($span);
  878. $em = '';
  879. $strong = '';
  880. } else {
  881. # Other closing marker: close one em or strong and
  882. # change current token state to match the other
  883. $token_stack[0] = str_repeat($token{0}, 3-$token_len);
  884. $tag = $token_len == 2 ? "strong" : "em";
  885. $span = $text_stack[0];
  886. $span = $this->runSpanGamut($span);
  887. $span = "<$tag>$span</$tag>";
  888. $text_stack[0] = $this->hashPart($span);
  889. $$tag = ''; # $$tag stands for $em or $strong
  890. }
  891. $tree_char_em = false;
  892. } else if ($token_len == 3) {
  893. if ($em) {
  894. # Reached closing marker for both em and strong.
  895. # Closing strong marker:
  896. for ($i = 0; $i < 2; ++$i) {
  897. $shifted_token = array_shift($token_stack);
  898. $tag = strlen($shifted_token) == 2 ? "strong" : "em";
  899. $span = array_shift($text_stack);
  900. $span = $this->runSpanGamut($span);
  901. $span = "<$tag>$span</$tag>";
  902. $text_stack[0] .= $this->hashPart($span);
  903. $$tag = ''; # $$tag stands for $em or $strong
  904. }
  905. } else {
  906. # Reached opening three-char emphasis marker. Push on token
  907. # stack; will be handled by the special condition above.
  908. $em = $token{0};
  909. $strong = "$em$em";
  910. array_unshift($token_stack, $token);
  911. array_unshift($text_stack, '');
  912. $tree_char_em = true;
  913. }
  914. } else if ($token_len == 2) {
  915. if ($strong) {
  916. # Unwind any dangling emphasis marker:
  917. if (strlen($token_stack[0]) == 1) {
  918. $text_stack[1] .= array_shift($token_stack);
  919. $text_stack[0] .= array_shift($text_stack);
  920. }
  921. # Closing strong marker:
  922. array_shift($token_stack);
  923. $span = array_shift($text_stack);
  924. $span = $this->runSpanGamut($span);
  925. $span = "<strong>$span</strong>";
  926. $text_stack[0] .= $this->hashPart($span);
  927. $strong = '';
  928. } else {
  929. array_unshift($token_stack, $token);
  930. array_unshift($text_stack, '');
  931. $strong = $token;
  932. }
  933. } else {
  934. # Here $token_len == 1
  935. if ($em) {
  936. if (strlen($token_stack[0]) == 1) {
  937. # Closing emphasis marker:
  938. array_shift($token_stack);
  939. $span = array_shift($text_stack);
  940. $span = $this->runSpanGamut($span);
  941. $span = "<em>$span</em>";
  942. $text_stack[0] .= $this->hashPart($span);
  943. $em = '';
  944. } else {
  945. $text_stack[0] .= $token;
  946. }
  947. } else {
  948. array_unshift($token_stack, $token);
  949. array_unshift($text_stack, '');
  950. $em = $token;
  951. }
  952. }
  953. }
  954. return $text_stack[0];
  955. }
  956. public function doBlockQuotes($text) {
  957. $text = preg_replace_callback('/
  958. ( # Wrap whole match in $1
  959. (?>
  960. ^[ ]*>[ ]? # ">" at the start of a line
  961. .+\n # rest of the first line
  962. (.+\n)* # subsequent consecutive lines
  963. \n* # blanks
  964. )+
  965. )
  966. /xm',
  967. array(&$this, '_doBlockQuotes_callback'), $text);
  968. return $text;
  969. }
  970. public function _doBlockQuotes_callback($matches) {
  971. $bq = $matches[1];
  972. # trim one level of quoting - trim whitespace-only lines
  973. $bq = preg_replace('/^[ ]*>[ ]?|^[ ]+$/m', '', $bq);
  974. $bq = $this->runBlockGamut($bq); # recurse
  975. $bq = preg_replace('/^/m', " ", $bq);
  976. # These leading spaces cause problem with <pre> content,
  977. # so we need to fix that:
  978. $bq = preg_replace_callback('{(\s*<pre>.+?</pre>)}sx',
  979. array(&$this, '_doBlockQuotes_callback2'), $bq);
  980. return "\n". $this->hashBlock("<blockquote>\n$bq\n</blockquote>")."\n\n";
  981. }
  982. public function _doBlockQuotes_callback2($matches) {
  983. $pre = $matches[1];
  984. $pre = preg_replace('/^ /m', '', $pre);
  985. return $pre;
  986. }
  987. public function formParagraphs($text) {
  988. #
  989. # Params:
  990. # $text - string to process with html <p> tags
  991. #
  992. # Strip leading and trailing lines:
  993. $text = preg_replace('/\A\n+|\n+\z/', '', $text);
  994. $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY);
  995. #
  996. # Wrap <p> tags and unhashify HTML blocks
  997. #
  998. foreach ($grafs as $key => $value) {
  999. if (!preg_match('/^B\x1A[0-9]+B$/', $value)) {
  1000. # Is a paragraph.
  1001. $value = $this->runSpanGamut($value);
  1002. $value = preg_replace('/^([ ]*)/', "<p>", $value);
  1003. $value .= "</p>";
  1004. $grafs[$key] = $this->unhash($value);
  1005. }
  1006. else {
  1007. # Is a block.
  1008. # Modify elements of @grafs in-place...
  1009. $graf = $value;
  1010. $block = $this->html_hashes[$graf];
  1011. $graf = $block;
  1012. // if (preg_match('{
  1013. // \A
  1014. // ( # $1 = <div> tag
  1015. // <div \s+
  1016. // [^>]*
  1017. // \b
  1018. // markdown\s*=\s* ([\'"]) # $2 = attr quote char
  1019. // 1
  1020. // \2
  1021. // [^>]*
  1022. // >
  1023. // )
  1024. // ( # $3 = contents
  1025. // .*
  1026. // )
  1027. // (</div>) # $4 = closing tag
  1028. // \z
  1029. // }xs', $block, $matches))
  1030. // {
  1031. // list(, $div_open, , $div_content, $div_close) = $matches;
  1032. //
  1033. // # We can't call Markdown(), because that resets the hash;
  1034. // # that initialization code should be pulled into its own sub, though.
  1035. // $div_content = $this->hashHTMLBlocks($div_content);
  1036. //
  1037. // # Run document gamut methods on the content.
  1038. // foreach ($this->document_gamut as $method => $priority) {
  1039. // $div_content = $this->$method($div_content);
  1040. // }
  1041. //
  1042. // $div_open = preg_replace(
  1043. // '{\smarkdown\s*=\s*([\'"]).+?\1}', '', $div_open);
  1044. //
  1045. // $graf = $div_open . "\n" . $div_content . "\n" . $div_close;
  1046. // }
  1047. $grafs[$key] = $graf;
  1048. }
  1049. }
  1050. return implode("\n\n", $grafs);
  1051. }
  1052. public function encodeAttribute($text) {
  1053. #
  1054. # Encode text for a double-quoted HTML attribute. This function
  1055. # is *not* suitable for attributes enclosed in single quotes.
  1056. #
  1057. $text = $this->encodeAmpsAndAngles($text);
  1058. $text = str_replace('"', '&quot;', $text);
  1059. return $text;
  1060. }
  1061. public function encodeAmpsAndAngles($text) {
  1062. #
  1063. # Smart processing for ampersands and angle brackets that need to
  1064. # be encoded. Valid character entities are left alone unless the
  1065. # no-entities mode is set.
  1066. #
  1067. if ($this->no_entities) {
  1068. $text = str_replace('&', '&amp;', $text);
  1069. } else {
  1070. # Ampersand-encoding based entirely on Nat Irons's Amputator
  1071. # MT plugin: <http://bumppo.net/projects/amputator/>
  1072. $text = preg_replace('/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/',
  1073. '&amp;', $text);;
  1074. }
  1075. # Encode remaining <'s
  1076. $text = str_replace('<', '&lt;', $text);
  1077. return $text;
  1078. }
  1079. public function doAutoLinks($text) {
  1080. $text = preg_replace_callback('{<((https?|ftp|dict):[^\'">\s]+)>}i',
  1081. array(&$this, '_doAutoLinks_url_callback'), $text);
  1082. # Email addresses: <address@domain.foo>
  1083. $text = preg_replace_callback('{
  1084. <
  1085. (?:mailto:)?
  1086. (
  1087. (?:
  1088. [-!#$%&\'*+/=?^_`.{|}~\w\x80-\xFF]+
  1089. |
  1090. ".*?"
  1091. )
  1092. \@
  1093. (?:
  1094. [-a-z0-9\x80-\xFF]+(\.[-a-z0-9\x80-\xFF]+)*\.[a-z]+
  1095. |
  1096. \[[\d.a-fA-F:]+\] # IPv4 & IPv6
  1097. )
  1098. )
  1099. >
  1100. }xi',
  1101. array(&$this, '_doAutoLinks_email_callback'), $text);
  1102. return $text;
  1103. }
  1104. public function _doAutoLinks_url_callback($matches) {
  1105. $url = $this->encodeAttribute($matches[1]);
  1106. $link = "<a href=\"$url\">$url</a>";
  1107. return $this->hashPart($link);
  1108. }
  1109. public function _doAutoLinks_email_callback($matches) {
  1110. $address = $matches[1];
  1111. $link = $this->encodeEmailAddress($address);
  1112. return $this->hashPart($link);
  1113. }
  1114. public function encodeEmailAddress($addr) {
  1115. #
  1116. # Input: an email address, e.g. "foo@example.com"
  1117. #
  1118. # Output: the email address as a mailto link, with each character
  1119. # of the address encoded as either a decimal or hex entity, in
  1120. # the hopes of foiling most address harvesting spam bots. E.g.:
  1121. #
  1122. # <p><a href="&#109;&#x61;&#105;&#x6c;&#116;&#x6f;&#58;&#x66;o&#111;
  1123. # &#x40;&#101;&#x78;&#97;&#x6d;&#112;&#x6c;&#101;&#46;&#x63;&#111;
  1124. # &#x6d;">&#x66;o&#111;&#x40;&#101;&#x78;&#97;&#x6d;&#112;&#x6c;
  1125. # &#101;&#46;&#x63;&#111;&#x6d;</a></p>
  1126. #
  1127. # Based by a filter by Matthew Wickline, posted to BBEdit-Talk.
  1128. # With some optimizations by Milian Wolff.
  1129. #
  1130. $addr = "mailto:" . $addr;
  1131. $chars = preg_split('/(?<!^)(?!$)/', $addr);
  1132. $seed = (int)abs(crc32($addr) / strlen($addr)); # Deterministic seed.
  1133. foreach ($chars as $key => $char) {
  1134. $ord = ord($char);
  1135. # Ignore non-ascii chars.
  1136. if ($ord < 128) {
  1137. $r = ($seed * (1 + $key)) % 100; # Pseudo-random function.
  1138. # roughly 10% raw, 45% hex, 45% dec
  1139. # '@' *must* be encoded. I insist.
  1140. if ($r > 90 && $char != '@') /* do nothing */;
  1141. else if ($r < 45) $chars[$key] = '&#x'.dechex($ord).';';
  1142. else $chars[$key] = '&#'.$ord.';';
  1143. }
  1144. }
  1145. $addr = implode('', $chars);
  1146. $text = implode('', array_slice($chars, 7)); # text without `mailto:`
  1147. $addr = "<a href=\"$addr\">$text</a>";
  1148. return $addr;
  1149. }
  1150. public function parseSpan($str) {
  1151. #
  1152. # Take the string $str and parse it into tokens, hashing embeded HTML,
  1153. # escaped characters and handling code spans.
  1154. #
  1155. $output = '';
  1156. $span_re = '{
  1157. (
  1158. \\\\'.$this->escape_chars_re.'
  1159. |
  1160. (?<![`\\\\])
  1161. `+ # code span marker
  1162. '.( $this->no_markup ? '' : '
  1163. |
  1164. <!-- .*? --> # comment
  1165. |
  1166. <\?.*?\?> | <%.*?%> # processing instruction
  1167. |
  1168. <[/!$]?[-a-zA-Z0-9:_]+ # regular tags
  1169. (?>
  1170. \s
  1171. (?>[^"\'>]+|"[^"]*"|\'[^\']*\')*
  1172. )?
  1173. >
  1174. ').'
  1175. )
  1176. }xs';
  1177. while (1) {
  1178. #
  1179. # Each loop iteration seach for either the next tag, the next
  1180. # openning code span marker, or the next escaped character.
  1181. # Each token is then passed to handleSpanToken.
  1182. #
  1183. $parts = preg_split($span_re, $str, 2, PREG_SPLIT_DELIM_CAPTURE);
  1184. # Create token from text preceding tag.
  1185. if ($parts[0] != "") {
  1186. $output .= $parts[0];
  1187. }
  1188. # Check if we reach the end.
  1189. if (isset($parts[1])) {
  1190. $output .= $this->handleSpanToken($parts[1], $parts[2]);
  1191. $str = $parts[2];
  1192. }
  1193. else {
  1194. break;
  1195. }
  1196. }
  1197. return $output;
  1198. }
  1199. public function handleSpanToken($token, &$str) {
  1200. #
  1201. # Handle $token provided by parseSpan by determining its nature and
  1202. # returning the corresponding value that should replace it.
  1203. #
  1204. switch ($token{0}) {
  1205. case "\\":
  1206. return $this->hashPart("&#". ord($token{1}). ";");
  1207. case "`":
  1208. # Search for end marker in remaining text.
  1209. if (preg_match('/^(.*?[^`])'.preg_quote($token).'(?!`)(.*)$/sm',
  1210. $str, $matches))
  1211. {
  1212. $str = $matches[2];
  1213. $codespan = $this->makeCodeSpan($matches[1]);
  1214. return $this->hashPart($codespan);
  1215. }
  1216. return $token; // return as text since no ending marker found.
  1217. default:
  1218. return $this->hashPart($token);
  1219. }
  1220. }
  1221. public function outdent($text) {
  1222. #
  1223. # Remove one level of line-leading tabs or spaces
  1224. #
  1225. return preg_replace('/^(\t|[ ]{1,'.$this->tab_width.'})/m', '', $text);
  1226. }
  1227. # String length function for detab. `_initDetab` will create a function to
  1228. # hanlde UTF-8 if the default function does not exist.
  1229. public $utf8_strlen = 'mb_strlen';
  1230. public function detab($text) {
  1231. #
  1232. # Replace tabs with the appropriate amount of space.
  1233. #
  1234. # For each line we separate the line in blocks delemited by
  1235. # tab characters. Then we reconstruct every line by adding the
  1236. # appropriate number of space between each blocks.
  1237. $text = preg_replace_callback('/^.*\t.*$/m',
  1238. array(&$this, '_detab_callback'), $text);
  1239. return $text;
  1240. }
  1241. public function _detab_callback($matches) {
  1242. $line = $matches[0];
  1243. $strlen = $this->utf8_strlen; # strlen function for UTF-8.
  1244. # Split in blocks.
  1245. $blocks = explode("\t", $line);
  1246. # Add each blocks to the line.
  1247. $line = $blocks[0];
  1248. unset($blocks[0]); # Do not add first block twice.
  1249. foreach ($blocks as $block) {
  1250. # Calculate amount of space, insert spaces, insert block.
  1251. $amount = $this->tab_width -
  1252. $strlen($line, 'UTF-8') % $this->tab_width;
  1253. $line .= str_repeat(" ", $amount) . $block;
  1254. }
  1255. return $line;
  1256. }
  1257. public function _initDetab() {
  1258. #
  1259. # Check for the availability of the function in the `utf8_strlen` property
  1260. # (initially `mb_strlen`). If the function is not available, create a
  1261. # function that will loosely count the number of UTF-8 characters with a
  1262. # regular expression.
  1263. #
  1264. if (function_exists($this->utf8_strlen)) return;
  1265. $this->utf8_strlen = create_function('$text', 'return preg_match_all(
  1266. "/[\\\\x00-\\\\xBF]|[\\\\xC0-\\\\xFF][\\\\x80-\\\\xBF]*/",
  1267. $text, $m);');
  1268. }
  1269. public function unhash($text) {
  1270. #
  1271. # Swap back in all the tags hashed by _HashHTMLBlocks.
  1272. #
  1273. return preg_replace_callback('/(.)\x1A[0-9]+\1/',
  1274. array(&$this, '_unhash_callback'), $text);
  1275. }
  1276. public function _unhash_callback($matches) {
  1277. return $this->html_hashes[$matches[0]];
  1278. }
  1279. }
  1280. #
  1281. # Markdown Extra Parser Class
  1282. #
  1283. class MarkdownExtra_Parser extends Markdown_Parser {
  1284. # Prefix for footnote ids.
  1285. public $fn_id_prefix = "";
  1286. # Optional title attribute for footnote links and backlinks.
  1287. public $fn_link_title = '';
  1288. public $fn_backlink_title = '';
  1289. # Optional class attribute for footnote links and backlinks.
  1290. public $fn_link_class = '';
  1291. public $fn_backlink_class = '';
  1292. # Predefined abbreviations.
  1293. public $predef_abbr = array();
  1294. public function __construct() {
  1295. #
  1296. # Constructor function. Initialize the parser object.
  1297. #
  1298. # Add extra escapable characters before parent constructor
  1299. # initialize the table.
  1300. $this->escape_chars .= ':|';
  1301. # Insert extra document, block, and span transformations.
  1302. # Parent constructor will do the sorting.
  1303. $this->document_gamut += array(
  1304. "doFencedCodeBlocks" => 5,
  1305. "stripFootnotes" => 15,
  1306. "stripAbbreviations" => 25,
  1307. "appendFootnotes" => 50,
  1308. );
  1309. $this->block_gamut += array(
  1310. "doFencedCodeBlocks" => 5,
  1311. "doTables" => 15,
  1312. "doDefLists" => 45,
  1313. );
  1314. $this->span_gamut += array(
  1315. "doFootnotes" => 5,
  1316. "doAbbreviations" => 70,
  1317. );
  1318. parent::__construct();
  1319. }
  1320. # Extra variables used during extra transformations.
  1321. public $footnotes = array();
  1322. public $footnotes_ordered = array();
  1323. public $abbr_desciptions = array();
  1324. public $abbr_word_re = '';
  1325. # Give the current footnote number.
  1326. public $footnote_counter = 1;
  1327. public function setup() {
  1328. #
  1329. # Setting up Extra-specific variables.
  1330. #
  1331. parent::setup();
  1332. $this->footnotes = array();
  1333. $this->footnotes_ordered = array();
  1334. $this->abbr_desciptions = array();
  1335. $this->abbr_word_re = '';
  1336. $this->footnote_counter = 1;
  1337. foreach ($this->predef_abbr as $abbr_word => $abbr_desc) {
  1338. if ($this->abbr_word_re)
  1339. $this->abbr_word_re .= '|';
  1340. $this->abbr_word_re .= preg_quote($abbr_word);
  1341. $this->abbr_desciptions[$abbr_word] = trim($abbr_desc);
  1342. }
  1343. }
  1344. public function teardown() {
  1345. #
  1346. # Clearing Extra-specific variables.
  1347. #
  1348. $this->footnotes = array();
  1349. $this->footnotes_ordered = array();
  1350. $this->abbr_desciptions = array();
  1351. $this->abbr_word_re = '';
  1352. parent::teardown();
  1353. }
  1354. ### HTML Block Parser ###
  1355. # Tags that are always treated as block tags:
  1356. public $block_tags_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend';
  1357. # Tags treated as block tags only if the opening tag is alone on it's line:
  1358. public $context_block_tags_re = 'script|noscript|math|ins|del';
  1359. # Tags where markdown="1" default to span mode:
  1360. public $contain_span_tags_re = 'p|h[1-6]|li|dd|dt|td|th|legend|address';
  1361. # Tags which must not have their contents modified, no matter where
  1362. # they appear:
  1363. public $clean_tags_re = 'script|math';
  1364. # Tags that do not need to be closed.
  1365. public $auto_close_tags_re = 'hr|img';
  1366. public function hashHTMLBlocks($text) {
  1367. #
  1368. # Hashify HTML Blocks and "clean tags".
  1369. #
  1370. # We only want to do this for block-level HTML tags, such as headers,
  1371. # lists, and tables. That's because we still want to wrap <p>s around
  1372. # "paragraphs" that are wrapped in non-block-level tags, such as anchors,
  1373. # phrase emphasis, and spans. The list of tags we're looking for is
  1374. # hard-coded.
  1375. #
  1376. # This works by calling _HashHTMLBlocks_InMarkdown, which then calls
  1377. # _HashHTMLBlocks_InHTML when it encounter block tags. When the markdown="1"
  1378. # attribute is found whitin a tag, _HashHTMLBlocks_InHTML calls back
  1379. # _HashHTMLBlocks_InMarkdown to handle the Markdown syntax within the tag.
  1380. # These two functions are calling each other. It's recursive!
  1381. #
  1382. #
  1383. # Call the HTML-in-Markdown hasher.
  1384. #
  1385. list($text, ) = $this->_hashHTMLBlocks_inMarkdown($text);
  1386. return $text;
  1387. }
  1388. public function _hashHTMLBlocks_inMarkdown($text, $indent = 0,
  1389. $enclosing_tag_re = '', $span = false)
  1390. {
  1391. #
  1392. # Parse markdown text, calling _HashHTMLBlocks_InHTML for block tags.
  1393. #
  1394. # * $indent is the number of space to be ignored when checking for code
  1395. # blocks. This is important because if we don't take the indent into
  1396. # account, something like this (which looks right) won't work as expected:
  1397. #
  1398. # <div>
  1399. # <div markdown="1">
  1400. # Hello World. <-- Is this a Markdown code block or text?
  1401. # </div> <-- Is this a Markdown code block or a real tag?
  1402. # <div>
  1403. #
  1404. # If you don't like this, just don't indent the tag on which
  1405. # you apply the markdown="1" attribute.
  1406. #
  1407. # * If $enclosing_tag_re is not empty, stops at the first unmatched closing
  1408. # tag with that name. Nested tags supported.
  1409. #
  1410. # * If $span is true, text inside must treated as span. So any double
  1411. # newline will be replaced by a single newline so that it does not create
  1412. # paragraphs.
  1413. #
  1414. # Returns an array of that form: ( processed text , remaining text )
  1415. #
  1416. if ($text === '') return array('', '');
  1417. # Regex to check for the presense of newlines around a block tag.
  1418. $newline_before_re = '/(?:^\n?|\n\n)*$/';
  1419. $newline_after_re =
  1420. '{
  1421. ^ # Start of text following the tag.
  1422. (?>[ ]*<!--.*?-->)? # Optional comment.
  1423. [ ]*\n # Must be followed by newline.
  1424. }xs';
  1425. # Regex to match any tag.
  1426. $block_tag_re =
  1427. '{
  1428. ( # $2: Capture hole tag.
  1429. </? # Any opening or closing tag.
  1430. (?> # Tag name.
  1431. '.$this->block_tags_re.' |
  1432. '.$this->context_block_tags_re.' |
  1433. '.$this->clean_tags_re.' |
  1434. (?!\s)'.$enclosing_tag_re.'
  1435. )
  1436. (?:
  1437. (?=[\s"\'/a-zA-Z0-9]) # Allowed characters after tag name.
  1438. (?>
  1439. ".*?" | # Double quotes (can contain `>`)
  1440. \'.*?\' | # Single quotes (can contain `>`)
  1441. .+? # Anything but quotes and `>`.
  1442. )*?
  1443. )?
  1444. > # End of tag.
  1445. |
  1446. <!-- .*? --> # HTML Comment
  1447. |
  1448. <\?.*?\?> | <%.*?%> # Processing instruction
  1449. |
  1450. <!\[CDATA\[.*?\]\]> # CData Block
  1451. |
  1452. # Code span marker
  1453. `+
  1454. '. ( !$span ? ' # If not in span.
  1455. |
  1456. # Indented code block
  1457. (?: ^[ ]*\n | ^ | \n[ ]*\n )
  1458. [ ]{'.($indent+4).'}[^\n]* \n
  1459. (?>
  1460. (?: [ ]{'.($indent+4).'}[^\n]* | [ ]* ) \n
  1461. )*
  1462. |
  1463. # Fenced code block marker
  1464. (?> ^ | \n )
  1465. [ ]{0,'.($indent).'}~~~+[ ]*\n
  1466. ' : '' ). ' # End (if not is span).
  1467. )
  1468. }xs';
  1469. $depth = 0; # Current depth inside the tag tree.
  1470. $parsed = ""; # Parsed text that will be returned.
  1471. #
  1472. # Loop through every tag until we find the closing tag of the parent
  1473. # or loop until reaching the end of text if no parent tag specified.
  1474. #
  1475. do {
  1476. #
  1477. # Split the text using the first $tag_match pattern found.
  1478. # Text before pattern will be first in the array, text after
  1479. # pattern will be at the end, and between will be any catches made
  1480. # by the pattern.
  1481. #
  1482. $parts = preg_split($block_tag_re, $text, 2,
  1483. PREG_SPLIT_DELIM_CAPTURE);
  1484. # If in Markdown span mode, add a empty-string span-level hash
  1485. # after each newline to prevent triggering any block element.
  1486. if ($span) {
  1487. $void = $this->hashPart("", ':');
  1488. $newline = "$void\n";
  1489. $parts[0] = $void . str_replace("\n", $newline, $parts[0]) . $void;
  1490. }
  1491. $parsed .= $parts[0]; # Text before current tag.
  1492. # If end of $text has been reached. Stop loop.
  1493. if (count($parts) < 3) {
  1494. $text = "";
  1495. break;
  1496. }
  1497. $tag = $parts[1]; # Tag to handle.
  1498. $text = $parts[2]; # Remaining text after current tag.
  1499. $tag_re = preg_quote($tag); # For use in a regular expression.
  1500. #
  1501. # Check for: Code span marker
  1502. #
  1503. if ($tag{0} == "`") {
  1504. # Find corresponding end marker.
  1505. $tag_re = preg_quote($tag);
  1506. if (preg_match('{^(?>.+?|\n(?!\n))*?(?<!`)'.$tag_re.'(?!`)}',
  1507. $text, $matches))
  1508. {
  1509. # End marker found: pass text unchanged until marker.
  1510. $parsed .= $tag . $matches[0];
  1511. $text = substr($text, strlen($matches[0]));
  1512. }
  1513. else {
  1514. # Unmatched marker: just skip it.
  1515. $parsed .= $tag;
  1516. }
  1517. }
  1518. #
  1519. # Check for: Fenced code block marker.
  1520. #
  1521. else if (preg_match('{^\n?[ ]{0,'.($indent+3).'}~}', $tag)) {
  1522. # Fenced code block marker: find matching end marker.
  1523. $tag_re = preg_quote(trim($tag));
  1524. if (preg_match('{^(?>.*\n)+?[ ]{0,'.($indent).'}'.$tag_re.'[ ]*\n}', $text,
  1525. $matches))
  1526. {
  1527. # End marker found: pass text unchanged until marker.
  1528. $parsed .= $tag . $matches[0];
  1529. $text = substr($text, strlen($matches[0]));
  1530. }
  1531. else {
  1532. # No end marker: just skip it.
  1533. $parsed .= $tag;
  1534. }
  1535. }
  1536. #
  1537. # Check for: Indented code block.
  1538. #
  1539. else if ($tag{0} == "\n" || $tag{0} == " ") {
  1540. # Indented code block: pass it unchanged, will be handled
  1541. # later.
  1542. $parsed .= $tag;
  1543. }
  1544. #
  1545. # Check for: Opening Block level tag or
  1546. # Opening Context Block tag (like ins and del)
  1547. # used as a block tag (tag is alone on it's line).
  1548. #
  1549. else if (preg_match('{^<(?:'.$this->block_tags_re.')\b}', $tag) ||
  1550. ( preg_match('{^<(?:'.$this->context_block_tags_re.')\b}', $tag) &&
  1551. preg_match($newline_before_re, $parsed) &&
  1552. preg_match($newline_after_re, $text) )
  1553. )
  1554. {
  1555. # Need to parse tag and following text using the HTML parser.
  1556. list($block_text, $text) =
  1557. $this->_hashHTMLBlocks_inHTML($tag . $text, "hashBlock", true);
  1558. # Make sure it stays outside of any paragraph by adding newlines.
  1559. $parsed .= "\n\n$block_text\n\n";
  1560. }
  1561. #
  1562. # Check for: Clean tag (like script, math)
  1563. # HTML Comments, processing instructions.
  1564. #
  1565. else if (preg_match('{^<(?:'.$this->clean_tags_re.')\b}', $tag) ||
  1566. $tag{1} == '!' || $tag{1} == '?')
  1567. {
  1568. # Need to parse tag and following text using the HTML parser.
  1569. # (don't check for markdown attribute)
  1570. list($block_text, $text) =
  1571. $this->_hashHTMLBlocks_inHTML($tag . $text, "hashClean", false);
  1572. $parsed .= $block_text;
  1573. }
  1574. #
  1575. # Check for: Tag with same name as enclosing tag.
  1576. #
  1577. else if ($enclosing_tag_re !== '' &&
  1578. # Same name as enclosing tag.
  1579. preg_match('{^</?(?:'.$enclosing_tag_re.')\b}', $tag))
  1580. {
  1581. #
  1582. # Increase/decrease nested tag count.
  1583. #
  1584. if ($tag{1} == '/') $depth--;
  1585. else if ($tag{strlen($tag)-2} != '/') $depth++;
  1586. if ($depth < 0) {
  1587. #
  1588. # Going out of parent element. Clean up and break so we
  1589. # return to the calling function.
  1590. #
  1591. $text = $tag . $text;
  1592. break;
  1593. }
  1594. $parsed .= $tag;
  1595. }
  1596. else {
  1597. $parsed .= $tag;
  1598. }
  1599. } while ($depth >= 0);
  1600. return array($parsed, $text);
  1601. }
  1602. public function _hashHTMLBlocks_inHTML($text, $hash_method, $md_attr) {
  1603. #
  1604. # Parse HTML, calling _HashHTMLBlocks_InMarkdown for block tags.
  1605. #
  1606. # * Calls $hash_method to convert any blocks.
  1607. # * Stops when the first opening tag closes.
  1608. # * $md_attr indicate if the use of the `markdown="1"` attribute is allowed.
  1609. # (it is not inside clean tags)
  1610. #
  1611. # Returns an array of that form: ( processed text , remaining text )
  1612. #
  1613. if ($text === '') return array('', '');
  1614. # Regex to match `markdown` attribute inside of a tag.
  1615. $markdown_attr_re = '
  1616. {
  1617. \s* # Eat whitespace before the `markdown` attribute
  1618. markdown
  1619. \s*=\s*
  1620. (?>
  1621. (["\']) # $1: quote delimiter
  1622. (.*?) # $2: attribute value
  1623. \1 # matching delimiter
  1624. |
  1625. ([^\s>]*) # $3: unquoted attribute value
  1626. )
  1627. () # $4: make $3 always defined (avoid warnings)
  1628. }xs';
  1629. # Regex to match any tag.
  1630. $tag_re = '{
  1631. ( # $2: Capture hole tag.
  1632. </? # Any opening or closing tag.
  1633. [\w:$]+ # Tag name.
  1634. (?:
  1635. (?=[\s"\'/a-zA-Z0-9]) # Allowed characters after tag name.
  1636. (?>
  1637. ".*?" | # Double quotes (can contain `>`)
  1638. \'.*?\' | # Single quotes (can contain `>`)
  1639. .+? # Anything but quotes and `>`.
  1640. )*?
  1641. )?
  1642. > # End of tag.
  1643. |
  1644. <!-- .*? --> # HTML Comment
  1645. |
  1646. <\?.*?\?> | <%.*?%> # Processing instruction
  1647. |
  1648. <!\[CDATA\[.*?\]\]> # CData Block
  1649. )
  1650. }xs';
  1651. $original_text = $text; # Save original text in case of faliure.
  1652. $depth = 0; # Current depth inside the tag tree.
  1653. $block_text = ""; # Temporary text holder for current text.
  1654. $parsed = ""; # Parsed text that will be returned.
  1655. #
  1656. # Get the name of the starting tag.
  1657. # (This pattern makes $base_tag_name_re safe without quoting.)
  1658. #
  1659. if (preg_match('/^<([\w:$]*)\b/', $text, $matches))
  1660. $base_tag_name_re = $matches[1];
  1661. #
  1662. # Loop through every tag until we find the corresponding closing tag.
  1663. #
  1664. do {
  1665. #
  1666. # Split the text using the first $tag_match pattern found.
  1667. # Text before pattern will be first in the array, text after
  1668. # pattern will be at the end, and between will be any catches made
  1669. # by the pattern.
  1670. #
  1671. $parts = preg_split($tag_re, $text, 2, PREG_SPLIT_DELIM_CAPTURE);
  1672. if (count($parts) < 3) {
  1673. #
  1674. # End of $text reached with unbalenced tag(s).
  1675. # In that case, we return original text unchanged and pass the
  1676. # first character as filtered to prevent an infinite loop in the
  1677. # parent function.
  1678. #
  1679. return array($original_text{0}, substr($original_text, 1));
  1680. }
  1681. $block_text .= $parts[0]; # Text before current tag.
  1682. $tag = $parts[1]; # Tag to handle.
  1683. $text = $parts[2]; # Remaining text after current tag.
  1684. #
  1685. # Check for: Auto-close tag (like <hr/>)
  1686. # Comments and Processing Instructions.
  1687. #
  1688. if (preg_match('{^</?(?:'.$this->auto_close_tags_re.')\b}', $tag) ||
  1689. $tag{1} == '!' || $tag{1} == '?')
  1690. {
  1691. # Just add the tag to the block as if it was text.
  1692. $block_text .= $tag;
  1693. }
  1694. else {
  1695. #
  1696. # Increase/decrease nested tag count. Only do so if
  1697. # the tag's name match base tag's.
  1698. #
  1699. if (preg_match('{^</?'.$base_tag_name_re.'\b}', $tag)) {
  1700. if ($tag{1} == '/') $depth--;
  1701. else if ($tag{strlen($tag)-2} != '/') $depth++;
  1702. }
  1703. #
  1704. # Check for `markdown="1"` attribute and handle it.
  1705. #
  1706. if ($md_attr &&
  1707. preg_match($markdown_attr_re, $tag, $attr_m) &&
  1708. preg_match('/^1|block|span$/', $attr_m[2] . $attr_m[3]))
  1709. {
  1710. # Remove `markdown` attribute from opening tag.
  1711. $tag = preg_replace($markdown_attr_re, '', $tag);
  1712. # Check if text inside this tag must be parsed in span mode.
  1713. $this->mode = $attr_m[2] . $attr_m[3];
  1714. $span_mode = $this->mode == 'span' || $this->mode != 'block' &&
  1715. preg_match('{^<(?:'.$this->contain_span_tags_re.')\b}', $tag);
  1716. # Calculate indent before tag.
  1717. if (preg_match('/(?:^|\n)( *?)(?! ).*?$/', $block_text, $matches)) {
  1718. $strlen = $this->utf8_strlen;
  1719. $indent = $strlen($matches[1], 'UTF-8');
  1720. } else {
  1721. $indent = 0;
  1722. }
  1723. # End preceding block with this tag.
  1724. $block_text .= $tag;
  1725. $parsed .= $this->$hash_method($block_text);
  1726. # Get enclosing tag name for the ParseMarkdown function.
  1727. # (This pattern makes $tag_name_re safe without quoting.)
  1728. preg_match('/^<([\w:$]*)\b/', $tag, $matches);
  1729. $tag_name_re = $matches[1];
  1730. # Parse the content using the HTML-in-Markdown parser.
  1731. list ($block_text, $text)
  1732. = $this->_hashHTMLBlocks_inMarkdown($text, $indent,
  1733. $tag_name_re, $span_mode);
  1734. # Outdent markdown text.
  1735. if ($indent > 0) {
  1736. $block_text = preg_replace("/^[ ]{1,$indent}/m", "",
  1737. $block_text);
  1738. }
  1739. # Append tag content to parsed text.
  1740. if (!$span_mode) $parsed .= "\n\n$block_text\n\n";
  1741. else $parsed .= "$block_text";
  1742. # Start over a new block.
  1743. $block_text = "";
  1744. }
  1745. else $block_text .= $tag;
  1746. }
  1747. } while ($depth > 0);
  1748. #
  1749. # Hash last block text that wasn't processed inside the loop.
  1750. #
  1751. $parsed .= $this->$hash_method($block_text);
  1752. return array($parsed, $text);
  1753. }
  1754. public function hashClean($text) {
  1755. #
  1756. # Called whenever a tag must be hashed when a function insert a "clean" tag
  1757. # in $text, it pass through this function and is automaticaly escaped,
  1758. # blocking invalid nested overlap.
  1759. #
  1760. return $this->hashPart($text, 'C');
  1761. }
  1762. public function doHeaders($text) {
  1763. #
  1764. # Redefined to add id attribute support.
  1765. #
  1766. # Setext-style headers:
  1767. # Header 1 {#header1}
  1768. # ========
  1769. #
  1770. # Header 2 {#header2}
  1771. # --------
  1772. #
  1773. $text = preg_replace_callback(
  1774. '{
  1775. (^.+?) # $1: Header text
  1776. (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? # $2: Id attribute
  1777. [ ]*\n(=+|-+)[ ]*\n+ # $3: Header footer
  1778. }mx',
  1779. array(&$this, '_doHeaders_callback_setext'), $text);
  1780. # atx-style headers:
  1781. # # Header 1 {#header1}
  1782. # ## Header 2 {#header2}
  1783. # ## Header 2 with closing hashes ## {#header3}
  1784. # ...
  1785. # ###### Header 6 {#header2}
  1786. #
  1787. $text = preg_replace_callback('{
  1788. ^(\#{1,6}) # $1 = string of #\'s
  1789. [ ]*
  1790. (.+?) # $2 = Header text
  1791. [ ]*
  1792. \#* # optional closing #\'s (not counted)
  1793. (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? # id attribute
  1794. [ ]*
  1795. \n+
  1796. }xm',
  1797. array(&$this, '_doHeaders_callback_atx'), $text);
  1798. return $text;
  1799. }
  1800. public function _doHeaders_attr($attr) {
  1801. if (empty($attr)) return "";
  1802. return " id=\"$attr\"";
  1803. }
  1804. public function _doHeaders_callback_setext($matches) {
  1805. if ($matches[3] == '-' && preg_match('{^- }', $matches[1]))
  1806. return $matches[0];
  1807. $level = $matches[3]{0} == '=' ? 1 : 2;
  1808. $attr = $this->_doHeaders_attr($id =& $matches[2]);
  1809. $block = "<h$level$attr>".$this->runSpanGamut($matches[1])."</h$level>";
  1810. return "\n" . $this->hashBlock($block) . "\n\n";
  1811. }
  1812. public function _doHeaders_callback_atx($matches) {
  1813. $level = strlen($matches[1]);
  1814. $attr = $this->_doHeaders_attr($id =& $matches[3]);
  1815. $block = "<h$level$attr>".$this->runSpanGamut($matches[2])."</h$level>";
  1816. return "\n" . $this->hashBlock($block) . "\n\n";
  1817. }
  1818. public function doTables($text) {
  1819. #
  1820. # Form HTML tables.
  1821. #
  1822. $less_than_tab = $this->tab_width - 1;
  1823. #
  1824. # Find tables with leading pipe.
  1825. #
  1826. # | Header 1 | Header 2
  1827. # | -------- | --------
  1828. # | Cell 1 | Cell 2
  1829. # | Cell 3 | Cell 4
  1830. #
  1831. $text = preg_replace_callback('
  1832. {
  1833. ^ # Start of a line
  1834. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  1835. [|] # Optional leading pipe (present)
  1836. (.+) \n # $1: Header row (at least one pipe)
  1837. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  1838. [|] ([ ]*[-:]+[-| :]*) \n # $2: Header underline
  1839. ( # $3: Cells
  1840. (?>
  1841. [ ]* # Allowed whitespace.
  1842. [|] .* \n # Row content.
  1843. )*
  1844. )
  1845. (?=\n|\Z) # Stop at final double newline.
  1846. }xm',
  1847. array(&$this, '_doTable_leadingPipe_callback'), $text);
  1848. #
  1849. # Find tables without leading pipe.
  1850. #
  1851. # Header 1 | Header 2
  1852. # -------- | --------
  1853. # Cell 1 | Cell 2
  1854. # Cell 3 | Cell 4
  1855. #
  1856. $text = preg_replace_callback('
  1857. {
  1858. ^ # Start of a line
  1859. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  1860. (\S.*[|].*) \n # $1: Header row (at least one pipe)
  1861. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  1862. ([-:]+[ ]*[|][-| :]*) \n # $2: Header underline
  1863. ( # $3: Cells
  1864. (?>
  1865. .* [|] .* \n # Row content
  1866. )*
  1867. )
  1868. (?=\n|\Z) # Stop at final double newline.
  1869. }xm',
  1870. array(&$this, '_DoTable_callback'), $text);
  1871. return $text;
  1872. }
  1873. public function _doTable_leadingPipe_callback($matches) {
  1874. $head = $matches[1];
  1875. $underline = $matches[2];
  1876. $content = $matches[3];
  1877. # Remove leading pipe for each row.
  1878. $content = preg_replace('/^ *[|]/m', '', $content);
  1879. return $this->_doTable_callback(array($matches[0], $head, $underline, $content));
  1880. }
  1881. public function _doTable_callback($matches) {
  1882. $head = $matches[1];
  1883. $underline = $matches[2];
  1884. $content = $matches[3];
  1885. # Remove any tailing pipes for each line.
  1886. $head = preg_replace('/[|] *$/m', '', $head);
  1887. $underline = preg_replace('/[|] *$/m', '', $underline);
  1888. $content = preg_replace('/[|] *$/m', '', $content);
  1889. # Reading alignement from header underline.
  1890. $separators = preg_split('/ *[|] */', $underline);
  1891. foreach ($separators as $n => $s) {
  1892. if (preg_match('/^ *-+: *$/', $s)) $attr[$n] = ' align="right"';
  1893. else if (preg_match('/^ *:-+: *$/', $s))$attr[$n] = ' align="center"';
  1894. else if (preg_match('/^ *:-+ *$/', $s)) $attr[$n] = ' align="left"';
  1895. else $attr[$n] = '';
  1896. }
  1897. # Parsing span elements, including code spans, character escapes,
  1898. # and inline HTML tags, so that pipes inside those gets ignored.
  1899. $head = $this->parseSpan($head);
  1900. $headers = preg_split('/ *[|] */', $head);
  1901. $col_count = count($headers);
  1902. # Write column headers.
  1903. $text = "<table>\n";
  1904. $text .= "<thead>\n";
  1905. $text .= "<tr>\n";
  1906. foreach ($headers as $n => $header)
  1907. $text .= " <th$attr[$n]>".$this->runSpanGamut(trim($header))."</th>\n";
  1908. $text .= "</tr>\n";
  1909. $text .= "</thead>\n";
  1910. # Split content by row.
  1911. $rows = explode("\n", trim($content, "\n"));
  1912. $text .= "<tbody>\n";
  1913. foreach ($rows as $row) {
  1914. # Parsing span elements, including code spans, character escapes,
  1915. # and inline HTML tags, so that pipes inside those gets ignored.
  1916. $row = $this->parseSpan($row);
  1917. # Split row by cell.
  1918. $row_cells = preg_split('/ *[|] */', $row, $col_count);
  1919. $row_cells = array_pad($row_cells, $col_count, '');
  1920. $text .= "<tr>\n";
  1921. foreach ($row_cells as $n => $cell)
  1922. $text .= " <td$attr[$n]>".$this->runSpanGamut(trim($cell))."</td>\n";
  1923. $text .= "</tr>\n";
  1924. }
  1925. $text .= "</tbody>\n";
  1926. $text .= "</table>";
  1927. return $this->hashBlock($text) . "\n";
  1928. }
  1929. public function doDefLists($text) {
  1930. #
  1931. # Form HTML definition lists.
  1932. #
  1933. $less_than_tab = $this->tab_width - 1;
  1934. # Re-usable pattern to match any entire dl list:
  1935. $whole_list_re = '(?>
  1936. ( # $1 = whole list
  1937. ( # $2
  1938. [ ]{0,'.$less_than_tab.'}
  1939. ((?>.*\S.*\n)+) # $3 = defined term
  1940. \n?
  1941. [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition
  1942. )
  1943. (?s:.+?)
  1944. ( # $4
  1945. \z
  1946. |
  1947. \n{2,}
  1948. (?=\S)
  1949. (?! # Negative lookahead for another term
  1950. [ ]{0,'.$less_than_tab.'}
  1951. (?: \S.*\n )+? # defined term
  1952. \n?
  1953. [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition
  1954. )
  1955. (?! # Negative lookahead for another definition
  1956. [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition
  1957. )
  1958. )
  1959. )
  1960. )'; // mx
  1961. $text = preg_replace_callback('{
  1962. (?>\A\n?|(?<=\n\n))
  1963. '.$whole_list_re.'
  1964. }mx',
  1965. array(&$this, '_doDefLists_callback'), $text);
  1966. return $text;
  1967. }
  1968. public function _doDefLists_callback($matches) {
  1969. # Re-usable patterns to match list item bullets and number markers:
  1970. $list = $matches[1];
  1971. # Turn double returns into triple returns, so that we can make a
  1972. # paragraph for the last item in a list, if necessary:
  1973. $result = trim($this->processDefListItems($list));
  1974. $result = "<dl>\n" . $result . "\n</dl>";
  1975. return $this->hashBlock($result) . "\n\n";
  1976. }
  1977. public function processDefListItems($list_str) {
  1978. #
  1979. # Process the contents of a single definition list, splitting it
  1980. # into individual term and definition list items.
  1981. #
  1982. $less_than_tab = $this->tab_width - 1;
  1983. # trim trailing blank lines:
  1984. $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str);
  1985. # Process definition terms.
  1986. $list_str = preg_replace_callback('{
  1987. (?>\A\n?|\n\n+) # leading line
  1988. ( # definition terms = $1
  1989. [ ]{0,'.$less_than_tab.'} # leading whitespace
  1990. (?![:][ ]|[ ]) # negative lookahead for a definition
  1991. # mark (colon) or more whitespace.
  1992. (?> \S.* \n)+? # actual term (not whitespace).
  1993. )
  1994. (?=\n?[ ]{0,3}:[ ]) # lookahead for following line feed
  1995. # with a definition mark.
  1996. }xm',
  1997. array(&$this, '_processDefListItems_callback_dt'), $list_str);
  1998. # Process actual definitions.
  1999. $list_str = preg_replace_callback('{
  2000. \n(\n+)? # leading line = $1
  2001. ( # marker space = $2
  2002. [ ]{0,'.$less_than_tab.'} # whitespace before colon
  2003. [:][ ]+ # definition mark (colon)
  2004. )
  2005. ((?s:.+?)) # definition text = $3
  2006. (?= \n+ # stop at next definition mark,
  2007. (?: # next term or end of text
  2008. [ ]{0,'.$less_than_tab.'} [:][ ] |
  2009. <dt> | \z
  2010. )
  2011. )
  2012. }xm',
  2013. array(&$this, '_processDefListItems_callback_dd'), $list_str);
  2014. return $list_str;
  2015. }
  2016. public function _processDefListItems_callback_dt($matches) {
  2017. $terms = explode("\n", trim($matches[1]));
  2018. $text = '';
  2019. foreach ($terms as $term) {
  2020. $term = $this->runSpanGamut(trim($term));
  2021. $text .= "\n<dt>" . $term . "</dt>";
  2022. }
  2023. return $text . "\n";
  2024. }
  2025. public function _processDefListItems_callback_dd($matches) {
  2026. $leading_line = $matches[1];
  2027. $marker_space = $matches[2];
  2028. $def = $matches[3];
  2029. if ($leading_line || preg_match('/\n{2,}/', $def)) {
  2030. # Replace marker with the appropriate whitespace indentation
  2031. $def = str_repeat(' ', strlen($marker_space)) . $def;
  2032. $def = $this->runBlockGamut($this->outdent($def . "\n\n"));
  2033. $def = "\n". $def ."\n";
  2034. }
  2035. else {
  2036. $def = rtrim($def);
  2037. $def = $this->runSpanGamut($this->outdent($def));
  2038. }
  2039. return "\n<dd>" . $def . "</dd>\n";
  2040. }
  2041. public function doFencedCodeBlocks($text) {
  2042. #
  2043. # Adding the fenced code block syntax to regular Markdown:
  2044. #
  2045. # ~~~
  2046. # Code block
  2047. # ~~~
  2048. #
  2049. $less_than_tab = $this->tab_width;
  2050. $text = preg_replace_callback('{
  2051. (?:\n|\A)
  2052. # 1: Opening marker
  2053. (
  2054. ~{3,} # Marker: three tilde or more.
  2055. )
  2056. [ ]* \n # Whitespace and newline following marker.
  2057. # 2: Content
  2058. (
  2059. (?>
  2060. (?!\1 [ ]* \n) # Not a closing marker.
  2061. .*\n+
  2062. )+
  2063. )
  2064. # Closing marker.
  2065. \1 [ ]* \n
  2066. }xm',
  2067. array(&$this, '_doFencedCodeBlocks_callback'), $text);
  2068. return $text;
  2069. }
  2070. public function _doFencedCodeBlocks_callback($matches) {
  2071. $codeblock = $matches[2];
  2072. $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
  2073. $codeblock = preg_replace_callback('/^\n+/',
  2074. array(&$this, '_doFencedCodeBlocks_newlines'), $codeblock);
  2075. $codeblock = "<pre><code>$codeblock</code></pre>";
  2076. return "\n\n".$this->hashBlock($codeblock)."\n\n";
  2077. }
  2078. public function _doFencedCodeBlocks_newlines($matches) {
  2079. return str_repeat("<br$this->empty_element_suffix",
  2080. strlen($matches[0]));
  2081. }
  2082. #
  2083. # Redefining emphasis markers so that emphasis by underscore does not
  2084. # work in the middle of a word.
  2085. #
  2086. public $em_relist = array(
  2087. '' => '(?:(?<!\*)\*(?!\*)|(?<![a-zA-Z0-9_])_(?!_))(?=\S|$)(?![\.,:;]\s)',
  2088. '*' => '(?<=\S|^)(?<!\*)\*(?!\*)',
  2089. '_' => '(?<=\S|^)(?<!_)_(?![a-zA-Z0-9_])',
  2090. );
  2091. public $strong_relist = array(
  2092. '' => '(?:(?<!\*)\*\*(?!\*)|(?<![a-zA-Z0-9_])__(?!_))(?=\S|$)(?![\.,:;]\s)',
  2093. '**' => '(?<=\S|^)(?<!\*)\*\*(?!\*)',
  2094. '__' => '(?<=\S|^)(?<!_)__(?![a-zA-Z0-9_])',
  2095. );
  2096. public $em_strong_relist = array(
  2097. '' => '(?:(?<!\*)\*\*\*(?!\*)|(?<![a-zA-Z0-9_])___(?!_))(?=\S|$)(?![\.,:;]\s)',
  2098. '***' => '(?<=\S|^)(?<!\*)\*\*\*(?!\*)',
  2099. '___' => '(?<=\S|^)(?<!_)___(?![a-zA-Z0-9_])',
  2100. );
  2101. public function formParagraphs($text) {
  2102. #
  2103. # Params:
  2104. # $text - string to process with html <p> tags
  2105. #
  2106. # Strip leading and trailing lines:
  2107. $text = preg_replace('/\A\n+|\n+\z/', '', $text);
  2108. $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY);
  2109. #
  2110. # Wrap <p> tags and unhashify HTML blocks
  2111. #
  2112. foreach ($grafs as $key => $value) {
  2113. $value = trim($this->runSpanGamut($value));
  2114. # Check if this should be enclosed in a paragraph.
  2115. # Clean tag hashes & block tag hashes are left alone.
  2116. $is_p = !preg_match('/^B\x1A[0-9]+B|^C\x1A[0-9]+C$/', $value);
  2117. if ($is_p) {
  2118. $value = "<p>$value</p>";
  2119. }
  2120. $grafs[$key] = $value;
  2121. }
  2122. # Join grafs in one text, then unhash HTML tags.
  2123. $text = implode("\n\n", $grafs);
  2124. # Finish by removing any tag hashes still present in $text.
  2125. $text = $this->unhash($text);
  2126. return $text;
  2127. }
  2128. ### Footnotes
  2129. public function stripFootnotes($text) {
  2130. #
  2131. # Strips link definitions from text, stores the URLs and titles in
  2132. # hash references.
  2133. #
  2134. $less_than_tab = $this->tab_width - 1;
  2135. # Link defs are in the form: [^id]: url "optional title"
  2136. $text = preg_replace_callback('{
  2137. ^[ ]{0,'.$less_than_tab.'}\[\^(.+?)\][ ]?: # note_id = $1
  2138. [ ]*
  2139. \n? # maybe *one* newline
  2140. ( # text = $2 (no blank lines allowed)
  2141. (?:
  2142. .+ # actual text
  2143. |
  2144. \n # newlines but
  2145. (?!\[\^.+?\]:\s)# negative lookahead for footnote marker.
  2146. (?!\n+[ ]{0,3}\S)# ensure line is not blank and followed
  2147. # by non-indented content
  2148. )*
  2149. )
  2150. }xm',
  2151. array(&$this, '_stripFootnotes_callback'),
  2152. $text);
  2153. return $text;
  2154. }
  2155. public function _stripFootnotes_callback($matches) {
  2156. $note_id = $this->fn_id_prefix . $matches[1];
  2157. $this->footnotes[$note_id] = $this->outdent($matches[2]);
  2158. return ''; # String that will replace the block
  2159. }
  2160. public function doFootnotes($text) {
  2161. #
  2162. # Replace footnote references in $text [^id] with a special text-token
  2163. # which will be replaced by the actual footnote marker in appendFootnotes.
  2164. #
  2165. if (!$this->in_anchor) {
  2166. $text = preg_replace('{\[\^(.+?)\]}', "F\x1Afn:\\1\x1A:", $text);
  2167. }
  2168. return $text;
  2169. }
  2170. public function appendFootnotes($text) {
  2171. #
  2172. # Append footnote list to text.
  2173. #
  2174. $text = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}',
  2175. array(&$this, '_appendFootnotes_callback'), $text);
  2176. if (!empty($this->footnotes_ordered)) {
  2177. $text .= "\n\n";
  2178. $text .= "<div class=\"footnotes\">\n";
  2179. $text .= "<hr". $this->empty_element_suffix ."\n";
  2180. $text .= "<ol>\n\n";
  2181. $attr = " rev=\"footnote\"";
  2182. if ($this->fn_backlink_class != "") {
  2183. $class = $this->fn_backlink_class;
  2184. $class = $this->encodeAttribute($class);
  2185. $attr .= " class=\"$class\"";
  2186. }
  2187. if ($this->fn_backlink_title != "") {
  2188. $title = $this->fn_backlink_title;
  2189. $title = $this->encodeAttribute($title);
  2190. $attr .= " title=\"$title\"";
  2191. }
  2192. $num = 0;
  2193. while (!empty($this->footnotes_ordered)) {
  2194. $footnote = reset($this->footnotes_ordered);
  2195. $note_id = key($this->footnotes_ordered);
  2196. unset($this->footnotes_ordered[$note_id]);
  2197. $footnote .= "\n"; # Need to append newline before parsing.
  2198. $footnote = $this->runBlockGamut("$footnote\n");
  2199. $footnote = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}',
  2200. array(&$this, '_appendFootnotes_callback'), $footnote);
  2201. $attr = str_replace("%%", ++$num, $attr);
  2202. $note_id = $this->encodeAttribute($note_id);
  2203. # Add backlink to last paragraph; create new paragraph if needed.
  2204. $backlink = "<a href=\"#fnref:$note_id\"$attr>&#8617;</a>";
  2205. if (preg_match('{</p>$}', $footnote)) {
  2206. $footnote = substr($footnote, 0, -4) . "&#160;$backlink</p>";
  2207. } else {
  2208. $footnote .= "\n\n<p>$backlink</p>";
  2209. }
  2210. $text .= "<li id=\"fn:$note_id\">\n";
  2211. $text .= $footnote . "\n";
  2212. $text .= "</li>\n\n";
  2213. }
  2214. $text .= "</ol>\n";
  2215. $text .= "</div>";
  2216. }
  2217. return $text;
  2218. }
  2219. public function _appendFootnotes_callback($matches) {
  2220. $node_id = $this->fn_id_prefix . $matches[1];
  2221. # Create footnote marker only if it has a corresponding footnote *and*
  2222. # the footnote hasn't been used by another marker.
  2223. if (isset($this->footnotes[$node_id])) {
  2224. # Transfert footnote content to the ordered list.
  2225. $this->footnotes_ordered[$node_id] = $this->footnotes[$node_id];
  2226. unset($this->footnotes[$node_id]);
  2227. $num = $this->footnote_counter++;
  2228. $attr = " rel=\"footnote\"";
  2229. if ($this->fn_link_class != "") {
  2230. $class = $this->fn_link_class;
  2231. $class = $this->encodeAttribute($class);
  2232. $attr .= " class=\"$class\"";
  2233. }
  2234. if ($this->fn_link_title != "") {
  2235. $title = $this->fn_link_title;
  2236. $title = $this->encodeAttribute($title);
  2237. $attr .= " title=\"$title\"";
  2238. }
  2239. $attr = str_replace("%%", $num, $attr);
  2240. $node_id = $this->encodeAttribute($node_id);
  2241. return
  2242. "<sup id=\"fnref:$node_id\">".
  2243. "<a href=\"#fn:$node_id\"$attr>$num</a>".
  2244. "</sup>";
  2245. }
  2246. return "[^".$matches[1]."]";
  2247. }
  2248. ### Abbreviations ###
  2249. public function stripAbbreviations($text) {
  2250. #
  2251. # Strips abbreviations from text, stores titles in hash references.
  2252. #
  2253. $less_than_tab = $this->tab_width - 1;
  2254. # Link defs are in the form: [id]*: url "optional title"
  2255. $text = preg_replace_callback('{
  2256. ^[ ]{0,'.$less_than_tab.'}\*\[(.+?)\][ ]?: # abbr_id = $1
  2257. (.*) # text = $2 (no blank lines allowed)
  2258. }xm',
  2259. array(&$this, '_stripAbbreviations_callback'),
  2260. $text);
  2261. return $text;
  2262. }
  2263. public function _stripAbbreviations_callback($matches) {
  2264. $abbr_word = $matches[1];
  2265. $abbr_desc = $matches[2];
  2266. if ($this->abbr_word_re)
  2267. $this->abbr_word_re .= '|';
  2268. $this->abbr_word_re .= preg_quote($abbr_word);
  2269. $this->abbr_desciptions[$abbr_word] = trim($abbr_desc);
  2270. return ''; # String that will replace the block
  2271. }
  2272. public function doAbbreviations($text) {
  2273. #
  2274. # Find defined abbreviations in text and wrap them in <abbr> elements.
  2275. #
  2276. if ($this->abbr_word_re) {
  2277. // cannot use the /x modifier because abbr_word_re may
  2278. // contain significant spaces:
  2279. $text = preg_replace_callback('{'.
  2280. '(?<![\w\x1A])'.
  2281. '(?:'.$this->abbr_word_re.')'.
  2282. '(?![\w\x1A])'.
  2283. '}',
  2284. array(&$this, '_doAbbreviations_callback'), $text);
  2285. }
  2286. return $text;
  2287. }
  2288. public function _doAbbreviations_callback($matches) {
  2289. $abbr = $matches[0];
  2290. if (isset($this->abbr_desciptions[$abbr])) {
  2291. $desc = $this->abbr_desciptions[$abbr];
  2292. if (empty($desc)) {
  2293. return $this->hashPart("<abbr>$abbr</abbr>");
  2294. } else {
  2295. $desc = $this->encodeAttribute($desc);
  2296. return $this->hashPart("<abbr title=\"$desc\">$abbr</abbr>");
  2297. }
  2298. } else {
  2299. return $matches[0];
  2300. }
  2301. }
  2302. }