HashComments-Talk
This space is for User-contributed commentary and notes. Please include your name and a date along with your comment.
Comment out character
I'd recommend changing the start-of-line comment-out character to one that is not a standard Wiki list character. The hash appears in the documentation and in many recipes. These may be more appropriate as start-of-line: semi-colon ; (comments in ini files) or fullstop . (hidden files in Unix). --Petko
If you choose a different character that doesn't conflict with the core, you can add a Cookbook:CustomSyntax to fade out the commented out lines. --Petko
I tried.
I tried. This recipe appears to have no effect against
page text variables. It appears to comment out again
everything except them. I tried. Is there any way to
comment them out with #? I tried '<extractptv' on the
markup. What is wrong with the php code? Is this impossible?
--gnuzoo
Page text variable definitions are parsed on the full page text in a different place, independent of markup rule order. When MarkupToHTML runs, definitions like (:Var:Value:) are simply removed from the page text, replaced by nothing. No need to comment them out. You should comment out lines that output the values like {*$:Var}. If you want to test how {*$:Var} behaves when there is no definition, you can temporarily prefix the variable name like (:DISABLE-Var:Value:) --Petko
It is a big convenience for me to put a # in front of a line to "comment it out".
It is inconvenient when you have to find a variable definition and put "DISABLE-" in front of the name.
And it still defines a page text variable named "DISABLE-Var". Could I comment out the code before
the page text variables are defined?
I had several paths defined while testing. They all had the same names with different values.
I wanted to comment a line in and test, then comment that out and comment another in and test
that. It would always use the value of the last defined page text variable, even though it was
commented out. I did not realize the problem until much debugging. I had about 10 paths I was
testing. The definitions were always being defined even if they were commented out.
This same problem happens using the (:if false:) method of making comments.
It seems very odd to me that a page code line can be commented out and still
processes the code. I have never known of code that allowed commented out lines
that would still process the code. The only effective way to comment
them out is by using (:comment Var:value:) which is inconvenient.
This recipe is made to make it super easy to comment in/out lines.
Along with recipe EditAssist, select some text, click a
button (comment or uncomment) and you are ready to save/view the results.
This works AFAIK on everything except on page text variable definitions.
It appears to me there should be some method to do this. If this caused
problems for me, is is likely to cause problems for others.
--gnuzoo
Can't your comment JS button also prefix the PTV definitions for you? --Petko
The js would get complex and confusing and be ambiguous.
How would I know if intent was to comment out just the PTV and keep
the rest of the line commented in? What if there are more than 1 PTV defined
on the same line?
I see this more as a fundamental problem with page code that cannot be
easily commented out. PTV definitions are parsed even when using the (:if false:)
method of making comments.
What would be better is if the recipe (or PmWiki) could allow some kind of method
to comment out a line base on the first character that would also stop the
PTV definitions on that line from processing. It appears PTV definitions inside
page code may not be affected by markup.
--gnuzoo
This continues in the next section.
Callback Function
I already mentioned that PTVs are parsed in a different place/circuit, separate from the MarkupToHTML route, the text is pulled directly from the page file without processing, and the variables are parsed and stored. We could add a hook for your recipe to define a callback function to pre-pocess this page text before parsing the variables. It could do a preg_replace and remove lines starting with the comment character. Will this work for you? --Petko
That would be exactly what I would like! That would be so awesome! --gnuzoo
Petko, actually, it might need to do a little more than a preg_replace.
If you glance at the reduced code for the next version you will see it is still compatible with lists:
Markup('hashcomments', '_begin', '/(?m)^.*$/', 'callback_hashcomments');
function callback_hashcomments($m) {
static $is_hashlist = false;
$line = $m[0];
# lines do not start with '#'
if (strpos(ltrim($line), '#') !== 0) {
if (stripos($line, '(:#lists:)' ) !== false) { $is_hashlist = true ; }
elseif (stripos($line, '(:#comments:)') !== false) { $is_hashlist = false; }
}
if (strpos($line, '#') === 0) {
if ($is_hashlist) return $line;
return ''; # remove comment line
}
# remove the directive tags
return preg_replace('/\(:\s*#(lists|comments)\s*:\)/i', '', $line);
}
Do I just comment out the "Markup" function in the recipe and set '$PTVPreParseFn' to the
name of my function "callback_hashcomments($m) {" or would there be more adjustments?
$PTVPreParseFn = callback_hashcomments ;
> PTVs are parsed in a different place/circuit, separate from the MarkupToHTML route
Might it be better to put the PTV defines into the MarkupToHTML route before '_begin''?
I'm sure it is complicated.
BTW, I do have code that will find all the pages that have an ordered list so someone
can put (:#lists:) in front of them and if necessary (:#comments:) after them.
I will put that code below this section.
--gnuzoo
We cannot move the PTV parsing into MarkupToHTML because we don't even process the markup of remote pages where we extract PTVs.
The full markup of the remote page PmWiki.BasicEditing is not processed, only its $:Summary PTV is extracted, then pasted below, then processed as part of the current page:
* Some PTV: {PmWiki.BasicEditing$:Summary}
* Another one: {PmWiki.ChangeLog$:Summary}
|
|
You still need your current function to process/hide lines in a page during MarkupToHTML. It seems you want to additionally change how PTVs are parsed, the separate function you define in $PTVPreParseFn receives the $pagename and the full text of the page as arguments. You can modify the text any way you want, for example, removing lines starting with the comment character, one or more replacements.
Say you have this:
(:Var1:This should be kept:) # (:Var2:This should be removed:) # Multiline (:Var3:This should # be removed:) |
Your function may look like this:
$PTVPreParseFn = 'gzStripPTVs';
function gzStripPTVs($pagename, $text) {
$rx = "/((^|\n)#.*)\\(: *\\w[-\\w]* *:(?!\\))[\\s\\S]*?:\\)/";
$text = preg_replace($rx, '$1', $text);
return $text;
}
Do whatever you want to do, keep the PTVs that should be found, remove the PTVs that should be removed, the sky is the limit, then return the text you want for PTV extraction. The returned text is temporary, it is discarded after PTV parsing. --Petko
FindOrderedLists
Code to find pages with ordered lists so users of hashcomments
can keep them working by putting (:#lists:) in front of them and
if necessary (:#comments:) after them. This should be done before
installing the recipe hashcomments.
Markup('FindOrderedLists', 'directives', '/\(:FindOrderedLists:\)/', 'FindOrderedLists');
function FindOrderedLists($m) {
global $ScriptUrl ;
$allPageNames = ListPages();
$pagesWithMatches = array();
foreach ($allPageNames as $pagename) {
if (preg_match('/^(Site|SiteAdmin|PmWiki)\./', $pagename)) continue;
$pageData = RetrieveAuthPage($pagename, 'read', false);
if (isset($pageData['text'])) {
# find pages that have a line that starts with '#'
if (preg_match('/^#/m', $pageData['text'])) {
# render the PmWiki page and see it it contains '<ol' - meaning it has an ordered list
$url = "$ScriptUrl/$pagename";
$htmlOutput = file_get_contents($url);
if (stripos($htmlOutput, '<ol') !== false) $pagesWithMatches[] = $pagename;
}
}
}
if (empty($pagesWithMatches)) return "No accessible pages found containing verified ordered lists. \$ScriptURL='$ScriptUrl'";
$out = "The following " . count($pagesWithMatches) . " pages contain ordered lists:\n\n";
foreach ($pagesWithMatches as $match) { $out .= "* [[$match]]\n"; }
return PRR($out);
}
I like talk pages
Talk page for the HashComments recipe (users).