Browse files

added support for: soft breaks, email links and image alignment

  • Loading branch information...
1 parent 2c0c67e commit e17bc3f54a9c80bfb048aa86c1a4016debb24048 Christophe VG committed Sep 11, 2011
Showing with 51 additions and 5 deletions.
  1. +6 −1 js/breakdown.js
  2. +12 −2 js/test.html
  3. +21 −0 php/breakdown.php
  4. +12 −2 php/test.php
View
7 js/breakdown.js
@@ -47,19 +47,23 @@ var Breakdown = {
return input
.replace( /\*\*([^\*]+)\*\*/g, "<b>$1</b>" )
.replace( /\*([^\*]+)\*/g, "<i>$1</i>" )
- .replace( /\[image:([^\|\]]+)\|([^\]]+)\]/g, "<img src=\"$1\" alt=\"$2\">" )
+ .replace( /\[image:([^\|\]]+)\|([^\|\]]+)\|(right|left)\]/g, "<img src=\"$1\" alt=\"$2\" style=\"float:$3\">" )
+ .replace( /\[image:([^\|\]]+)\|(right|left)\]/g, "<img src=\"$1\" style=\"float:$2\">" )
+ .replace( /\[image:([^\|\]]+)\|([^\|\]]+)\]/g, "<img src=\"$1\" alt=\"$2\">" )
.replace( /\[image:([^\]]+)\]/g, "<img src=\"$1\">" )
.replace( /\[style:([^\|\]]+)\|([^\]]+)\]/g, "<div class=\"$1\">$2</div>" )
.replace( /\[include:([^\]]+)\]/g, insertInclude )
.replace( /\[([^\|\]]+)\|([^\]]+)\]/g, createLink )
.replace( /\[([^\]]+)\]/g, createLink )
.replace( /([^>"]+)(http:\/\/[a-zA-Z.]+)/g, "$1<a href=\"$2\">$2</a>" )
+ .replace( / ([a-zA-Z0-9\.\-_\+]+@[a-zA-Z0-9\.\-_]+)/g, " <a href=\"mailto:$1\">$1</a>" )
;
};
function createLink(m, url, label, offset, s) {
if( ! s ) { label = url; }
url = url.replace( / /g, '-' );
+ if( /^([a-zA-Z0-9\.\-_\+]+@[a-zA-Z0-9\.\-_]+)$/.test(url) ) { url = "mailto:" + url; }
return '<a href="' + url + '">' + label + '</a>';
}
@@ -99,6 +103,7 @@ var Breakdown = {
} else if( body.match( /^<li>/ ) ) {
return "<ul>\n" + body + "\n</ul>";
} else {
+ body = body.replace( / \n/g, "<br>\n" )
return "<p>" + body + "</p>";
}
};
View
14 js/test.html
@@ -42,10 +42,18 @@
and another one [http://christophe.vg]
and even another one http://christophe.vg
+Email addresses like somebody@somewhere.com are also automatically detected,
+but can also be enclosed by brackets: [somebody@somewhere.com].
+
[Links with spaces are cleaned up] ...
And a second paragraph.
+Within a paragraph a double space
+at the end of a line
+is rendered as a soft-break
+like in this paragrah.
+
---
## Bullets
@@ -63,9 +71,11 @@
[image:http://christophevg.github.com/christophe.vg/images/avatar.png|Christophe VG]
-even twice ...
+even aligned ...
-[image:http://christophevg.github.com/christophe.vg/images/avatar.png]
+[image:http://christophevg.github.com/christophe.vg/images/avatar.png|right]
+
+[image:http://christophevg.github.com/christophe.vg/images/avatar.png|Christophe VG]
and even with a link ...
View
21 php/breakdown.php
@@ -139,19 +139,35 @@ private function insertLink($matches) {
$url = $matches[1];
$label = count($matches) < 3 ? $url : $matches[2];
$url = str_replace( " ", "-", $url );
+ if( preg_match( '/([a-zA-Z0-9\.\-_\+]+@[a-zA-Z0-9\.\-_]+)/', $url ) ) {
+ $url = 'mailto:' . $url;
+ }
return "<a href=\"$url\">$label</a>";
}
+ private function insertEmail($matches) {
+ $url = $matches[1];
+ if( preg_match( '/^mailto:/', $url ) ) {
+ return $url;
+ } else {
+ return '<a href="mailto:' . $url . '">' . $url . '</a>';
+ }
+ }
+
// bold, italic, images and links can appear anywhere in-line the text
private function translateInlines($input) {
// bold, italic, images and styled blocks
$patterns = array( '/\*\*([^\*]+)\*\*/',
'/\*([^\*]+)\*/',
+ '/\[image:([^\|\]]+)\|([^\|\]]+)\|(left|right)\]/',
+ '/\[image:([^\|\]]+)\|(left|right)\]/',
'/\[image:([^\|\]]+)\|([^\]]+)\]/',
'/\[image:([^\]]+)\]/',
'/\[style:([^\|\]]+)\|([^\]]+)\]/' );
$replacements = array( '<b>\1</b>',
'<i>\1</i>',
+ '<img src="\1" alt="\2" style="float:\3">',
+ '<img src="\1" style="float:\2">',
'<img src="\1" alt="\2">',
'<img src="\1">',
'<div class="\1">\2</div>' );
@@ -167,6 +183,10 @@ private function translateInlines($input) {
$input = preg_replace( '/([^">])(http:\/\/[a-zA-Z.]+)/',
'\1<a href="\2">\2</a>',
$input );
+ // email links
+ $input = preg_replace_callback( '/([:a-zA-Z0-9\.\-_\+]+@[a-zA-Z0-9\.\-_]+)/',
+ array( $this, 'insertEmail' ),
+ $input );
// include support
$input = preg_replace_callback( '/\[include:([^\]]+)\]/',
@@ -206,6 +226,7 @@ function generateBlock($body) {
} else if( preg_match( '/^<li>/', $body ) ) {
return "<ul>\n$body\n</ul>";
} else {
+ $body = preg_replace( '/ \n/', "<br>\n", $body );
return "<p>$body</p>";
}
}
View
14 php/test.php
@@ -18,10 +18,18 @@
and another one [http://christophe.vg]
and even another one http://christophe.vg
+Email addresses like somebody@somewhere.com are also automatically detected,
+but can also be enclosed by brackets: [somebody@somewhere.com].
+
[Links with spaces are cleaned up] ...
And a second paragraph.
+Within a paragraph a double space
+at the end of a line
+is rendered as a soft-break
+like in this paragrah.
+
---
## Bullets
@@ -39,9 +47,11 @@
[image:http://christophevg.github.com/christophe.vg/images/avatar.png|Christophe VG]
-even twice ...
+even aligned ...
-[image:http://christophevg.github.com/christophe.vg/images/avatar.png]
+[image:http://christophevg.github.com/christophe.vg/images/avatar.png|right]
+
+[image:http://christophevg.github.com/christophe.vg/images/avatar.png|Christophe VG]
and even with a link ...

0 comments on commit e17bc3f

Please sign in to comment.