Page MenuHomePhabricator

Add Highlight Remarkup Rule
ClosedPublic

Authored by chad on Nov 2 2015, 8:42 PM.
Tags
None
Referenced Files
Unknown Object (File)
Fri, Dec 27, 1:42 AM
Unknown Object (File)
Wed, Dec 11, 1:21 AM
Unknown Object (File)
Sun, Dec 8, 11:18 PM
Unknown Object (File)
Sun, Dec 8, 9:20 PM
Unknown Object (File)
Fri, Dec 6, 9:21 AM
Unknown Object (File)
Oct 21 2024, 11:38 PM
Unknown Object (File)
Oct 18 2024, 9:15 PM
Unknown Object (File)
Oct 14 2024, 7:25 AM

Details

Summary

Ref T5560, Adds a basic highlight rule.

Test Plan

Edit a Document and add a highlighted word.

Diff Detail

Repository
rPHU libphutil
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

chad retitled this revision from to Add Highlight Remarkup Rule.
chad updated this object.
chad edited the test plan for this revision. (Show Details)
chad added a reviewer: epriestley.

Something is wrong with the unit test, but I can't figure out what. Here's the failure:

Assertion failed, expected values to be equal (at PhutilRemarkupEngineTestCase.php:56): Failed to markup HTML in file 'highlight.txt'.
Expected vs Actual Output Diff
--- Old Value
+++ New Value
@@ -1,4 +1 @@
-'<p>how about we
-<span class="remarkup-highlight">highlight</span>
-some
-<span class="remarkup-highlight">text</span>!</p>'
+'<p>how about we !!highlight!! some !!text!!!</p>'

Oh, I think you need to add the rule to PhutilRemarkupEngineTestCase.

For this input:

!!xxx!!!

...it seems like the "!" at the end should be highlighted? I think you could do that with a regular expression like this:

'@!!(.+?)(!{2,})@'

...which says "match two or more exclamation points at the end".

Then, in applyCallback(), you'll get $matches[2] with all the exclamation points at the end. Then do:

// Remove the two exclamation points that represent syntax.
$excitement = substr($matches[2], 2);

// $excitement now has two fewer !'s than we started with.

return hsprintf('<blah ...>%s%s<... blah>', $matches[1], $excitement);
src/markup/engine/remarkup/markuprule/PhutilRemarkupHighlightRule.php
16

You should be able to get away without escaping the ! characters, since they don't have special meanings in regular expressions. I think this slightly simpler thing will work:

'@!!(.+?)!!@s'

oh, I presumed it should not highlight the ! in the second example. Maybe we should use \\this to highlight\\ instead?

My thinking is that if I type this:

IF YOU DO THIS, IT WILL DESTROY ALL YOUR DATA!

...I probably mean the extra "!" as punctuation for the highlighted part. Are you imagining a different sort of use case?

I think we currently could use \\ but I'm hesitant to use it for things that aren't related to escaping because it's so consistently an escaping character across many programming languages. For example, there's no way to write two bold asterisks right now. Possibly this sequence should eventually produce that:

**\*\***

That is: begin bold text, two literal escaped asterisks, end bold text.

'@!!(.+?)(!{2,})@' doesn't quite work, I get:

pasted_file (59×135 px, 4 KB)

We should also maybe use a regex like this:

'@!!(?!\s)...

That is, "two exclamation points, not followed by a whitespace character". Then this wouldn't highlight:

No Do not do this It is bad!!

...because all of the "!!" are followed by spaces (or no text, in the last case).

javascript and regexes are my kryptonite

it's fine now after a second test.

chad edited edge metadata.
  • Updates
In D14384#160642, @chad wrote:

javascript and regexes are my kryptonite

https://regex101.com/ don't help ??

epriestley edited edge metadata.

That lint stuff will clear itself up if we move the stuff to a __tests__/data/ directory, I just haven't gotten around to doing that.

This revision is now accepted and ready to land.Nov 2 2015, 9:16 PM
This revision was automatically updated to reflect the committed changes.