php-textile icon indicating copy to clipboard operation
php-textile copied to clipboard

Unespected space at beginning of link text

Open neffets opened this issue 8 years ago • 12 comments

I was irritated by text from an editor. Why it does not generate correct links?

The raw text was:

E-Mail: " name":mailto:[email protected]

it generates as html output

<a href="mailto:[email protected]">E-Mail: “ name</a>

Until I found: the link-text begins with a space. I expected as output:

E-Mail: <a href="mailto:[email protected]"> name</a>

or

E-Mail: <a href="mailto:[email protected]">name</a>

Why we cannot accept spaces at beginning of link-text? (came from paste and copy) And it made a typographic quote from it.

neffets avatar Jul 10 '16 20:07 neffets

combined bug. if a second line have a broken link (caused by space-linktext), then a former correct link will be broken too

e.g.

* "Bauordnung für Berlin":http://www.stadtentwicklung.berlin.de/service/gesetzestexte/de/bauen.shtml
* " Verwaltungsverfahrensgesetz":http://www.gesetze-im-internet.de/vwvfg/BJNR012530976.html

will get you two broken / not-evaluated links. line one alone will get you an correct evaluated link.

neffets avatar Jul 11 '16 10:07 neffets

Broken syntax will generate broken code, in any language.

eliph avatar Jul 14 '16 07:07 eliph

@neffets I've just pushed a new branch with a partial fix for this. Could you try out the issue-168 branch and let me know how that works for you. Could you please also verify that the issue you reported in #167 still exists in that branch.

Many thanks!

netcarver avatar Jul 21 '16 21:07 netcarver

@eliph I'd like to make the handling of this case more robust if I can - but I agree with your sentiment here.

netcarver avatar Jul 23 '16 21:07 netcarver

@eliph @neffets - I've pushed an update to the issue-168 branch (link mentioned above) that should improve the detection of the start-of-link quote. Would appreciate if folks would test test this.

netcarver avatar Jul 25 '16 23:07 netcarver

Tested the "issue-168" branch.

It accepts the first link with a space at begin of the linktext. But only the frist. I You sets such an link in line-1, line-2 and line-3 - only the first line will be recognized and changed to a link.

With "issue-168" branch the line-2 and line-3 remains untouched


" Line 1":/test/
" Line 2":/test/
" Line 3":/test/

generates source code

<a href="/test/">Line 1</a><br />
&#8220; Line 2&#8221;:/test/<br />
&#8220; Line 3&#8221;:/test/<br />

Bug #167 is fixed in this branch, it works

neffets avatar Jul 27 '16 22:07 neffets

@neffets Just pushed a correction that should cover this case. Please test again.

netcarver avatar Jul 28 '16 08:07 netcarver

@netcarver: Generally I would not not consider bad usage of the Textile syntax a "bug", as already pointed out. Hopefully, this change will not result in breaking other, complex situations where Textile is used correcly.

eliph avatar Jul 28 '16 08:07 eliph

Hi @eliph - this has not caused any of the existing test cases to fail. It also makes php-textile more resilient to typos in otherwise correct link text. If you know of any examples that this change would negatively impact, please make them known so I can include them in the test cases and work towards getting them fixed.

netcarver avatar Jul 28 '16 10:07 netcarver

Thanx @eliph , with last commit it is now working completely, all links work.

neffets avatar Jul 28 '16 10:07 neffets

@neffets Thank you for testing that. I'll leave this open a few more days in case there is more feedback.

netcarver avatar Jul 28 '16 12:07 netcarver

Hello, i tried v3.6.0 it fixes issue #167, but issue #168 has still problems.

Example-1:

" Line 1":/test/ " Line 2":/test/ " Line 3":/test/`

Example-2:

"Line 1":/test/ " Line 2":/test/ " Line 3":/test/

I need a little patch in line 3630 like former issue-168 patch

patch.Netcarver_Textile_Parser-118.txt

--- vendor/netcarver/textile/src/Netcarver/Textile/Parser.php-3.6.0	2018-10-09 09:23:42.792001373 +0000
+++ vendor/netcarver/textile/src/Netcarver/Textile/Parser.php	2018-10-09 15:38:24.667558319 +0000
@@ -3627,16 +3627,27 @@
                     }
                 }
 
-                // Rebuild the link's text by reversing the parts and sticking them back
-                // together with quotes.
-                $link_content = implode('"', array_reverse($linkparts));
- 
-                // Rebuild the remaining stuff that goes before the link but that's
-                // already in order.
-                $pre_link = implode('"', $possible_start_quotes);
- 
-                // Re-assemble the link starts with a specific marker for the next regex.
-                $slice = $pre_link . $this->uid.'linkStartMarker:"' . $link_content;
+                if ($balanced > 0) {
+                    // We found more end-quotes than start quotes. We can try to correct this if there are 'ambiguous' quotes
+                    // present. Ambiguous quotes are quotes that appear with spaces on each side - such that they are not
+                    // obviously a start or an end quote.
+                    $replaced = 0;
+                    $slice = preg_replace('~ +" +~'.$mod, ' '.$this->uid.'linkStartMarker:"', $slice, 1, $replaced);
+                    if ($replaced == 0) {
+                        $slice = preg_replace('~^" +~m'.$mod, $this->uid.'linkStartMarker:"', $slice, 1, $replaced);
+                    }
+                } else {
+                    // Rebuild the link's text by reversing the parts and sticking them back
+                    // together with quotes.
+                    $link_content = implode('"', array_reverse($linkparts));
+ 
+                    // Rebuild the remaining stuff that goes before the link but that's
+                    // already in order.
+                    $pre_link = implode('"', $possible_start_quotes);
+ 
+                    // Re-assemble the link starts with a specific marker for the next regex.
+                    $slice = $pre_link . $this->uid.'linkStartMarker:"' . $link_content;
+                }
             }
 
             // Add the last part back

neffets avatar Oct 09 '18 15:10 neffets