LyricExtension/Source Code Dev
From PeacockWiki
(Difference between revisions)
Revision as of 03:22, 1 June 2006 (edit) Trevorp (Talk | contribs) ← Previous diff |
Current revision (06:04, 11 June 2006) (edit) Trevorp (Talk | contribs) |
||
Line 4: | Line 4: | ||
# | # | ||
# Simple lyric parser extension for mediawiki. | # Simple lyric parser extension for mediawiki. | ||
- | # Written by Trevor Peacock, 18 May 2006 | + | # Written by Trevor Peacock, 11 June 2006 |
- | # version 0.1 | + | # version 0.3 |
# Tested on MediaWiki 1.6devel, PHP 5.0.5 (apache2handler) | # Tested on MediaWiki 1.6devel, PHP 5.0.5 (apache2handler) | ||
+ | # | ||
+ | # Official Site | ||
+ | # http://wiki.peacocktech.com/wiki/LyricExtension | ||
# | # | ||
# developed to support the notation of lyrics in mediawiki. | # developed to support the notation of lyrics in mediawiki. | ||
Line 14: | Line 17: | ||
# * Allows basic lyric notation | # * Allows basic lyric notation | ||
# * Optional CSS styling embedded in every page | # * Optional CSS styling embedded in every page | ||
+ | # * CSS styling not embedded in meta tage, rather @import-ed from extension file | ||
+ | # * Support for lyricnote and spoken/speech tags | ||
+ | # | ||
+ | # Thanks to Sean Columbo for his assistance | ||
+ | # http://lyricwiki.org/User:Sean_Colombo | ||
# | # | ||
# To install, copy this file into "extensions" directory, and add | # To install, copy this file into "extensions" directory, and add | ||
Line 21: | Line 29: | ||
# require("extensions/lyric.php"); | # require("extensions/lyric.php"); | ||
# | # | ||
+ | |||
+ | ################################################################################ | ||
+ | # Functions | ||
+ | # | ||
+ | # This section has no configuration, and can be ignored. | ||
+ | # | ||
+ | |||
+ | function filename($name) | ||
+ | { | ||
+ | $name=explode('/', $name); | ||
+ | $name=explode('\\', $name[count($name)-1]); | ||
+ | return $name[count($name)-1]; | ||
+ | } | ||
################################################################################ | ################################################################################ | ||
Line 28: | Line 49: | ||
# | # | ||
+ | if(isset($wgScriptPath)) | ||
+ | { | ||
$wgExtensionCredits["parserhook"][]=array( | $wgExtensionCredits["parserhook"][]=array( | ||
'name' => 'Lyric Extension', | 'name' => 'Lyric Extension', | ||
Line 34: | Line 57: | ||
'author' => '[http://about.peacocktech.com/trevorp/ Trevor Peacock]', | 'author' => '[http://about.peacocktech.com/trevorp/ Trevor Peacock]', | ||
'description' => 'Adds features allowing easy notation of lyrics in mediawiki' ); | 'description' => 'Adds features allowing easy notation of lyrics in mediawiki' ); | ||
- | + | } | |
################################################################################ | ################################################################################ | ||
Line 46: | Line 69: | ||
# | # | ||
+ | if(isset($wgScriptPath)) | ||
+ | { | ||
#Instruct mediawiki to call LyricExtension to initialise new extension | #Instruct mediawiki to call LyricExtension to initialise new extension | ||
$wgExtensionFunctions[] = "LyricExtension"; | $wgExtensionFunctions[] = "LyricExtension"; | ||
+ | } | ||
#Install extension | #Install extension | ||
Line 55: | Line 81: | ||
global $wgParser; | global $wgParser; | ||
$wgParser->setHook("lyric", "renderLyric"); | $wgParser->setHook("lyric", "renderLyric"); | ||
+ | $wgParser->setHook("lyrics", "renderLyric"); | ||
+ | $wgParser->setHook("spoken", "renderLyricSpeech"); | ||
+ | $wgParser->setHook("speech", "renderLyricSpeech"); | ||
+ | $wgParser->setHook("lyricnote", "renderLyricNote"); | ||
+ | } | ||
+ | |||
+ | function renderWikiText($input, &$parser) | ||
+ | { | ||
+ | return $parser->parse($input, $parser->mTitle, $parser->mOptions, true, false)->getText(); | ||
} | } | ||
#render <lyric> text | #render <lyric> text | ||
- | function renderLyric($input, $argv) | + | function breakText($input) |
+ | { | ||
+ | return str_replace(array("\r\n", "\r","\n"), "<br/>", trim($input)); | ||
+ | } | ||
+ | |||
+ | function renderLyric($input, $argv, &$parser) | ||
{ | { | ||
#make new lines in wikitext new lines in html | #make new lines in wikitext new lines in html | ||
- | $transform=str_replace(array("\r\n", "\r","\n"), "<br/>", trim($input)); | + | $transform=breakText($input); |
#define css lyric style | #define css lyric style | ||
Line 67: | Line 107: | ||
#parse embedded wikitext | #parse embedded wikitext | ||
- | global $wgOut; | + | #dirty dirty hack: how else to pass parameter to subsequent calls? |
- | return $wgOut->parse($transform, false); | + | $parser->mTitle->mRestrictions['inlyric']=1; |
+ | $output=renderWikiText($transform, $parser); | ||
+ | unset($parser->mTitle->mRestrictions['inlyric']); | ||
+ | return $output; | ||
+ | } | ||
+ | |||
+ | function renderCSSTag($input, $argv, &$parser, $tag) | ||
+ | { | ||
+ | #var_dump($parser->mTitle); | ||
+ | #define css lyric style | ||
+ | if(!isset($parser->mTitle->mRestrictions['inlyric'])) | ||
+ | { | ||
+ | #http://svn.wikimedia.org/doc/classParser.html#ee79d906f7320ab444b48d19c9d20f1e | ||
+ | #above instructs parse is not to be called recursivly. Am i breaking this? | ||
+ | # $transform="<lyric>"."<div id=\"$tag\">".$input."</div>"."</lyric>"; | ||
+ | $transform="<lyric>"."<$tag>".$input."</$tag>"."</lyric>"; | ||
+ | #parse embedded wikitext | ||
+ | $output=renderWikiText(breakText($transform), $parser); | ||
+ | return $output; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | return "<div id=\"$tag\">".renderWikiText(breakText($input), $parser)."</div>"; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #render <speech> text | ||
+ | function renderLyricSpeech($input, $argv, &$parser) | ||
+ | { | ||
+ | return renderCSSTag($input, $argv, &$parser, 'spoken'); | ||
+ | } | ||
+ | |||
+ | #render <note> text | ||
+ | function renderLyricNote($input, $argv, &$parser) | ||
+ | { | ||
+ | return renderCSSTag($input, $argv, &$parser, 'lyricnote'); | ||
} | } | ||
Line 81: | Line 156: | ||
# If you wish to manually define the style in the css templates, | # If you wish to manually define the style in the css templates, | ||
# add a "#" at the start of this line: | # add a "#" at the start of this line: | ||
+ | if(isset($wgScriptPath)) | ||
+ | { | ||
$wgHooks['SkinTemplateSetupPageCss'][] = 'LyricCss'; | $wgHooks['SkinTemplateSetupPageCss'][] = 'LyricCss'; | ||
+ | } | ||
function LyricCss(&$css) { | function LyricCss(&$css) { | ||
- | $css = "/*<![CDATA[*/\n". | + | $css = "/*<![CDATA[*/". |
- | "#lyric {\n". | + | " @import \"$wgScriptPath/extensions/".filename(__FILE__)."\"; ". |
- | " padding: 1em;\n". | + | "/*]]>*/"; |
- | " border: 1px solid silver;\n". | + | |
- | " color: black;\n". | + | |
- | " background-color: #ffffcc;\n". | + | |
- | "}\n". | + | |
- | "\n/*]]>*/"; | + | |
return true; | return true; | ||
+ | } | ||
+ | |||
+ | ################################################################################ | ||
+ | # CSS section | ||
+ | # | ||
+ | # This section has no configuration, and can be ignored. | ||
+ | # | ||
+ | |||
+ | if(!isset($wgScriptPath)) | ||
+ | { | ||
+ | header("Content-type: text/css"); | ||
+ | ?>#lyric { | ||
+ | padding: 1em; | ||
+ | border: 1px solid silver; | ||
+ | color: black; | ||
+ | background-color: #ffffcc; | ||
+ | } | ||
+ | |||
+ | #lyricnote { | ||
+ | padding: 0em; | ||
+ | border: 0px; | ||
+ | color: black; | ||
+ | background-color: #cccccc; | ||
+ | } | ||
+ | |||
+ | #spoken { | ||
+ | padding: 0em; | ||
+ | border: 0px; | ||
+ | color: black; | ||
+ | background-color: #ffffff; | ||
+ | } | ||
+ | <?php | ||
} | } | ||
?> | ?> | ||
</pre> | </pre> |
Current revision
<?php # # Simple lyric parser extension for mediawiki. # Written by Trevor Peacock, 11 June 2006 # version 0.3 # Tested on MediaWiki 1.6devel, PHP 5.0.5 (apache2handler) # # Official Site # http://wiki.peacocktech.com/wiki/LyricExtension # # developed to support the notation of lyrics in mediawiki. # see http://lyricwiki.org/User:TrevorP/Notation # # Features: # * Allows basic lyric notation # * Optional CSS styling embedded in every page # * CSS styling not embedded in meta tage, rather @import-ed from extension file # * Support for lyricnote and spoken/speech tags # # Thanks to Sean Columbo for his assistance # http://lyricwiki.org/User:Sean_Colombo # # To install, copy this file into "extensions" directory, and add # the following line to the end of LocalSettings.php # (above the ? > ) # # require("extensions/lyric.php"); # ################################################################################ # Functions # # This section has no configuration, and can be ignored. # function filename($name) { $name=explode('/', $name); $name=explode('\\', $name[count($name)-1]); return $name[count($name)-1]; } ################################################################################ # Extension Credits Definition # # This section has no configuration, and can be ignored. # if(isset($wgScriptPath)) { $wgExtensionCredits["parserhook"][]=array( 'name' => 'Lyric Extension', 'version' => '0.1', 'url' => 'http://wiki.peacocktech.com/wiki/LyricExtension', 'author' => '[http://about.peacocktech.com/trevorp/ Trevor Peacock]', 'description' => 'Adds features allowing easy notation of lyrics in mediawiki' ); } ################################################################################ # Lyric Render Section # # This section has no configuration, and can be ignored. # # This section renders <lyric> tags. It forces a html break on every line, # and styles the section with a css id. # this id can either be in the mediawiki css files, or defined by the extension # if(isset($wgScriptPath)) { #Instruct mediawiki to call LyricExtension to initialise new extension $wgExtensionFunctions[] = "LyricExtension"; } #Install extension function LyricExtension() { #install hook on the element <lyric> global $wgParser; $wgParser->setHook("lyric", "renderLyric"); $wgParser->setHook("lyrics", "renderLyric"); $wgParser->setHook("spoken", "renderLyricSpeech"); $wgParser->setHook("speech", "renderLyricSpeech"); $wgParser->setHook("lyricnote", "renderLyricNote"); } function renderWikiText($input, &$parser) { return $parser->parse($input, $parser->mTitle, $parser->mOptions, true, false)->getText(); } #render <lyric> text function breakText($input) { return str_replace(array("\r\n", "\r","\n"), "<br/>", trim($input)); } function renderLyric($input, $argv, &$parser) { #make new lines in wikitext new lines in html $transform=breakText($input); #define css lyric style $transform="<div id=\"lyric\">".$transform."</div>"; #parse embedded wikitext #dirty dirty hack: how else to pass parameter to subsequent calls? $parser->mTitle->mRestrictions['inlyric']=1; $output=renderWikiText($transform, $parser); unset($parser->mTitle->mRestrictions['inlyric']); return $output; } function renderCSSTag($input, $argv, &$parser, $tag) { #var_dump($parser->mTitle); #define css lyric style if(!isset($parser->mTitle->mRestrictions['inlyric'])) { #http://svn.wikimedia.org/doc/classParser.html#ee79d906f7320ab444b48d19c9d20f1e #above instructs parse is not to be called recursivly. Am i breaking this? # $transform="<lyric>"."<div id=\"$tag\">".$input."</div>"."</lyric>"; $transform="<lyric>"."<$tag>".$input."</$tag>"."</lyric>"; #parse embedded wikitext $output=renderWikiText(breakText($transform), $parser); return $output; } else { return "<div id=\"$tag\">".renderWikiText(breakText($input), $parser)."</div>"; } } #render <speech> text function renderLyricSpeech($input, $argv, &$parser) { return renderCSSTag($input, $argv, &$parser, 'spoken'); } #render <note> text function renderLyricNote($input, $argv, &$parser) { return renderCSSTag($input, $argv, &$parser, 'lyricnote'); } ################################################################################ # CSS Styling Section # # This section may require configuration. # # This section adds a css style to all pages to style lyric sections # # If you wish to manually define the style in the css templates, # add a "#" at the start of this line: if(isset($wgScriptPath)) { $wgHooks['SkinTemplateSetupPageCss'][] = 'LyricCss'; } function LyricCss(&$css) { $css = "/*<![CDATA[*/". " @import \"$wgScriptPath/extensions/".filename(__FILE__)."\"; ". "/*]]>*/"; return true; } ################################################################################ # CSS section # # This section has no configuration, and can be ignored. # if(!isset($wgScriptPath)) { header("Content-type: text/css"); ?>#lyric { padding: 1em; border: 1px solid silver; color: black; background-color: #ffffcc; } #lyricnote { padding: 0em; border: 0px; color: black; background-color: #cccccc; } #spoken { padding: 0em; border: 0px; color: black; background-color: #ffffff; } <?php } ?>