Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for additional geshi options using an extendable options array (PR-2). #1985

Closed
wants to merge 3 commits into from

Conversation

lpaulsen93
Copy link
Collaborator

Also see the feature request in issue #1625.

I extended the syntax for the code and file tags in the following way:

  • additional options for geshi can be specified in square brackets
  • options have to be separated by commas
  • values should be enclosed in "

The actually implemented additional options are named after their corresponding Geshi functions:

  • enable_line_numbers
  • start_line_numbers_at
  • highlight_lines_extra
  • enable_keyword_links

Here are some syntax examples:

Example 1, normal code tag, unchanged behaviour:

<code C Hello.c>
void main () {
    printf ("Hello World 123!");
    exit 0;
}
</code>

Example 2, enable line numbers:

<code C Hello.c [enable_line_numbers]>
void main () {
    printf ("Hello World 123!");
    exit 0;
}
</code>

Example 3, highlight a specific line:

<code C Hello.c [highlight_lines_extra="2"]>
void main () {
    printf ("Hello World 123!");
    exit 0;
}
</code>

Example 4, start at specific line number (requires two options):

<code C Hello.c [enable_line_numbers, start_line_numbers_at="42"]>
void main () {
    printf ("Hello World 123!");
    exit 0;
}
</code>

Example 5, highlight specific lines:

<code C Hello.c [highlight_lines_extra="2,3"]>
void main () {
    printf ("Hello World 123!");
    exit 0;
}
</code>

Implementation details:
In function code in file handler.php options get cut off the match and are parsed in function parse_highlight_options. The function searches for key=value pairs separated by commas. Each found pair is added to the options array like $options[$key] = $value;.

The options array is passed through as a additional parameter in functions code, file and _highlight in file xhtml.php. And finally its passed to p_xhtml_cached_geshi which calls the corresponding functions.

@mention-bot
Copy link

@LarsGit223, thanks for your PR! By analyzing the history of the files in this pull request, we identified @splitbrain, @andyboeh and @Chris--S to be potential reviewers.

@lpaulsen93
Copy link
Collaborator Author

@splitbrain: maybe you also want to merge this. We've already worked on that some time ago (also see the changes/work done in #1625).

@lpaulsen93
Copy link
Collaborator Author

@splitbrain: little reminder ping. It would be cool if this could become part of the next release if it's acceptable/planned to add it. Just want to make sure it's not forgotten maybe.

@splitbrain splitbrain added this to the 🐱 Greebo milestone Apr 6, 2018
Copy link
Collaborator

@splitbrain splitbrain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@micgro42 what do you think?

protected function parse_highlight_options ($options) {
$result = array();
preg_match_all('/(\w+(?:="[^"]*"))|(\w+[^=,\]])(?:,*)/', $options, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this parser needed? At first glance the syntax looks like json. What about using the json parser?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. It never occured to me that this is basically JSON.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the consensus here? Do we keep the parser or do we swap to JSON (means a change is requested)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to replace this by json_decode() it makes the code less complex and easier to understand.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Klap-in
Copy link
Collaborator

Klap-in commented Apr 9, 2018

For normal applications of code blocks, I guess that most users will experience this syntax as quite complex. Therefore I recommend to describe these options only at a separate wikipage (and not as addition on the syntax wikipage). Such that an unexperienced user is not overwhelmed by details.

Copy link
Collaborator

@micgro42 micgro42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bug when highlighting lines, it eats the newline:

screenshot-2018-4-9 geshi testwiki

@lpaulsen93
Copy link
Collaborator Author

@micgro42: true, but it's OK if you use highlight and line numbers together:

geshi_highlightok

As this seems to be a Geshi issue what can we do about it? Which version of Geshi does DokuWiki run?

@lpaulsen93
Copy link
Collaborator Author

It's still there in the latest version and has already been reported some time ago, see https://sourceforge.net/p/geshi/bugs/190/.

@splitbrain
Copy link
Collaborator

we're using https://github.com/GeSHi/geshi-1.0

@splitbrain
Copy link
Collaborator

I submitted GeSHi/geshi-1.0#104 to address this.

@lpaulsen93
Copy link
Collaborator Author

Below some examples with the new JSON encoding:

<code JavaScript {"enable_line_numbers":true,"highlight_lines_extra":[2]}>
var de = function() {
	return (typeof(window.de) == 'object') ? window.de : {};
}();
</code>
<code C {"enable_line_numbers":false, "start_line_numbers_at":4}>
void main () {
    printf ("Hello World!");
    exit 0;
}
</code>
<code C {"enable_keyword_links":false,"enable_line_numbers":true, "highlight_lines_extra":[2,4]}>
void main () {
    printf ("Hello World!");
    exit 0;
}
</code>

@splitbrain
Copy link
Collaborator

You're gonna hate me… but now that I actually see the syntax in JSON I do not really like it 😞 Sorry, I should have actually thought this through.

My fault. I'll clean this up and merge. Thank you @LarsGit223

@splitbrain splitbrain mentioned this pull request Apr 13, 2018
@lpaulsen93
Copy link
Collaborator Author

@splitbrain: never mind.

@lpaulsen93
Copy link
Collaborator Author

Closed in favor of #2307.

@lpaulsen93 lpaulsen93 closed this Apr 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants