Poem.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. # MediaWiki Poem extension v1.0cis
  3. #
  4. # Based on example code from
  5. # http://meta.wikimedia.org/wiki/Write_your_own_MediaWiki_extension
  6. #
  7. # All other code is copyright © 2005 Nikola Smolenski <smolensk@eunet.yu>
  8. # (with modified parser callback and attribute additions)
  9. #
  10. # Anyone is allowed to use this code for any purpose.
  11. #
  12. # To install, copy the extension to your extensions directory and add line
  13. # include("extensions/Poem.php");
  14. # to the bottom of your LocalSettings.php
  15. #
  16. # To use, put some text between <poem></poem> tags
  17. #
  18. # For more information see its page at
  19. # http://meta.wikimedia.org/wiki/Poem_Extension
  20. if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
  21. $wgHooks['ParserFirstCallInit'][] = 'wfPoemExtension';
  22. } else {
  23. $wgExtensionFunctions[] = 'wfPoemExtension';
  24. }
  25. $wgExtensionCredits['parserhook'][] = array(
  26. 'name' => 'Poem',
  27. 'author' => array( 'Nikola Smolenski', 'Brion Vibber', 'Steve Sanbeg' ),
  28. 'url' => 'http://www.mediawiki.org/wiki/Extension:Poem',
  29. 'svn-date' => '$LastChangedDate: 2008-12-20 09:32:47 +0000 (Sat, 20 Dec 2008) $',
  30. 'svn-revision' => '$LastChangedRevision: 44839 $',
  31. 'description' => 'Adds <tt>&lt;poem&gt;</tt> tag for poem formatting',
  32. 'descriptionmsg' => 'poem-desc',
  33. );
  34. $wgParserTestFiles[] = dirname( __FILE__ ) . "/poemParserTests.txt";
  35. $wgExtensionMessagesFiles['Poem'] = dirname(__FILE__) . '/Poem.i18n.php';
  36. function wfPoemExtension() {
  37. $GLOBALS['wgParser']->setHook("poem","PoemExtension");
  38. return true;
  39. }
  40. function PoemExtension( $in, $param=array(), $parser=null ) {
  41. /* using newlines in the text will cause the parser to add <p> tags,
  42. * which may not be desired in some cases
  43. */
  44. $nl = isset( $param['compact'] ) ? '' : "\n";
  45. if( method_exists( $parser, 'recursiveTagParse' ) ) {
  46. //new methods in 1.8 allow nesting <nowiki> in <poem>.
  47. $tag = $parser->insertStripItem( "<br />", $parser->mStripState );
  48. $text = preg_replace(
  49. array( "/^\n/", "/\n$/D", "/\n/", "/^( +)/me" ),
  50. array( "", "", "$tag\n", "str_replace(' ','&nbsp;','\\1')" ),
  51. $in );
  52. $text = $parser->recursiveTagParse( $text );
  53. } else {
  54. $text = preg_replace(
  55. array( "/^\n/", "/\n$/D", "/\n/", "/^( +)/me" ),
  56. array( "", "", "<br />\n", "str_replace(' ','&nbsp;','\\1')" ),
  57. $in );
  58. $ret = $parser->parse(
  59. $text,
  60. $parser->getTitle(),
  61. $parser->getOptions(),
  62. // We begin at line start
  63. true,
  64. // Important, otherwise $this->clearState()
  65. // would get run every time <ref> or
  66. // <references> is called, fucking the whole
  67. // thing up.
  68. false
  69. );
  70. $text = $ret->getText();
  71. }
  72. global $wgVersion;
  73. if( version_compare( $wgVersion, "1.7alpha" ) >= 0 ) {
  74. // Pass HTML attributes through to the output.
  75. $attribs = Sanitizer::validateTagAttributes( $param, 'div' );
  76. } else {
  77. // Can't guarantee safety on 1.6 or older.
  78. $attribs = array();
  79. }
  80. // Wrap output in a <div> with "poem" class.
  81. if( isset( $attribs['class'] ) ) {
  82. $attribs['class'] = 'poem ' . $attribs['class'];
  83. } else {
  84. $attribs['class'] = 'poem';
  85. }
  86. return Xml::openElement( 'div', $attribs ) .
  87. $nl .
  88. trim( $text ) .
  89. "$nl</div>";
  90. }