Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
variable scope, $Head->AddString, argh
ithcy
New
so i'm writing a new extension which extends the HtmlFormatter extension. i need to add strings (some css generated on the fly) to the document head and i'm trying to do that with the Head->AddString method, but php's variable scoping (or $Head->AddString) is giving me headaches.
this code almost works -- it appends "touched by MyFormatter" to every Html-formatted comment, but it only inserts "<!-- test -->" into the head once.
shouldn't there be as many "<!-- test -->" added to the head as there are Html-formatted comments on the page?
what am i missing here?
extensions/MyFormatter/default.php:
<?php
/*
Extension Name: MyFormatter
Extension Url: http://server.com/
Description: skeleton of an html formatter extension.
Version: 0.1
Author: ithcy
Author Url: http://server.com/
*/
class MyFormatter extends HtmlFormatter
{
function Parse($String, $Object, $FormatPurpose)
{
if($FormatPurpose == FORMAT_STRING_FOR_DISPLAY) return $this->MyFormatter($String);
else return $String;
}
public function MyFormatter($txt)
{
global $Head;
$Head->AddString("\n<!-- test -->\n");
return $txt . "<br>this text touched by MyFormatter.";
}
}
$MyFormatter = $Context->ObjectFactory->NewObject($Context, "MyFormatter");
$Context->StringManipulator->AddManipulator("Html", $MyFormatter);
?>
this code almost works -- it appends "touched by MyFormatter" to every Html-formatted comment, but it only inserts "<!-- test -->" into the head once.
shouldn't there be as many "<!-- test -->" added to the head as there are Html-formatted comments on the page?
what am i missing here?
0
This discussion has been closed.
Comments
What are you trying to accomplish? Maybe there's another way...
<code lang="perl">print "x";</code>
would invoke geshi's perl highlighter on the contents of that code element. (geshi actually turns the whole code block into a styled pre or div.)geshi can use css to highlight code. it generates the css on the fly, one stylesheet per language. if there are multiple code blocks in different languages on the page, multiple stylesheets are needed.
as i've written it now, to save page weight, i don't render all the stylesheets that geshi can produce at once - i only call up the ones for the languages that are used on the page.
i've actually made it work perfectly by rendering the stylesheets within the comments div, but it's against standards to have inline stylesheets in the body, isn't it?
something like ...
function CommentGrid_IncludeGeshiScripts(&$CommentGrid) { global $Head; while ($Row = $CommentGrid->Context->Database->GetRow($CommentGrid->CommentData)) { // Perform any actions you need to on the head control based on the $Row data } $CommentGrid->Context->Database->RewindDataSet($CommentGrid->CommentData); } $Context->AddToDelegate('CommentGrid', 'Constructor', 'CommentGrid_IncludeGeshiScripts');
//so can i get rid of the Parse function altogether, or how do i work that?
(note: i have been reading the docs. i'm just feeling slow this week.)
//is there a way to update the comment bodies in that function before they get passed to Parse?
of course there is just one more thing. now that this formatter is working, it seems to be preventing both Html Formatter, and another extension which also extends Html Formatter, from executing.
do i need to do anything special to make sure all formatters execute? i thought AddManipulator with a NewObject with a unique name would be enough.
$GeSHiFormatter = $Context->ObjectFactory->NewObject($Context, "GeSHiFormatter"); $Context->StringManipulator->Formatters["Html"]->AddChildFormatter($GeSHiFormatter);
this is my way (based on HtmlPlusPlus extension):
$GeSHiFormatter = $Context->ObjectFactory->NewObject($Context, "GeSHiFormatter"); $Context->StringManipulator->AddManipulator("Html", $GeSHiFormatter);
when i do it my way, GeshiColor works but HtmlPlusPlus and HtmlFormatter don't work.
when i do it the ExtendedTextFormatter way, GeshiColor doesn't work.
If you can provide a sample of the type of comments ppl will enter as well, that would be spanky.
thanks again.
Regardless, I added it in and it is available in svn. So, if you use this method...
$GeSHiFormatter = $Context->ObjectFactory->NewObject($Context, "GeSHiFormatter"); $Context->StringManipulator->Formatters["Html"]->AddChildFormatter($GeSHiFormatter);
... it will work. The only problem is the language="javascript" because the htmlformatter doesn't even allow the word javascript to be written - it replaces some characters in the word with their html charcode equivalent. So <code language="javascript> will not work. You may have to do some more tinkering in your extension for that...
AddChildFormatter works now, though.
i am starting to think that the problem is in HtmlPlusPlus. it's an old extension and some of its code probably needs to be updated to match 1.0.