From d778e7bfb74ca04ac9e65effbe1e4d1de5b979ba Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Mon, 4 Jan 2016 01:00:19 +0100 Subject: [PATCH 01/23] API: Preparation for statuses/lookup support --- include/api.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/api.php b/include/api.php index 790894d3f..f1294c690 100644 --- a/include/api.php +++ b/include/api.php @@ -318,6 +318,7 @@ } } } + logger('API call not implemented: '.$a->query_string); throw new NotImplementedException(); } catch (HTTPException $e) { header("HTTP/1.1 {$e->httpcode} {$e->httpdesc}"); @@ -2576,6 +2577,7 @@ killme(); } } + api_register_func('api/gnusocial/version','api_statusnet_version',false); api_register_func('api/statusnet/version','api_statusnet_version',false); /** From bc517ef3d2cd4f134851ba23135d595f73e45ef8 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 22 Mar 2017 05:26:44 +0000 Subject: [PATCH 02/23] Remove reduncancies --- include/dfrn.php | 4 +++- include/diaspora.php | 9 +++------ include/discover_poco.php | 1 + include/ostatus.php | 7 +++---- include/socgraph.php | 12 ++++++++---- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/include/dfrn.php b/include/dfrn.php index 25f8c9358..0e3cdf76f 100644 --- a/include/dfrn.php +++ b/include/dfrn.php @@ -1352,7 +1352,9 @@ class dfrn { $poco["photo"] = $author["avatar"]; $poco["hide"] = $hide; $poco["contact-type"] = $contact["contact-type"]; - update_gcontact($poco); + $gcid = update_gcontact($poco); + + link_gcontact($gcid, $importer["uid"], $contact["id"]); } return($author); diff --git a/include/diaspora.php b/include/diaspora.php index eca22092d..841ba7e7f 100644 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -1848,18 +1848,15 @@ class Diaspora { intval($importer["uid"]) ); - if ($searchable) { - poco_check($contact["url"], $name, NETWORK_DIASPORA, $image_url, $about, $location, $gender, $keywords, "", - datetime_convert(), 2, $contact["id"], $importer["uid"]); - } - $gcontact = array("url" => $contact["url"], "network" => NETWORK_DIASPORA, "generation" => 2, "photo" => $image_url, "name" => $name, "location" => $location, "about" => $about, "birthday" => $birthday, "gender" => $gender, "addr" => $author, "nick" => $nick, "keywords" => $keywords, "hide" => !$searchable, "nsfw" => $nsfw); - update_gcontact($gcontact); + $gcid = update_gcontact($gcontact); + + link_gcontact($gcid, $importer["uid"], $contact["id"]); logger("Profile of contact ".$contact["id"]." stored for user ".$importer["uid"], LOGGER_DEBUG); diff --git a/include/discover_poco.php b/include/discover_poco.php index d203cfd6b..3dba1f321 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -266,6 +266,7 @@ function gs_search_user($search) { return false; } foreach($contacts->data AS $user) { + //update_gcontact_from_probe($user->site_address."/".$user->name); $contact = probe_url($user->site_address."/".$user->name); if ($contact["network"] != NETWORK_PHANTOM) { $contact["about"] = $user->description; diff --git a/include/ostatus.php b/include/ostatus.php index 2c4b677a5..931ea870f 100644 --- a/include/ostatus.php +++ b/include/ostatus.php @@ -132,9 +132,6 @@ class ostatus { dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["alias"]), dbesc($contact["about"]), dbesc($contact["location"]), dbesc(datetime_convert()), intval($contact["id"])); - - poco_check($contact["url"], $contact["name"], $contact["network"], $author["author-avatar"], $contact["about"], $contact["location"], - "", "", "", datetime_convert(), 2, $contact["id"], $contact["uid"]); } if (isset($author["author-avatar"]) AND ($author["author-avatar"] != $r[0]['avatar'])) { @@ -163,7 +160,9 @@ class ostatus { $contact["generation"] = 2; $contact["hide"] = false; // OStatus contacts are never hidden $contact["photo"] = $author["author-avatar"]; - update_gcontact($contact); + $gcid = update_gcontact($contact); + + link_gcontact($gcid, $contact["uid"], $contact["id"]); } return($author); diff --git a/include/socgraph.php b/include/socgraph.php index 71771bb95..2634c3ddc 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -357,14 +357,20 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca if(!$gcid) return $gcid; + link_gcontact($gcid, $uid, $cid, $zcid); + + return $gcid; +} + +function link_gcontact($gcid, $uid = 0, $cid = 0, $zcid = 0) { $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1", intval($cid), intval($uid), intval($gcid), intval($zcid) ); - if (! dbm::is_result($r)) { - q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ", + if (!dbm::is_result($r)) { + q("INSERT INTO `glink` (`cid`, `uid`, `gcid`, `zcid`, `updated`) VALUES (%d, %d, %d, %d, '%s') ", intval($cid), intval($uid), intval($gcid), @@ -380,8 +386,6 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca intval($zcid) ); } - - return $gcid; } function poco_reachable($profile, $server = "", $network = "", $force = false) { From 1d6910277dd5a8b0f1aa5f3653941a058c0dee63 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 22 Mar 2017 07:11:58 +0000 Subject: [PATCH 03/23] Remove more deprecated "poco_check" calls --- include/discover_poco.php | 6 +++++- mod/dirfind.php | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/discover_poco.php b/include/discover_poco.php index 3dba1f321..5a0ca70a4 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -232,7 +232,11 @@ function discover_directory($search) { if ($data["network"] == NETWORK_DFRN) { logger("Profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG); logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG); - poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0); + + if ($jj->tags != "") { + $data["keywords"] = $jj->tags; + } + update_gcontact($data); } else { logger("Profile ".$jj->url." is not responding or no Friendica contact - but network ".$data["network"], LOGGER_DEBUG); } diff --git a/mod/dirfind.php b/mod/dirfind.php index 1b19ad92c..c5844d13d 100644 --- a/mod/dirfind.php +++ b/mod/dirfind.php @@ -73,9 +73,9 @@ function dirfind_content(App $a, $prefix = "") { $j->results[] = $objresult; // Add the contact to the global contacts if it isn't already in our system - if (($contact["cid"] == 0) AND ($contact["zid"] == 0) AND ($contact["gid"] == 0)) - poco_check($user_data["url"], $user_data["name"], $user_data["network"], $user_data["photo"], - "", "", "", "", "", datetime_convert(), 0); + if (($contact["cid"] == 0) AND ($contact["zid"] == 0) AND ($contact["gid"] == 0)) { + update_gcontact($user_data); + } } elseif ($local) { if ($community) From 8e240a1e8cb9785de6d224751bb38acf8f9fc566 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 22 Mar 2017 23:13:32 +0000 Subject: [PATCH 04/23] New function to validate gcontact array data --- include/socgraph.php | 175 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 4 deletions(-) diff --git a/include/socgraph.php b/include/socgraph.php index 2634c3ddc..3d72286ea 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -190,6 +190,159 @@ function poco_load_worker($cid, $uid, $zcid, $url) { ); } +/** + * @brief Validate the given gcontact data + * + * @param array $gcontact array with gcontact data + * @param integer $cid Contact ID + * @param integer $uid User ID + * @param integer $zcid Global Contact ID + * + * Generation: + * 0: No definition + * 1: Profiles on this server + * 2: Contacts of profiles on this server + * 3: Contacts of contacts of profiles on this server + * 4: ... + * + */ +function validate_gcontact(&$gcontact) { + + if ($gcontact['url'] == "") { + return false; + } + + $urlparts = parse_url($gcontact['url']); + if (!isset($urlparts["scheme"])) { + return false; + } + + if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com", + "identi.ca", "alpha.app.net"))) { + return false; + } + + // Don't store the statusnet connector as network + // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well + if ($gcontact['network'] == NETWORK_STATUSNET) { + $gcontact['network'] = ""; + } + + // Assure that there are no parameter fragments in the profile url + if (in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) { + $gcontact['url'] = clean_contact_url($gcontact['url']); + } + + $alternate = poco_alternate_ostatus_url($gcontact['url']); + + // The global contacts should contain the original picture, not the cached one + if (($gcontact['generation'] != 1) AND stristr(normalise_link($gcontact['photo']), normalise_link(App::get_baseurl()."/photo/"))) { + $gcontact['photo'] = ""; + } + + if (!isset($gcontact['network'])) { + $r = q("SELECT `network` FROM `contact` WHERE `uid` = 0 AND `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1", + dbesc(normalise_link($gcontact['url'])), dbesc(NETWORK_STATUSNET) + ); + if (dbm::is_result($r)) { + $gcontact['network'] = $r[0]["network"]; + } + + if (($gcontact['network'] == "") OR ($gcontact['network'] == NETWORK_OSTATUS)) { + $r = q("SELECT `network`, `url` FROM `contact` WHERE `uid` = 0 AND `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1", + dbesc($gcontact['url']), dbesc(normalise_link($gcontact['url'])), dbesc(NETWORK_STATUSNET) + ); + if (dbm::is_result($r)) { + $gcontact['network'] = $r[0]["network"]; + } + } + } + + $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1", + dbesc(normalise_link($gcontact['url'])) + ); + + if (count($x)) { + if (!isset($gcontact['network']) AND ($x[0]["network"] != NETWORK_STATUSNET)) { + $gcontact['network'] = $x[0]["network"]; + } + if ($gcontact['updated'] <= NULL_DATE) { + $gcontact['updated'] = $x[0]["updated"]; + } + if (!isset($gcontact['server_url'])) { + $gcontact['server_url'] = $x[0]["server_url"]; + } + if (!isset($gcontact['addr'])) { + $gcontact['addr'] = $x[0]["addr"]; + } + } else { + if (!isset($gcontact['server_url'])) { + $gcontact['server_url'] = ''; + } + if (!isset($gcontact['network'])) { + $gcontact['network'] = ''; + } + } + + if ((!isset($gcontact['network']) OR !isset($gcontact['name']) OR !isset($gcontact['addr']) OR !isset($gcontact['photo']) OR !isset($gcontact['server_url']) OR $alternate) + AND poco_reachable($gcontact['url'], $gcontact['server_url'], $gcontact['network'], false)) { + $data = Probe::uri($gcontact['url']); + + if ($data["network"] == NETWORK_PHANTOM) { + return false; + } + + $orig_profile = $gcontact['url']; + + $gcontact["server_url"] = $data["baseurl"]; + + unset($data["guid"]); + unset($data["batch"]); + unset($data["poll"]); + unset($data["request"]); + unset($data["confirm"]); + unset($data["poco"]); + unset($data["priority"]); + unset($data["pubkey"]); + unset($data["baseurl"]); + + $gcontact = array_merge($gcontact, $data); + + if ($alternate AND ($gcontact['network'] == NETWORK_OSTATUS)) { + // Delete the old entry - if it exists + $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile))); + if ($r) { + q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile))); + q("DELETE FROM `glink` WHERE `gcid` = %d", intval($r[0]["id"])); + } + } + } + + if (!isset($gcontact['name']) OR !isset($gcontact['photo'])) { + return false; + } + + if (!in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) { + return false; + } + + if (!isset($gcontact['server_url'])) { + // We check the server url to be sure that it is a real one + $server_url = poco_detect_server($gcontact['url']); + + // We are now sure that it is a correct URL. So we use it in the future + if ($server_url != "") { + $gcontact['server_url'] = $server_url; + } + } + + // The server URL doesn't seem to be valid, so we don't store it. + if (!poco_check_server($gcontact['server_url'], $gcontact['network'])) { + $gcontact['server_url'] = ""; + } + + return true; +} function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) { @@ -604,7 +757,7 @@ function poco_last_updated($profile, $force = false) { return $gcontacts[0]["updated"]; } - $data = probe_url($profile); + $data = Probe::uri($profile); // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711) // Then check the other link and delete this one @@ -639,6 +792,7 @@ function poco_last_updated($profile, $force = false) { $contact["server_url"] = $data["baseurl"]; + unset($contact["guid"]); unset($contact["batch"]); unset($contact["poll"]); unset($contact["request"]); @@ -1893,10 +2047,23 @@ function poco_discover_server($data, $default_generation = 0) { $success = true; logger("Store profile ".$profile_url, LOGGER_DEBUG); - poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, 0, 0, 0); - $gcontact = array("url" => $profile_url, "contact-type" => $contact_type, "generation" => $generation); - update_gcontact($gcontact); + $gcontact = array("url" => $profile_url, + "name" => $name, + "network" => $network, + "photo" => $profile_photo, + "about" => $about, + "location" => $location, + "gender" => $gender, + "keywords" => $keywords, + "connect" => $connect_url, + "updated" => $updated, + "contact-type" => $contact_type, + "generation" => $generation); + + if (validate_gcontact($gcontact)) { + update_gcontact($gcontact); + } logger("Done for profile ".$profile_url, LOGGER_DEBUG); } From 9e5bec1bc884f100139988af3d8166de6902deca Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 23 Mar 2017 06:23:23 +0000 Subject: [PATCH 05/23] "poco_check" is now replaced and removed --- include/socgraph.php | 248 +++++++++---------------------------------- 1 file changed, 49 insertions(+), 199 deletions(-) diff --git a/include/socgraph.php b/include/socgraph.php index 3d72286ea..cd971a7c7 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -159,27 +159,24 @@ function poco_load_worker($cid, $uid, $zcid, $url) { if (isset($entry->contactType) AND ($entry->contactType >= 0)) $contact_type = $entry->contactType; - // If you query a Friendica server for its profiles, the network has to be Friendica - /// TODO It could also be a Redmatrix server - //if ($uid == 0) - // $network = NETWORK_DFRN; + $gcontact = array("url" => $profile_url, + "name" => $name, + "network" => $network, + "photo" => $profile_photo, + "about" => $about, + "location" => $location, + "gender" => $gender, + "keywords" => $keywords, + "connect" => $connect_url, + "updated" => $updated, + "contact-type" => $contact_type, + "generation" => $generation); - poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid); + if (sanitized_gcontact($gcontact)) { + $gcid = update_gcontact($gcontact); - $gcontact = array("url" => $profile_url, "contact-type" => $contact_type, "generation" => $generation); - update_gcontact($gcontact); - - // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php) - // Deactivated because we now update Friendica contacts in dfrn.php - //if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != "")) - // q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' - // WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'", - // dbesc($location), - // dbesc($about), - // dbesc($keywords), - // dbesc($gender), - // dbesc(normalise_link($profile_url)), - // dbesc(NETWORK_DFRN)); + link_gcontact($gcid, $uid, $cid, $zcid); + } } logger("poco_load: loaded $total entries",LOGGER_DEBUG); @@ -191,12 +188,9 @@ function poco_load_worker($cid, $uid, $zcid, $url) { } /** - * @brief Validate the given gcontact data + * @brief Sanitize the given gcontact data * * @param array $gcontact array with gcontact data - * @param integer $cid Contact ID - * @param integer $uid User ID - * @param integer $zcid Global Contact ID * * Generation: * 0: No definition @@ -206,7 +200,7 @@ function poco_load_worker($cid, $uid, $zcid, $url) { * 4: ... * */ -function validate_gcontact(&$gcontact) { +function sanitized_gcontact(&$gcontact) { if ($gcontact['url'] == "") { return false; @@ -344,178 +338,21 @@ function validate_gcontact(&$gcontact) { return true; } -function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) { - - // Generation: - // 0: No definition - // 1: Profiles on this server - // 2: Contacts of profiles on this server - // 3: Contacts of contacts of profiles on this server - // 4: ... - - $gcid = ""; - - if ($profile_url == "") - return $gcid; - - $urlparts = parse_url($profile_url); - if (!isset($urlparts["scheme"])) - return $gcid; - - if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com", - "identi.ca", "alpha.app.net"))) - return $gcid; - - // Don't store the statusnet connector as network - // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well - if ($network == NETWORK_STATUSNET) - $network = ""; - - // Assure that there are no parameter fragments in the profile url - if (in_array($network, array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) - $profile_url = clean_contact_url($profile_url); - - $alternate = poco_alternate_ostatus_url($profile_url); - - $orig_updated = $updated; - - // The global contacts should contain the original picture, not the cached one - if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link(App::get_baseurl()."/photo/"))) { - $profile_photo = ""; - } - - $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1", - dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET) - ); - if (dbm::is_result($r)) { - $network = $r[0]["network"]; - } - - if (($network == "") OR ($network == NETWORK_OSTATUS)) { - $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1", - dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET) - ); - if (dbm::is_result($r)) { - $network = $r[0]["network"]; - //$profile_url = $r[0]["url"]; - } - } - - $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1", - dbesc(normalise_link($profile_url)) - ); - - if (count($x)) { - if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET)) { - $network = $x[0]["network"]; - } - if ($updated <= NULL_DATE) { - $updated = $x[0]["updated"]; - } - $created = $x[0]["created"]; - $server_url = $x[0]["server_url"]; - $nick = $x[0]["nick"]; - $addr = $x[0]["addr"]; - $alias = $x[0]["alias"]; - $notify = $x[0]["notify"]; - } else { - $created = NULL_DATE; - $server_url = ""; - - $urlparts = parse_url($profile_url); - $nick = end(explode("/", $urlparts["path"])); - $addr = ""; - $alias = ""; - $notify = ""; - } - - if ((($network == "") OR ($name == "") OR ($addr == "") OR ($profile_photo == "") OR ($server_url == "") OR $alternate) - AND poco_reachable($profile_url, $server_url, $network, false)) { - $data = probe_url($profile_url); - - $orig_profile = $profile_url; - - $network = $data["network"]; - $name = $data["name"]; - $nick = $data["nick"]; - $addr = $data["addr"]; - $alias = $data["alias"]; - $notify = $data["notify"]; - $profile_url = $data["url"]; - $profile_photo = $data["photo"]; - $server_url = $data["baseurl"]; - - if ($alternate AND ($network == NETWORK_OSTATUS)) { - // Delete the old entry - if it exists - $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile))); - if ($r) { - q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile))); - q("DELETE FROM `glink` WHERE `gcid` = %d", intval($r[0]["id"])); - } - - // possibly create a new entry - poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid); - } - } - - if ($alternate AND ($network == NETWORK_OSTATUS)) - return $gcid; - - if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) { - q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'", - dbesc($network), - dbesc(normalise_link($profile_url)) - ); - } - - if (($name == "") OR ($profile_photo == "")) - return $gcid; - - if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) - return $gcid; - - logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG); - - // We check the server url to be sure that it is a real one - $server_url2 = poco_detect_server($profile_url); - - // We are no sure that it is a correct URL. So we use it in the future - if ($server_url2 != "") { - $server_url = $server_url2; - } - - // The server URL doesn't seem to be valid, so we don't store it. - if (!poco_check_server($server_url, $network)) { - $server_url = ""; - } - - $gcontact = array("url" => $profile_url, - "addr" => $addr, - "alias" => $alias, - "name" => $name, - "network" => $network, - "photo" => $profile_photo, - "about" => $about, - "location" => $location, - "gender" => $gender, - "keywords" => $keywords, - "server_url" => $server_url, - "connect" => $connect_url, - "notify" => $notify, - "updated" => $updated, - "generation" => $generation); - - $gcid = update_gcontact($gcontact); - - if(!$gcid) - return $gcid; - - link_gcontact($gcid, $uid, $cid, $zcid); - - return $gcid; -} - +/** + * @brief Link the gcontact entry with user, contact and global contact + * + * @param integer $gcid Global contact ID + * @param integer $cid Contact ID + * @param integer $uid User ID + * @param integer $zcid Global Contact ID + * * + */ function link_gcontact($gcid, $uid = 0, $cid = 0, $zcid = 0) { + + if ($gcid <= 0) { + return; + } + $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1", intval($cid), intval($uid), @@ -769,10 +606,23 @@ function poco_last_updated($profile, $force = false) { q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile))); q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"])); - poco_check($data["url"], $data["name"], $data["network"], $data["photo"], $gcontacts[0]["about"], $gcontacts[0]["location"], - $gcontacts[0]["gender"], $gcontacts[0]["keywords"], $data["addr"], $gcontacts[0]["updated"], $gcontacts[0]["generation"]); + $gcontact = array_merge($gcontacts[0], $data); - poco_last_updated($data["url"], $force); + unset($gcontact["guid"]); + unset($gcontact["batch"]); + unset($gcontact["poll"]); + unset($gcontact["request"]); + unset($gcontact["confirm"]); + unset($gcontact["poco"]); + unset($gcontact["priority"]); + unset($gcontact["pubkey"]); + unset($gcontact["baseurl"]); + + if (sanitized_gcontact($gcontact)) { + update_gcontact($gcontact); + + poco_last_updated($data["url"], $force); + } logger("Profile ".$profile." was deleted", LOGGER_DEBUG); return false; @@ -2061,7 +1911,7 @@ function poco_discover_server($data, $default_generation = 0) { "contact-type" => $contact_type, "generation" => $generation); - if (validate_gcontact($gcontact)) { + if (sanitized_gcontact($gcontact)) { update_gcontact($gcontact); } From 26ec56dfa58d8d20cd6ed06801d77ac857bf1c19 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 23 Mar 2017 06:45:00 +0000 Subject: [PATCH 06/23] server_url was forgotten --- include/discover_poco.php | 3 +++ include/socgraph.php | 32 +++----------------------------- 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/include/discover_poco.php b/include/discover_poco.php index 5a0ca70a4..4c52fdd3e 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -236,6 +236,9 @@ function discover_directory($search) { if ($jj->tags != "") { $data["keywords"] = $jj->tags; } + + $data["server_url"] = $data["baseurl"]; + update_gcontact($data); } else { logger("Profile ".$jj->url." is not responding or no Friendica contact - but network ".$data["network"], LOGGER_DEBUG); diff --git a/include/socgraph.php b/include/socgraph.php index cd971a7c7..db843614a 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -290,16 +290,6 @@ function sanitized_gcontact(&$gcontact) { $gcontact["server_url"] = $data["baseurl"]; - unset($data["guid"]); - unset($data["batch"]); - unset($data["poll"]); - unset($data["request"]); - unset($data["confirm"]); - unset($data["poco"]); - unset($data["priority"]); - unset($data["pubkey"]); - unset($data["baseurl"]); - $gcontact = array_merge($gcontact, $data); if ($alternate AND ($gcontact['network'] == NETWORK_OSTATUS)) { @@ -608,15 +598,7 @@ function poco_last_updated($profile, $force = false) { $gcontact = array_merge($gcontacts[0], $data); - unset($gcontact["guid"]); - unset($gcontact["batch"]); - unset($gcontact["poll"]); - unset($gcontact["request"]); - unset($gcontact["confirm"]); - unset($gcontact["poco"]); - unset($gcontact["priority"]); - unset($gcontact["pubkey"]); - unset($gcontact["baseurl"]); + $gcontact["server_url"] = $data["baseurl"]; if (sanitized_gcontact($gcontact)) { update_gcontact($gcontact); @@ -642,16 +624,6 @@ function poco_last_updated($profile, $force = false) { $contact["server_url"] = $data["baseurl"]; - unset($contact["guid"]); - unset($contact["batch"]); - unset($contact["poll"]); - unset($contact["request"]); - unset($contact["confirm"]); - unset($contact["poco"]); - unset($contact["priority"]); - unset($contact["pubkey"]); - unset($contact["baseurl"]); - update_gcontact($contact); $feedret = z_fetch_url($data["poll"]); @@ -2208,6 +2180,8 @@ function update_gcontact_from_probe($url) { return; } + $data["server_url"] = $data["baseurl"]; + update_gcontact($data); } From c25af197c9d6436286351a76e7f1be183f1f9265 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 23 Mar 2017 07:10:22 +0000 Subject: [PATCH 07/23] baseurl should now be set correctly at all the time. --- include/Probe.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/include/Probe.php b/include/Probe.php index 1b6feb107..929e89a68 100644 --- a/include/Probe.php +++ b/include/Probe.php @@ -18,6 +18,8 @@ require_once('include/network.php'); */ class Probe { + private static $baseurl; + /** * @brief Rearrange the array so that it always has the same order * @@ -57,6 +59,8 @@ class Probe { $ssl_url = "https://".$host."/.well-known/host-meta"; $url = "http://".$host."/.well-known/host-meta"; + $baseurl = "http://".$host; + $xrd_timeout = Config::get('system','xrd_timeout', 20); $redirects = 0; @@ -102,6 +106,9 @@ class Probe { elseif ($attributes["rel"] == "lrdd") $xrd_data["lrdd"] = $attributes["template"]; } + + self::$baseurl = $baseurl; + return $xrd_data; } @@ -258,8 +265,13 @@ class Probe { $data['nick'] = trim(substr($data['nick'], 0, strpos($data['nick'], ' '))); } - if (!isset($data["network"])) + if (self::$baseurl != "") { + $data["baseurl"] = self::$baseurl; + } + + if (!isset($data["network"])) { $data["network"] = NETWORK_PHANTOM; + } $data = self::rearrange_data($data); @@ -286,6 +298,7 @@ class Probe { dbesc(normalise_link($data['url'])) ); } + return $data; } From 5b6215d7bd4347b0f1b6043e115fd5802b862666 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 23 Mar 2017 07:34:45 +0000 Subject: [PATCH 08/23] Better server_url handling --- include/discover_poco.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/include/discover_poco.php b/include/discover_poco.php index 4c52fdd3e..f5cdd1b10 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -165,12 +165,17 @@ function discover_users() { continue; } + $server_url = poco_detect_server($user["url"]); + $force_update = false; + if ($user["server_url"] != "") { + + $force_update = (normalise_link($user["server_url"]) != normalise_link($server_url)); + $server_url = $user["server_url"]; - } else { - $server_url = poco_detect_server($user["url"]); } - if ((($server_url == "") AND ($user["network"] == NETWORK_FEED)) OR poco_check_server($server_url, $user["network"])) { + + if ((($server_url == "") AND ($user["network"] == NETWORK_FEED)) OR $force_update OR poco_check_server($server_url, $user["network"])) { logger('Check profile '.$user["url"]); proc_run(PRIORITY_LOW, "include/discover_poco.php", "check_profile", base64_encode($user["url"])); From 2ce5ab1467de69ca2a75504013750997dd329f84 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 23 Mar 2017 07:36:22 +0000 Subject: [PATCH 09/23] removed commented out line --- include/discover_poco.php | 1 - 1 file changed, 1 deletion(-) diff --git a/include/discover_poco.php b/include/discover_poco.php index f5cdd1b10..bb53efb5c 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -278,7 +278,6 @@ function gs_search_user($search) { return false; } foreach($contacts->data AS $user) { - //update_gcontact_from_probe($user->site_address."/".$user->name); $contact = probe_url($user->site_address."/".$user->name); if ($contact["network"] != NETWORK_PHANTOM) { $contact["about"] = $user->description; From b7242c3fa0816f2e53cb35a40c0d18823aa8b2ca Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 23 Mar 2017 22:56:22 +0000 Subject: [PATCH 10/23] poco_last_updated is optimized --- include/socgraph.php | 89 +++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/include/socgraph.php b/include/socgraph.php index db843614a..ec0f3854d 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -252,6 +252,9 @@ function sanitized_gcontact(&$gcontact) { } } + $gcontact['server_url'] = ''; + $gcontact['network'] = ''; + $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1", dbesc(normalise_link($gcontact['url'])) ); @@ -263,19 +266,12 @@ function sanitized_gcontact(&$gcontact) { if ($gcontact['updated'] <= NULL_DATE) { $gcontact['updated'] = $x[0]["updated"]; } - if (!isset($gcontact['server_url'])) { + if (!isset($gcontact['server_url']) AND (normalise_link($x[0]["server_url"]) != normalise_link($x[0]["url"]))) { $gcontact['server_url'] = $x[0]["server_url"]; } if (!isset($gcontact['addr'])) { $gcontact['addr'] = $x[0]["addr"]; } - } else { - if (!isset($gcontact['server_url'])) { - $gcontact['server_url'] = ''; - } - if (!isset($gcontact['network'])) { - $gcontact['network'] = ''; - } } if ((!isset($gcontact['network']) OR !isset($gcontact['name']) OR !isset($gcontact['addr']) OR !isset($gcontact['photo']) OR !isset($gcontact['server_url']) OR $alternate) @@ -463,15 +459,26 @@ function poco_last_updated($profile, $force = false) { $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile))); - if ($gcontacts[0]["created"] <= NULL_DATE) { - q("UPDATE `gcontact` SET `created` = '%s' WHERE `nurl` = '%s'", - dbesc(datetime_convert()), dbesc(normalise_link($profile))); + if (!dbm::is_result($gcontacts)) { + return false; } - if ($gcontacts[0]["server_url"] != "") { + + $contact = array("url" => $profile); + + if ($gcontacts[0]["created"] <= NULL_DATE) { + $contact['created'] = datetime_convert(); + } + + if ($force) { + $server_url = normalise_link(poco_detect_server($profile)); + } + + if (($server_url == '') AND ($gcontacts[0]["server_url"] != "")) { $server_url = $gcontacts[0]["server_url"]; } - if (($server_url == '') OR ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"])) { - $server_url = poco_detect_server($profile); + + if (!$force AND (($server_url == '') OR ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) { + $server_url = normalise_link(poco_detect_server($profile)); } if (!in_array($gcontacts[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""))) { @@ -481,67 +488,64 @@ function poco_last_updated($profile, $force = false) { if ($server_url != "") { if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) { - - if ($force) + if ($force) { q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($profile))); + } logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG); return false; } - - q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'", - dbesc($server_url), dbesc(normalise_link($profile))); + $contact['server_url'] = $server_url; } if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) { $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''", dbesc(normalise_link($server_url))); - if ($server) - q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'", - dbesc($server[0]["network"]), dbesc(normalise_link($profile))); - else + if ($server) { + $contact['network'] = $server[0]["network"]; + } else { return false; + } } // noscrape is really fast so we don't cache the call. - if (($gcontacts[0]["server_url"] != "") AND ($gcontacts[0]["nick"] != "")) { + if (($server_url != "") AND ($gcontacts[0]["nick"] != "")) { // Use noscrape if possible - $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($gcontacts[0]["server_url"]))); + $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($server_url))); if ($server) { $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]); - if ($noscraperet["success"] AND ($noscraperet["body"] != "")) { + if ($noscraperet["success"] AND ($noscraperet["body"] != "")) { $noscrape = json_decode($noscraperet["body"], true); if (is_array($noscrape)) { - $contact = array("url" => $profile, - "network" => $server[0]["network"], - "generation" => $gcontacts[0]["generation"]); + $contact["network"] = $server[0]["network"]; - if (isset($noscrape["fn"])) + if (isset($noscrape["fn"])) { $contact["name"] = $noscrape["fn"]; - - if (isset($noscrape["comm"])) + } + if (isset($noscrape["comm"])) { $contact["community"] = $noscrape["comm"]; - + } if (isset($noscrape["tags"])) { $keywords = implode(" ", $noscrape["tags"]); - if ($keywords != "") + if ($keywords != "") { $contact["keywords"] = $keywords; + } } $location = formatted_location($noscrape); - if ($location) + if ($location) { $contact["location"] = $location; - - if (isset($noscrape["dfrn-notify"])) + } + if (isset($noscrape["dfrn-notify"])) { $contact["notify"] = $noscrape["dfrn-notify"]; - + } // Remove all fields that are not present in the gcontact table unset($noscrape["fn"]); unset($noscrape["key"]); @@ -579,8 +583,10 @@ function poco_last_updated($profile, $force = false) { } // If we only can poll the feed, then we only do this once a while - if (!$force AND !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) { + if (!$force AND !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) { logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG); + + update_gcontact($contact); return $gcontacts[0]["updated"]; } @@ -618,8 +624,6 @@ function poco_last_updated($profile, $force = false) { return false; } - $contact = array("generation" => $gcontacts[0]["generation"]); - $contact = array_merge($contact, $data); $contact["server_url"] = $data["baseurl"]; @@ -666,9 +670,10 @@ function poco_last_updated($profile, $force = false) { q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc(dbm::date($last_updated)), dbesc(dbm::date()), dbesc(normalise_link($profile))); - if (($gcontacts[0]["generation"] == 0)) + if (($gcontacts[0]["generation"] == 0)) { q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'", dbesc(normalise_link($profile))); + } logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG); From c9214c20519d660b28cf97fb1186ec9161c7c4c0 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 23 Mar 2017 21:19:24 -0400 Subject: [PATCH 11/23] Upgraded PHP Markdown library to version 1.7.0 --- library/php-markdown/License.md | 6 +- library/php-markdown/Michelf/Markdown.inc.php | 10 +- library/php-markdown/Michelf/Markdown.php | 3009 +++++------------ .../Michelf/MarkdownExtra.inc.php | 10 +- .../php-markdown/Michelf/MarkdownExtra.php | 1803 +++++++++- .../Michelf/MarkdownInterface.inc.php | 10 +- .../Michelf/MarkdownInterface.php | 60 +- library/php-markdown/Readme.md | 103 +- library/php-markdown/Readme.php | 14 +- library/php-markdown/composer.json | 11 +- 10 files changed, 2798 insertions(+), 2238 deletions(-) diff --git a/library/php-markdown/License.md b/library/php-markdown/License.md index 8a5b85447..c16197b69 100644 --- a/library/php-markdown/License.md +++ b/library/php-markdown/License.md @@ -1,11 +1,11 @@ PHP Markdown Lib -Copyright (c) 2004-2014 Michel Fortin - +Copyright (c) 2004-2016 Michel Fortin + All rights reserved. Based on Markdown Copyright (c) 2003-2006 John Gruber - + All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/library/php-markdown/Michelf/Markdown.inc.php b/library/php-markdown/Michelf/Markdown.inc.php index 8c281094c..e2bd3808e 100644 --- a/library/php-markdown/Michelf/Markdown.inc.php +++ b/library/php-markdown/Michelf/Markdown.inc.php @@ -1,10 +1,10 @@ -# -# Original Markdown -# Copyright (c) 2004-2006 John Gruber -# -# +/** + * Markdown - A text-to-HTML conversion tool for web writers + * + * @package php-markdown + * @author Michel Fortin + * @copyright 2004-2016 Michel Fortin + * @copyright (Original Markdown) 2004-2006 John Gruber + */ + namespace Michelf; - -# -# Markdown Parser Class -# - +/** + * Markdown Parser Class + */ class Markdown implements MarkdownInterface { + /** + * Define the package version + * @var string + */ + const MARKDOWNLIB_VERSION = "1.7.0"; - ### Version ### - - const MARKDOWNLIB_VERSION = "1.4.1"; - - ### Simple Function Interface ### - + /** + * Simple function interface - Initialize the parser and return the result + * of its transform method. This will work fine for derived classes too. + * + * @api + * + * @param string $text + * @return string + */ public static function defaultTransform($text) { - # - # Initialize the parser and return the result of its transform method. - # This will work fine for derived classes too. - # - # Take parser class on which this function was called. + // Take parser class on which this function was called. $parser_class = \get_called_class(); - # try to take parser from the static parser list + // Try to take parser from the static parser list static $parser_list; $parser =& $parser_list[$parser_class]; - # create the parser it not already set - if (!$parser) + // Create the parser it not already set + if (!$parser) { $parser = new $parser_class; + } - # Transform text using parser. + // Transform text using parser. return $parser->transform($text); } - ### Configuration Variables ### + /** + * Configuration variables + */ - # Change to ">" for HTML output. + /** + * Change to ">" for HTML output. + * @var string + */ public $empty_element_suffix = " />"; + + /** + * The width of indentation of the output markup + * @var int + */ public $tab_width = 4; - - # Change to `true` to disallow markup or entities. - public $no_markup = false; + + /** + * Change to `true` to disallow markup or entities. + * @var boolean + */ + public $no_markup = false; public $no_entities = false; - - # Predefined urls and titles for reference links and images. - public $predef_urls = array(); + + + /** + * Change to `true` to enable line breaks on \n without two trailling spaces + * @var boolean + */ + public $hard_wrap = false; + + /** + * Predefined URLs and titles for reference links and images. + * @var array + */ + public $predef_urls = array(); public $predef_titles = array(); - # Optional filter function for URLs + /** + * Optional filter function for URLs + * @var callable + */ public $url_filter_func = null; + /** + * Optional header id="" generation callback function. + * @var callable + */ + public $header_id_func = null; - ### Parser Implementation ### + /** + * Optional function for converting code block content to HTML + * @var callable + */ + public $code_block_content_func = null; - # Regex to match balanced [brackets]. - # Needed to insert a maximum bracked depth while converting to PHP. + /** + * Optional function for converting code span content to HTML. + * @var callable + */ + public $code_span_content_func = null; + + /** + * Class attribute to toggle "enhanced ordered list" behaviour + * setting this to true will allow ordered lists to start from the index + * number that is defined first. + * + * For example: + * 2. List item two + * 3. List item three + * + * Becomes: + *
    + *
  1. List item two
  2. + *
  3. List item three
  4. + *
+ * + * @var bool + */ + public $enhanced_ordered_list = false; + + /** + * Parser implementation + */ + + /** + * Regex to match balanced [brackets]. + * Needed to insert a maximum bracked depth while converting to PHP. + * @var int + */ protected $nested_brackets_depth = 6; protected $nested_brackets_re; - + protected $nested_url_parenthesis_depth = 4; protected $nested_url_parenthesis_re; - # Table of hash values for escaped characters: + /** + * Table of hash values for escaped characters: + * @var string + */ protected $escape_chars = '\`*_{}[]()>#+-.!'; protected $escape_chars_re; - + /** + * Constructor function. Initialize appropriate member variables. + * @return void + */ public function __construct() { - # - # Constructor function. Initialize appropriate member variables. - # $this->_initDetab(); $this->prepareItalicsAndBold(); - - $this->nested_brackets_re = + + $this->nested_brackets_re = str_repeat('(?>[^\[\]]+|\[', $this->nested_brackets_depth). str_repeat('\])*', $this->nested_brackets_depth); - - $this->nested_url_parenthesis_re = + + $this->nested_url_parenthesis_re = str_repeat('(?>[^()\s]+|\(', $this->nested_url_parenthesis_depth). str_repeat('(?>\)))*', $this->nested_url_parenthesis_depth); - + $this->escape_chars_re = '['.preg_quote($this->escape_chars).']'; - - # Sort document, block, and span gamut in ascendent priority order. + + // Sort document, block, and span gamut in ascendent priority order. asort($this->document_gamut); asort($this->block_gamut); asort($this->span_gamut); } - # Internal hashes used during transformation. - protected $urls = array(); - protected $titles = array(); + /** + * Internal hashes used during transformation. + * @var array + */ + protected $urls = array(); + protected $titles = array(); protected $html_hashes = array(); - - # Status flag to avoid invalid nesting. + + /** + * Status flag to avoid invalid nesting. + * @var boolean + */ protected $in_anchor = false; - - + + /** + * Called before the transformation process starts to setup parser states. + * @return void + */ protected function setup() { - # - # Called before the transformation process starts to setup parser - # states. - # - # Clear global hashes. - $this->urls = $this->predef_urls; - $this->titles = $this->predef_titles; + // Clear global hashes. + $this->urls = $this->predef_urls; + $this->titles = $this->predef_titles; $this->html_hashes = array(); - - $this->in_anchor = false; + $this->in_anchor = false; } - + + /** + * Called after the transformation process to clear any variable which may + * be taking up memory unnecessarly. + * @return void + */ protected function teardown() { - # - # Called after the transformation process to clear any variable - # which may be taking up memory unnecessarly. - # - $this->urls = array(); - $this->titles = array(); + $this->urls = array(); + $this->titles = array(); $this->html_hashes = array(); } - + /** + * Main function. Performs some preprocessing on the input text and pass + * it through the document gamut. + * + * @api + * + * @param string $text + * @return string + */ public function transform($text) { - # - # Main function. Performs some preprocessing on the input text - # and pass it through the document gamut. - # $this->setup(); - + # Remove UTF-8 BOM and marker character in input, if present. $text = preg_replace('{^\xEF\xBB\xBF|\x1A}', '', $text); @@ -168,28 +248,33 @@ class Markdown implements MarkdownInterface { foreach ($this->document_gamut as $method => $priority) { $text = $this->$method($text); } - + $this->teardown(); return $text . "\n"; } - + + /** + * Define the document gamut + * @var array + */ protected $document_gamut = array( - # Strip link definitions, store in hashes. + // Strip link definitions, store in hashes. "stripLinkDefinitions" => 20, - "runBasicBlockGamut" => 30, - ); - + ); + /** + * Strips link definitions from text, stores the URLs and titles in + * hash references + * @param string $text + * @return string + */ protected function stripLinkDefinitions($text) { - # - # Strips link definitions from text, stores the URLs and titles in - # hash references. - # + $less_than_tab = $this->tab_width - 1; - # Link defs are in the form: ^[id]: url "optional title" + // Link defs are in the form: ^[id]: url "optional title" $text = preg_replace_callback('{ ^[ ]{0,'.$less_than_tab.'}\[(.+)\][ ]?: # id = $1 [ ]* @@ -213,43 +298,58 @@ class Markdown implements MarkdownInterface { (?:\n+|\Z) }xm', array($this, '_stripLinkDefinitions_callback'), - $text); + $text + ); return $text; } + + /** + * The callback to strip link definitions + * @param array $matches + * @return string + */ protected function _stripLinkDefinitions_callback($matches) { $link_id = strtolower($matches[1]); $url = $matches[2] == '' ? $matches[3] : $matches[2]; $this->urls[$link_id] = $url; $this->titles[$link_id] =& $matches[4]; - return ''; # String that will replace the block + return ''; // String that will replace the block } - + /** + * Hashify HTML blocks + * @param string $text + * @return string + */ protected function hashHTMLBlocks($text) { - if ($this->no_markup) return $text; + if ($this->no_markup) { + return $text; + } $less_than_tab = $this->tab_width - 1; - # Hashify HTML blocks: - # We only want to do this for block-level HTML tags, such as headers, - # lists, and tables. That's because we still want to wrap

s around - # "paragraphs" that are wrapped in non-block-level tags, such as anchors, - # phrase emphasis, and spans. The list of tags we're looking for is - # hard-coded: - # - # * List "a" is made of tags which can be both inline or block-level. - # These will be treated block-level when the start tag is alone on - # its line, otherwise they're not matched here and will be taken as - # inline later. - # * List "b" is made of tags which are always block-level; - # + /** + * Hashify HTML blocks: + * + * We only want to do this for block-level HTML tags, such as headers, + * lists, and tables. That's because we still want to wrap

s around + * "paragraphs" that are wrapped in non-block-level tags, such as + * anchors, phrase emphasis, and spans. The list of tags we're looking + * for is hard-coded: + * + * * List "a" is made of tags which can be both inline or block-level. + * These will be treated block-level when the start tag is alone on + * its line, otherwise they're not matched here and will be taken as + * inline later. + * * List "b" is made of tags which are always block-level; + */ $block_tags_a_re = 'ins|del'; $block_tags_b_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|'. 'script|noscript|style|form|fieldset|iframe|math|svg|'. 'article|section|nav|aside|hgroup|header|footer|'. 'figure'; - # Regular expression for the content of a block tag. + // Regular expression for the content of a block tag. $nested_tags_level = 4; $attr = ' (?> # optional tag attributes @@ -263,7 +363,7 @@ class Markdown implements MarkdownInterface { | \'[^\']*\' # text inside single quotes (tolerate ">") )* - )? + )? '; $content = str_repeat(' @@ -275,29 +375,32 @@ class Markdown implements MarkdownInterface { (?> /> | - >', $nested_tags_level). # end of opening tag - '.*?'. # last level nested tag content + >', $nested_tags_level). // end of opening tag + '.*?'. // last level nested tag content str_repeat(' # closing nested tag ) - | + | <(?!/\2\s*> # other tags with a different name ) )*', $nested_tags_level); $content2 = str_replace('\2', '\3', $content); - # First, look for nested blocks, e.g.: - #

- #
- # tags for inner block must be indented. - #
- #
- # - # The outermost tags must start at the left margin for this to match, and - # the inner nested divs must be indented. - # We need to do this before the next, more liberal match, because the next - # match will start at the first `
` and stop at the first `
`. + /** + * First, look for nested blocks, e.g.: + *
+ *
+ * tags for inner block must be indented. + *
+ *
+ * + * The outermost tags must start at the left margin for this to match, + * and the inner nested divs must be indented. + * We need to do this before the next, more liberal match, because the + * next match will start at the first `
` and stop at the + * first `
`. + */ $text = preg_replace_callback('{(?> (?> (?<=\n) # Starting on its own line @@ -306,9 +409,9 @@ class Markdown implements MarkdownInterface { ) ( # save in $1 - # Match from `\n` to `\n`, handling nested tags + # Match from `\n` to `\n`, handling nested tags # in between. - + [ ]{0,'.$less_than_tab.'} <('.$block_tags_b_re.')# start tag = $2 '.$attr.'> # attributes followed by > and \n @@ -326,28 +429,28 @@ class Markdown implements MarkdownInterface { # the matching end tag [ ]* # trailing spaces/tabs (?=\n+|\Z) # followed by a newline or end of document - - | # Special case just for
. It was easier to make a special + + | # Special case just for
. It was easier to make a special # case than to make the other regex more complicated. - + [ ]{0,'.$less_than_tab.'} <(hr) # start tag = $2 '.$attr.' # attributes /?> # the matching end tag [ ]* (?=\n{2,}|\Z) # followed by a blank line or end of document - + | # Special case for standalone HTML comments: - + [ ]{0,'.$less_than_tab.'} (?s: ) [ ]* (?=\n{2,}|\Z) # followed by a blank line or end of document - + | # PHP and ASP-style processor instructions (hashBlock($text); return "\n\n$key\n\n"; } - - + + /** + * Called whenever a tag must be hashed when a function insert an atomic + * element in the text stream. Passing $text to through this function gives + * a unique text-token which will be reverted back when calling unhash. + * + * The $boundary argument specify what character should be used to surround + * the token. By convension, "B" is used for block elements that needs not + * to be wrapped into paragraph tags at the end, ":" is used for elements + * that are word separators and "X" is used in the general case. + * + * @param string $text + * @param string $boundary + * @return string + */ protected function hashPart($text, $boundary = 'X') { - # - # Called whenever a tag must be hashed when a function insert an atomic - # element in the text stream. Passing $text to through this function gives - # a unique text-token which will be reverted back when calling unhash. - # - # The $boundary argument specify what character should be used to surround - # the token. By convension, "B" is used for block elements that needs not - # to be wrapped into paragraph tags at the end, ":" is used for elements - # that are word separators and "X" is used in the general case. - # - # Swap back any tag hash found in $text so we do not have to `unhash` - # multiple times at the end. + // Swap back any tag hash found in $text so we do not have to `unhash` + // multiple times at the end. $text = $this->unhash($text); - - # Then hash the block. + + // Then hash the block. static $i = 0; $key = "$boundary\x1A" . ++$i . $boundary; $this->html_hashes[$key] = $text; - return $key; # String that will replace the tag. + return $key; // String that will replace the tag. } - + /** + * Shortcut function for hashPart with block-level boundaries. + * @param string $text + * @return string + */ protected function hashBlock($text) { - # - # Shortcut function for hashPart with block-level boundaries. - # return $this->hashPart($text, 'B'); } - + /** + * Define the block gamut - these are all the transformations that form + * block-level tags like paragraphs, headers, and list items. + * @var array + */ protected $block_gamut = array( - # - # These are all the transformations that form block-level - # tags like paragraphs, headers, and list items. - # "doHeaders" => 10, "doHorizontalRules" => 20, - "doLists" => 40, "doCodeBlocks" => 50, "doBlockQuotes" => 60, - ); + ); + /** + * Run block gamut tranformations. + * + * We need to escape raw HTML in Markdown source before doing anything + * else. This need to be done for each block, and not only at the + * begining in the Markdown function since hashed blocks can be part of + * list items and could have been indented. Indented blocks would have + * been seen as a code block in a previous pass of hashHTMLBlocks. + * + * @param string $text + * @return string + */ protected function runBlockGamut($text) { - # - # Run block gamut tranformations. - # - # We need to escape raw HTML in Markdown source before doing anything - # else. This need to be done for each block, and not only at the - # begining in the Markdown function since hashed blocks can be part of - # list items and could have been indented. Indented blocks would have - # been seen as a code block in a previous pass of hashHTMLBlocks. $text = $this->hashHTMLBlocks($text); - return $this->runBasicBlockGamut($text); } - + + /** + * Run block gamut tranformations, without hashing HTML blocks. This is + * useful when HTML blocks are known to be already hashed, like in the first + * whole-document pass. + * + * @param string $text + * @return string + */ protected function runBasicBlockGamut($text) { - # - # Run block gamut tranformations, without hashing HTML blocks. This is - # useful when HTML blocks are known to be already hashed, like in the first - # whole-document pass. - # + foreach ($this->block_gamut as $method => $priority) { $text = $this->$method($text); } - - # Finally form paragraph and restore hashed blocks. + + // Finally form paragraph and restore hashed blocks. $text = $this->formParagraphs($text); return $text; } - - + + /** + * Convert horizontal rules + * @param string $text + * @return string + */ protected function doHorizontalRules($text) { - # Do Horizontal Rules: return preg_replace( '{ ^[ ]{0,3} # Leading space @@ -459,67 +582,82 @@ class Markdown implements MarkdownInterface { [ ]* # Tailing spaces $ # End of line. }mx', - "\n".$this->hashBlock("empty_element_suffix")."\n", - $text); + "\n".$this->hashBlock("empty_element_suffix")."\n", + $text + ); } - + /** + * These are all the transformations that occur *within* block-level + * tags like paragraphs, headers, and list items. + * @var array + */ protected $span_gamut = array( - # - # These are all the transformations that occur *within* block-level - # tags like paragraphs, headers, and list items. - # - # Process character escapes, code spans, and inline HTML - # in one shot. + // Process character escapes, code spans, and inline HTML + // in one shot. "parseSpan" => -30, - - # Process anchor and image tags. Images must come first, - # because ![foo][f] looks like an anchor. + // Process anchor and image tags. Images must come first, + // because ![foo][f] looks like an anchor. "doImages" => 10, "doAnchors" => 20, - - # Make links out of things like `` - # Must come after doAnchors, because you can use < and > - # delimiters in inline links like [this](). + // Make links out of things like `` + // Must come after doAnchors, because you can use < and > + // delimiters in inline links like [this](). "doAutoLinks" => 30, "encodeAmpsAndAngles" => 40, - "doItalicsAndBold" => 50, "doHardBreaks" => 60, - ); + ); + /** + * Run span gamut transformations + * @param string $text + * @return string + */ protected function runSpanGamut($text) { - # - # Run span gamut tranformations. - # foreach ($this->span_gamut as $method => $priority) { $text = $this->$method($text); } return $text; } - - + + /** + * Do hard breaks + * @param string $text + * @return string + */ protected function doHardBreaks($text) { - # Do hard breaks: - return preg_replace_callback('/ {2,}\n/', - array($this, '_doHardBreaks_callback'), $text); + if ($this->hard_wrap) { + return preg_replace_callback('/ *\n/', + array($this, '_doHardBreaks_callback'), $text); + } else { + return preg_replace_callback('/ {2,}\n/', + array($this, '_doHardBreaks_callback'), $text); + } } + + /** + * Trigger part hashing for the hard break (callback method) + * @param array $matches + * @return string + */ protected function _doHardBreaks_callback($matches) { return $this->hashPart("empty_element_suffix\n"); } - + /** + * Turn Markdown link shortcuts into XHTML tags. + * @param string $text + * @return string + */ protected function doAnchors($text) { - # - # Turn Markdown link shortcuts into XHTML tags. - # - if ($this->in_anchor) return $text; + if ($this->in_anchor) { + return $text; + } $this->in_anchor = true; - - # - # First, handle reference-style links: [link text] [id] - # + + // First, handle reference-style links: [link text] [id] $text = preg_replace_callback('{ ( # wrap whole match in $1 \[ @@ -536,9 +674,7 @@ class Markdown implements MarkdownInterface { }xs', array($this, '_doAnchors_reference_callback'), $text); - # - # Next, inline-style links: [link text](url "optional title") - # + // Next, inline-style links: [link text](url "optional title") $text = preg_replace_callback('{ ( # wrap whole match in $1 \[ @@ -563,11 +699,9 @@ class Markdown implements MarkdownInterface { }xs', array($this, '_doAnchors_inline_callback'), $text); - # - # Last, handle reference-style shortcuts: [link text] - # These must come last in case you've also got [link text][1] - # or [link text](/foo) - # + // Last, handle reference-style shortcuts: [link text] + // These must come last in case you've also got [link text][1] + // or [link text](/foo) $text = preg_replace_callback('{ ( # wrap whole match in $1 \[ @@ -580,48 +714,60 @@ class Markdown implements MarkdownInterface { $this->in_anchor = false; return $text; } + + /** + * Callback method to parse referenced anchors + * @param string $matches + * @return string + */ protected function _doAnchors_reference_callback($matches) { $whole_match = $matches[1]; $link_text = $matches[2]; $link_id =& $matches[3]; if ($link_id == "") { - # for shortcut links like [this][] or [this]. + // for shortcut links like [this][] or [this]. $link_id = $link_text; } - - # lower-case and turn embedded newlines into spaces + + // lower-case and turn embedded newlines into spaces $link_id = strtolower($link_id); $link_id = preg_replace('{[ ]?\n}', ' ', $link_id); if (isset($this->urls[$link_id])) { $url = $this->urls[$link_id]; $url = $this->encodeURLAttribute($url); - + $result = "titles[$link_id] ) ) { $title = $this->titles[$link_id]; $title = $this->encodeAttribute($title); $result .= " title=\"$title\""; } - + $link_text = $this->runSpanGamut($link_text); $result .= ">$link_text"; $result = $this->hashPart($result); - } - else { + } else { $result = $whole_match; } return $result; } + + /** + * Callback method to parse inline anchors + * @param string $matches + * @return string + */ protected function _doAnchors_inline_callback($matches) { $whole_match = $matches[1]; $link_text = $this->runSpanGamut($matches[2]); $url = $matches[3] == '' ? $matches[4] : $matches[3]; $title =& $matches[7]; - // if the URL was of the form it got caught by the HTML - // tag parser and hashed. Need to reverse the process before using the URL. + // If the URL was of the form it got caught by the HTML + // tag parser and hashed. Need to reverse the process before using + // the URL. $unhashed = $this->unhash($url); if ($unhashed != $url) $url = preg_replace('/^<(.*)>$/', '\1', $unhashed); @@ -633,21 +779,20 @@ class Markdown implements MarkdownInterface { $title = $this->encodeAttribute($title); $result .= " title=\"$title\""; } - + $link_text = $this->runSpanGamut($link_text); $result .= ">$link_text"; return $this->hashPart($result); } - + /** + * Turn Markdown image shortcuts into tags. + * @param string $text + * @return string + */ protected function doImages($text) { - # - # Turn Markdown image shortcuts into tags. - # - # - # First, handle reference-style labeled images: ![alt text][id] - # + // First, handle reference-style labeled images: ![alt text][id] $text = preg_replace_callback('{ ( # wrap whole match in $1 !\[ @@ -662,13 +807,11 @@ class Markdown implements MarkdownInterface { \] ) - }xs', + }xs', array($this, '_doImages_reference_callback'), $text); - # - # Next, handle inline images: ![alt text](url "optional title") - # Don't forget: encode * and _ - # + // Next, handle inline images: ![alt text](url "optional title") + // Don't forget: encode * and _ $text = preg_replace_callback('{ ( # wrap whole match in $1 !\[ @@ -696,13 +839,19 @@ class Markdown implements MarkdownInterface { return $text; } + + /** + * Callback to parse references image tags + * @param array $matches + * @return string + */ protected function _doImages_reference_callback($matches) { $whole_match = $matches[1]; $alt_text = $matches[2]; $link_id = strtolower($matches[3]); if ($link_id == "") { - $link_id = strtolower($alt_text); # for shortcut links like ![this][]. + $link_id = strtolower($alt_text); // for shortcut links like ![this][]. } $alt_text = $this->encodeAttribute($alt_text); @@ -716,14 +865,19 @@ class Markdown implements MarkdownInterface { } $result .= $this->empty_element_suffix; $result = $this->hashPart($result); - } - else { - # If there's no such link ID, leave intact: + } else { + // If there's no such link ID, leave intact: $result = $whole_match; } return $result; } + + /** + * Callback to parse inline image tags + * @param array $matches + * @return string + */ protected function _doImages_inline_callback($matches) { $whole_match = $matches[1]; $alt_text = $matches[2]; @@ -735,32 +889,38 @@ class Markdown implements MarkdownInterface { $result = "\"$alt_text\"";encodeAttribute($title); - $result .= " title=\"$title\""; # $title already quoted + $result .= " title=\"$title\""; // $title already quoted } $result .= $this->empty_element_suffix; return $this->hashPart($result); } - + /** + * Parse Markdown heading elements to HTML + * @param string $text + * @return string + */ protected function doHeaders($text) { - # Setext-style headers: - # Header 1 - # ======== - # - # Header 2 - # -------- - # + /** + * Setext-style headers: + * Header 1 + * ======== + * + * Header 2 + * -------- + */ $text = preg_replace_callback('{ ^(.+?)[ ]*\n(=+|-+)[ ]*\n+ }mx', array($this, '_doHeaders_callback_setext'), $text); - # atx-style headers: - # # Header 1 - # ## Header 2 - # ## Header 2 with closing hashes ## - # ... - # ###### Header 6 - # + /** + * atx-style headers: + * # Header 1 + * ## Header 2 + * ## Header 2 with closing hashes ## + * ... + * ###### Header 6 + */ $text = preg_replace_callback('{ ^(\#{1,6}) # $1 = string of #\'s [ ]* @@ -773,29 +933,72 @@ class Markdown implements MarkdownInterface { return $text; } + + /** + * Setext header parsing callback + * @param array $matches + * @return string + */ protected function _doHeaders_callback_setext($matches) { - # Terrible hack to check we haven't found an empty list item. - if ($matches[2] == '-' && preg_match('{^-(?: |$)}', $matches[1])) + // Terrible hack to check we haven't found an empty list item. + if ($matches[2] == '-' && preg_match('{^-(?: |$)}', $matches[1])) { return $matches[0]; - + } + $level = $matches[2]{0} == '=' ? 1 : 2; - $block = "".$this->runSpanGamut($matches[1]).""; + + // ID attribute generation + $idAtt = $this->_generateIdFromHeaderValue($matches[1]); + + $block = "".$this->runSpanGamut($matches[1]).""; return "\n" . $this->hashBlock($block) . "\n\n"; } + + /** + * ATX header parsing callback + * @param array $matches + * @return string + */ protected function _doHeaders_callback_atx($matches) { + // ID attribute generation + $idAtt = $this->_generateIdFromHeaderValue($matches[2]); + $level = strlen($matches[1]); - $block = "".$this->runSpanGamut($matches[2]).""; + $block = "".$this->runSpanGamut($matches[2]).""; return "\n" . $this->hashBlock($block) . "\n\n"; } + /** + * If a header_id_func property is set, we can use it to automatically + * generate an id attribute. + * + * This method returns a string in the form id="foo", or an empty string + * otherwise. + * @param string $headerValue + * @return string + */ + protected function _generateIdFromHeaderValue($headerValue) { + if (!is_callable($this->header_id_func)) { + return ""; + } + $idValue = call_user_func($this->header_id_func, $headerValue); + if (!$idValue) { + return ""; + } + + return ' id="' . $this->encodeAttribute($idValue) . '"'; + } + + /** + * Form HTML ordered (numbered) and unordered (bulleted) lists. + * @param string $text + * @return string + */ protected function doLists($text) { - # - # Form HTML ordered (numbered) and unordered (bulleted) lists. - # $less_than_tab = $this->tab_width - 1; - # Re-usable patterns to match list item bullets and number markers: + // Re-usable patterns to match list item bullets and number markers: $marker_ul_re = '[*+-]'; $marker_ol_re = '\d+[\.]'; @@ -805,7 +1008,7 @@ class Markdown implements MarkdownInterface { ); foreach ($markers_relist as $marker_re => $other_marker_re) { - # Re-usable pattern to match any entirel ul or ol list: + // Re-usable pattern to match any entirel ul or ol list: $whole_list_re = ' ( # $1 = whole list ( # $2 @@ -832,18 +1035,17 @@ class Markdown implements MarkdownInterface { ) ) '; // mx - - # We use a different prefix before nested lists than top-level lists. - # See extended comment in _ProcessListItems(). - + + // We use a different prefix before nested lists than top-level lists. + //See extended comment in _ProcessListItems(). + if ($this->list_level) { $text = preg_replace_callback('{ ^ '.$whole_list_re.' }mx', array($this, '_doLists_callback'), $text); - } - else { + } else { $text = preg_replace_callback('{ (?:(?<=\n)\n|\A\n?) # Must eat the newline '.$whole_list_re.' @@ -854,55 +1056,86 @@ class Markdown implements MarkdownInterface { return $text; } + + /** + * List parsing callback + * @param array $matches + * @return string + */ protected function _doLists_callback($matches) { - # Re-usable patterns to match list item bullets and number markers: + // Re-usable patterns to match list item bullets and number markers: $marker_ul_re = '[*+-]'; $marker_ol_re = '\d+[\.]'; $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)"; - + $marker_ol_start_re = '[0-9]+'; + $list = $matches[1]; $list_type = preg_match("/$marker_ul_re/", $matches[4]) ? "ul" : "ol"; - + $marker_any_re = ( $list_type == "ul" ? $marker_ul_re : $marker_ol_re ); - + $list .= "\n"; $result = $this->processListItems($list, $marker_any_re); - - $result = $this->hashBlock("<$list_type>\n" . $result . ""); + + $ol_start = 1; + if ($this->enhanced_ordered_list) { + // Get the start number for ordered list. + if ($list_type == 'ol') { + $ol_start_array = array(); + $ol_start_check = preg_match("/$marker_ol_start_re/", $matches[4], $ol_start_array); + if ($ol_start_check){ + $ol_start = $ol_start_array[0]; + } + } + } + + if ($ol_start > 1 && $list_type == 'ol'){ + $result = $this->hashBlock("<$list_type start=\"$ol_start\">\n" . $result . ""); + } else { + $result = $this->hashBlock("<$list_type>\n" . $result . ""); + } return "\n". $result ."\n\n"; } + /** + * Nesting tracker for list levels + * @var integer + */ protected $list_level = 0; + /** + * Process the contents of a single ordered or unordered list, splitting it + * into individual list items. + * @param string $list_str + * @param string $marker_any_re + * @return string + */ protected function processListItems($list_str, $marker_any_re) { - # - # Process the contents of a single ordered or unordered list, splitting it - # into individual list items. - # - # The $this->list_level global keeps track of when we're inside a list. - # Each time we enter a list, we increment it; when we leave a list, - # we decrement. If it's zero, we're not in a list anymore. - # - # We do this because when we're not inside a list, we want to treat - # something like this: - # - # I recommend upgrading to version - # 8. Oops, now this line is treated - # as a sub-list. - # - # As a single paragraph, despite the fact that the second line starts - # with a digit-period-space sequence. - # - # Whereas when we're inside a list (or sub-list), that line will be - # treated as the start of a sub-list. What a kludge, huh? This is - # an aspect of Markdown's syntax that's hard to parse perfectly - # without resorting to mind-reading. Perhaps the solution is to - # change the syntax rules such that sub-lists must start with a - # starting cardinal number; e.g. "1." or "a.". - + /** + * The $this->list_level global keeps track of when we're inside a list. + * Each time we enter a list, we increment it; when we leave a list, + * we decrement. If it's zero, we're not in a list anymore. + * + * We do this because when we're not inside a list, we want to treat + * something like this: + * + * I recommend upgrading to version + * 8. Oops, now this line is treated + * as a sub-list. + * + * As a single paragraph, despite the fact that the second line starts + * with a digit-period-space sequence. + * + * Whereas when we're inside a list (or sub-list), that line will be + * treated as the start of a sub-list. What a kludge, huh? This is + * an aspect of Markdown's syntax that's hard to parse perfectly + * without resorting to mind-reading. Perhaps the solution is to + * change the syntax rules such that sub-lists must start with a + * starting cardinal number; e.g. "1." or "a.". + */ $this->list_level++; - # trim trailing blank lines: + // Trim trailing blank lines: $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str); $list_str = preg_replace_callback('{ @@ -920,6 +1153,12 @@ class Markdown implements MarkdownInterface { $this->list_level--; return $list_str; } + + /** + * List item parsing callback + * @param array $matches + * @return string + */ protected function _processListItems_callback($matches) { $item = $matches[4]; $leading_line =& $matches[1]; @@ -927,28 +1166,27 @@ class Markdown implements MarkdownInterface { $marker_space = $matches[3]; $tailing_blank_line =& $matches[5]; - if ($leading_line || $tailing_blank_line || + if ($leading_line || $tailing_blank_line || preg_match('/\n{2,}/', $item)) { - # Replace marker with the appropriate whitespace indentation + // Replace marker with the appropriate whitespace indentation $item = $leading_space . str_repeat(' ', strlen($marker_space)) . $item; $item = $this->runBlockGamut($this->outdent($item)."\n"); - } - else { - # Recursion for sub-lists: + } else { + // Recursion for sub-lists: $item = $this->doLists($this->outdent($item)); - $item = preg_replace('/\n+$/', '', $item); - $item = $this->runSpanGamut($item); + $item = $this->formParagraphs($item, false); } return "
  • " . $item . "
  • \n"; } - + /** + * Process Markdown `
    ` blocks.
    +	 * @param  string $text
    +	 * @return string
    +	 */
     	protected function doCodeBlocks($text) {
    -	#
    -	#	Process Markdown `
    ` blocks.
    -	#
     		$text = preg_replace_callback('{
     				(?:\n\n|\A\n?)
     				(	            # $1 = the code block -- one or more lines, starting with a space/tab
    @@ -963,106 +1201,141 @@ class Markdown implements MarkdownInterface {
     
     		return $text;
     	}
    +
    +	/**
    +	 * Code block parsing callback
    +	 * @param  array $matches
    +	 * @return string
    +	 */
     	protected function _doCodeBlocks_callback($matches) {
     		$codeblock = $matches[1];
     
     		$codeblock = $this->outdent($codeblock);
    -		$codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
    +		if ($this->code_block_content_func) {
    +			$codeblock = call_user_func($this->code_block_content_func, $codeblock, "");
    +		} else {
    +			$codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
    +		}
     
     		# trim leading newlines and trailing newlines
     		$codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
     
     		$codeblock = "
    $codeblock\n
    "; - return "\n\n".$this->hashBlock($codeblock)."\n\n"; + return "\n\n" . $this->hashBlock($codeblock) . "\n\n"; } - + /** + * Create a code span markup for $code. Called from handleSpanToken. + * @param string $code + * @return string + */ protected function makeCodeSpan($code) { - # - # Create a code span markup for $code. Called from handleSpanToken. - # - $code = htmlspecialchars(trim($code), ENT_NOQUOTES); + if ($this->code_span_content_func) { + $code = call_user_func($this->code_span_content_func, $code); + } else { + $code = htmlspecialchars(trim($code), ENT_NOQUOTES); + } return $this->hashPart("$code"); } - + /** + * Define the emphasis operators with their regex matches + * @var array + */ protected $em_relist = array( '' => '(?:(? '(? '(? '(?:(? '(? '(? '(?:(? '(? '(?em_relist as $em => $em_re) { foreach ($this->strong_relist as $strong => $strong_re) { - # Construct list of allowed token expressions. + // Construct list of allowed token expressions. $token_relist = array(); if (isset($this->em_strong_relist["$em$strong"])) { $token_relist[] = $this->em_strong_relist["$em$strong"]; } $token_relist[] = $em_re; $token_relist[] = $strong_re; - - # Construct master expression from list. - $token_re = '{('. implode('|', $token_relist) .')}'; + + // Construct master expression from list. + $token_re = '{(' . implode('|', $token_relist) . ')}'; $this->em_strong_prepared_relist["$em$strong"] = $token_re; } } } - + + /** + * Convert Markdown italics (emphasis) and bold (strong) to HTML + * @param string $text + * @return string + */ protected function doItalicsAndBold($text) { $token_stack = array(''); $text_stack = array(''); $em = ''; $strong = ''; $tree_char_em = false; - + while (1) { - # - # Get prepared regular expression for seraching emphasis tokens - # in current context. - # + // Get prepared regular expression for seraching emphasis tokens + // in current context. $token_re = $this->em_strong_prepared_relist["$em$strong"]; - - # - # Each loop iteration search for the next emphasis token. - # Each token is then passed to handleSpanToken. - # + + // Each loop iteration search for the next emphasis token. + // Each token is then passed to handleSpanToken. $parts = preg_split($token_re, $text, 2, PREG_SPLIT_DELIM_CAPTURE); $text_stack[0] .= $parts[0]; $token =& $parts[1]; $text =& $parts[2]; - + if (empty($token)) { - # Reached end of text span: empty stack without emitting. - # any more emphasis. + // Reached end of text span: empty stack without emitting. + // any more emphasis. while ($token_stack[0]) { $text_stack[1] .= array_shift($token_stack); $text_stack[0] .= array_shift($text_stack); } break; } - + $token_len = strlen($token); if ($tree_char_em) { - # Reached closing marker while inside a three-char emphasis. + // Reached closing marker while inside a three-char emphasis. if ($token_len == 3) { - # Three-char closing marker, close em and strong. + // Three-char closing marker, close em and strong. array_shift($token_stack); $span = array_shift($text_stack); $span = $this->runSpanGamut($span); @@ -1071,21 +1344,21 @@ class Markdown implements MarkdownInterface { $em = ''; $strong = ''; } else { - # Other closing marker: close one em or strong and - # change current token state to match the other + // Other closing marker: close one em or strong and + // change current token state to match the other $token_stack[0] = str_repeat($token{0}, 3-$token_len); $tag = $token_len == 2 ? "strong" : "em"; $span = $text_stack[0]; $span = $this->runSpanGamut($span); $span = "<$tag>$span"; $text_stack[0] = $this->hashPart($span); - $$tag = ''; # $$tag stands for $em or $strong + $$tag = ''; // $$tag stands for $em or $strong } $tree_char_em = false; } else if ($token_len == 3) { if ($em) { - # Reached closing marker for both em and strong. - # Closing strong marker: + // Reached closing marker for both em and strong. + // Closing strong marker: for ($i = 0; $i < 2; ++$i) { $shifted_token = array_shift($token_stack); $tag = strlen($shifted_token) == 2 ? "strong" : "em"; @@ -1093,11 +1366,11 @@ class Markdown implements MarkdownInterface { $span = $this->runSpanGamut($span); $span = "<$tag>$span"; $text_stack[0] .= $this->hashPart($span); - $$tag = ''; # $$tag stands for $em or $strong + $$tag = ''; // $$tag stands for $em or $strong } } else { - # Reached opening three-char emphasis marker. Push on token - # stack; will be handled by the special condition above. + // Reached opening three-char emphasis marker. Push on token + // stack; will be handled by the special condition above. $em = $token{0}; $strong = "$em$em"; array_unshift($token_stack, $token); @@ -1106,12 +1379,12 @@ class Markdown implements MarkdownInterface { } } else if ($token_len == 2) { if ($strong) { - # Unwind any dangling emphasis marker: + // Unwind any dangling emphasis marker: if (strlen($token_stack[0]) == 1) { $text_stack[1] .= array_shift($token_stack); $text_stack[0] .= array_shift($text_stack); } - # Closing strong marker: + // Closing strong marker: array_shift($token_stack); $span = array_shift($text_stack); $span = $this->runSpanGamut($span); @@ -1124,10 +1397,10 @@ class Markdown implements MarkdownInterface { $strong = $token; } } else { - # Here $token_len == 1 + // Here $token_len == 1 if ($em) { if (strlen($token_stack[0]) == 1) { - # Closing emphasis marker: + // Closing emphasis marker: array_shift($token_stack); $span = array_shift($text_stack); $span = $this->runSpanGamut($span); @@ -1147,7 +1420,11 @@ class Markdown implements MarkdownInterface { return $text_stack[0]; } - + /** + * Parse Markdown blockquotes to HTML + * @param string $text + * @return string + */ protected function doBlockQuotes($text) { $text = preg_replace_callback('/ ( # Wrap whole match in $1 @@ -1163,51 +1440,64 @@ class Markdown implements MarkdownInterface { return $text; } + + /** + * Blockquote parsing callback + * @param array $matches + * @return string + */ protected function _doBlockQuotes_callback($matches) { $bq = $matches[1]; - # trim one level of quoting - trim whitespace-only lines + // trim one level of quoting - trim whitespace-only lines $bq = preg_replace('/^[ ]*>[ ]?|^[ ]+$/m', '', $bq); - $bq = $this->runBlockGamut($bq); # recurse + $bq = $this->runBlockGamut($bq); // recurse $bq = preg_replace('/^/m', " ", $bq); - # These leading spaces cause problem with
     content, 
    -		# so we need to fix that:
    -		$bq = preg_replace_callback('{(\s*
    .+?
    )}sx', + // These leading spaces cause problem with
     content,
    +		// so we need to fix that:
    +		$bq = preg_replace_callback('{(\s*
    .+?
    )}sx', array($this, '_doBlockQuotes_callback2'), $bq); - return "\n". $this->hashBlock("
    \n$bq\n
    ")."\n\n"; + return "\n" . $this->hashBlock("
    \n$bq\n
    ") . "\n\n"; } + + /** + * Blockquote parsing callback + * @param array $matches + * @return string + */ protected function _doBlockQuotes_callback2($matches) { $pre = $matches[1]; $pre = preg_replace('/^ /m', '', $pre); return $pre; } - - protected function formParagraphs($text) { - # - # Params: - # $text - string to process with html

    tags - # - # Strip leading and trailing lines: + /** + * Parse paragraphs + * + * @param string $text String to process in paragraphs + * @param boolean $wrap_in_p Whether paragraphs should be wrapped in

    tags + * @return string + */ + protected function formParagraphs($text, $wrap_in_p = true) { + // Strip leading and trailing lines: $text = preg_replace('/\A\n+|\n+\z/', '', $text); $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY); - # - # Wrap

    tags and unhashify HTML blocks - # + // Wrap

    tags and unhashify HTML blocks foreach ($grafs as $key => $value) { if (!preg_match('/^B\x1A[0-9]+B$/', $value)) { - # Is a paragraph. + // Is a paragraph. $value = $this->runSpanGamut($value); - $value = preg_replace('/^([ ]*)/', "

    ", $value); - $value .= "

    "; + if ($wrap_in_p) { + $value = preg_replace('/^([ ]*)/', "

    ", $value); + $value .= "

    "; + } $grafs[$key] = $this->unhash($value); - } - else { - # Is a block. - # Modify elements of @grafs in-place... + } else { + // Is a block. + // Modify elements of @grafs in-place... $graf = $value; $block = $this->html_hashes[$graf]; $graf = $block; @@ -1232,11 +1522,11 @@ class Markdown implements MarkdownInterface { // { // list(, $div_open, , $div_content, $div_close) = $matches; // -// # We can't call Markdown(), because that resets the hash; -// # that initialization code should be pulled into its own sub, though. +// // We can't call Markdown(), because that resets the hash; +// // that initialization code should be pulled into its own sub, though. // $div_content = $this->hashHTMLBlocks($div_content); -// -// # Run document gamut methods on the content. +// +// // Run document gamut methods on the content. // foreach ($this->document_gamut as $method => $priority) { // $div_content = $this->$method($div_content); // } @@ -1253,71 +1543,78 @@ class Markdown implements MarkdownInterface { return implode("\n\n", $grafs); } - + /** + * Encode text for a double-quoted HTML attribute. This function + * is *not* suitable for attributes enclosed in single quotes. + * @param string $text + * @return string + */ protected function encodeAttribute($text) { - # - # Encode text for a double-quoted HTML attribute. This function - # is *not* suitable for attributes enclosed in single quotes. - # $text = $this->encodeAmpsAndAngles($text); $text = str_replace('"', '"', $text); return $text; } - + /** + * Encode text for a double-quoted HTML attribute containing a URL, + * applying the URL filter if set. Also generates the textual + * representation for the URL (removing mailto: or tel:) storing it in $text. + * This function is *not* suitable for attributes enclosed in single quotes. + * + * @param string $url + * @param string &$text Passed by reference + * @return string URL + */ protected function encodeURLAttribute($url, &$text = null) { - # - # Encode text for a double-quoted HTML attribute containing a URL, - # applying the URL filter if set. Also generates the textual - # representation for the URL (removing mailto: or tel:) storing it in $text. - # This function is *not* suitable for attributes enclosed in single quotes. - # - if ($this->url_filter_func) + if ($this->url_filter_func) { $url = call_user_func($this->url_filter_func, $url); + } - if (preg_match('{^mailto:}i', $url)) + if (preg_match('{^mailto:}i', $url)) { $url = $this->encodeEntityObfuscatedAttribute($url, $text, 7); - else if (preg_match('{^tel:}i', $url)) - { + } else if (preg_match('{^tel:}i', $url)) { $url = $this->encodeAttribute($url); $text = substr($url, 4); - } - else - { + } else { $url = $this->encodeAttribute($url); $text = $url; } return $url; } - - + + /** + * Smart processing for ampersands and angle brackets that need to + * be encoded. Valid character entities are left alone unless the + * no-entities mode is set. + * @param string $text + * @return string + */ protected function encodeAmpsAndAngles($text) { - # - # Smart processing for ampersands and angle brackets that need to - # be encoded. Valid character entities are left alone unless the - # no-entities mode is set. - # if ($this->no_entities) { $text = str_replace('&', '&', $text); } else { - # Ampersand-encoding based entirely on Nat Irons's Amputator - # MT plugin: - $text = preg_replace('/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/', + // Ampersand-encoding based entirely on Nat Irons's Amputator + // MT plugin: + $text = preg_replace('/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/', '&', $text); } - # Encode remaining <'s + // Encode remaining <'s $text = str_replace('<', '<', $text); return $text; } - + /** + * Parse Markdown automatic links to anchor HTML tags + * @param string $text + * @return string + */ protected function doAutoLinks($text) { $text = preg_replace_callback('{<((https?|ftp|dict|tel):[^\'">\s]+)>}i', array($this, '_doAutoLinks_url_callback'), $text); - # Email addresses: + // Email addresses: $text = preg_replace_callback('{ < (?:mailto:)? @@ -1340,11 +1637,23 @@ class Markdown implements MarkdownInterface { return $text; } + + /** + * Parse URL callback + * @param array $matches + * @return string + */ protected function _doAutoLinks_url_callback($matches) { $url = $this->encodeURLAttribute($matches[1], $text); $link = "$text"; return $this->hashPart($link); } + + /** + * Parse email address callback + * @param array $matches + * @return string + */ protected function _doAutoLinks_email_callback($matches) { $addr = $matches[1]; $url = $this->encodeURLAttribute("mailto:$addr", $text); @@ -1352,42 +1661,52 @@ class Markdown implements MarkdownInterface { return $this->hashPart($link); } - + /** + * Input: some text to obfuscate, e.g. "mailto:foo@example.com" + * + * Output: the same text but with most characters encoded as either a + * decimal or hex entity, in the hopes of foiling most address + * harvesting spam bots. E.g.: + * + * mailto:foo + * @example.co + * m + * + * Note: the additional output $tail is assigned the same value as the + * ouput, minus the number of characters specified by $head_length. + * + * Based by a filter by Matthew Wickline, posted to BBEdit-Talk. + * With some optimizations by Milian Wolff. Forced encoding of HTML + * attribute special characters by Allan Odgaard. + * + * @param string $text + * @param string &$tail + * @param integer $head_length + * @return string + */ protected function encodeEntityObfuscatedAttribute($text, &$tail = null, $head_length = 0) { - # - # Input: some text to obfuscate, e.g. "mailto:foo@example.com" - # - # Output: the same text but with most characters encoded as either a - # decimal or hex entity, in the hopes of foiling most address - # harvesting spam bots. E.g.: - # - # mailto:foo - # @example.co - # m - # - # Note: the additional output $tail is assigned the same value as the - # ouput, minus the number of characters specified by $head_length. - # - # Based by a filter by Matthew Wickline, posted to BBEdit-Talk. - # With some optimizations by Milian Wolff. Forced encoding of HTML - # attribute special characters by Allan Odgaard. - # - if ($text == "") return $tail = ""; + if ($text == "") { + return $tail = ""; + } $chars = preg_split('/(? $char) { $ord = ord($char); - # Ignore non-ascii chars. + // Ignore non-ascii chars. if ($ord < 128) { - $r = ($seed * (1 + $key)) % 100; # Pseudo-random function. - # roughly 10% raw, 45% hex, 45% dec - # '@' *must* be encoded. I insist. - # '"' and '>' have to be encoded inside the attribute - if ($r > 90 && strpos('@"&>', $char) === false) /* do nothing */; - else if ($r < 45) $chars[$key] = '&#x'.dechex($ord).';'; - else $chars[$key] = '&#'.$ord.';'; + $r = ($seed * (1 + $key)) % 100; // Pseudo-random function. + // roughly 10% raw, 45% hex, 45% dec + // '@' *must* be encoded. I insist. + // '"' and '>' have to be encoded inside the attribute + if ($r > 90 && strpos('@"&>', $char) === false) { + /* do nothing */ + } else if ($r < 45) { + $chars[$key] = '&#x'.dechex($ord).';'; + } else { + $chars[$key] = '&#'.$ord.';'; + } } } @@ -1397,14 +1716,15 @@ class Markdown implements MarkdownInterface { return $text; } - + /** + * Take the string $str and parse it into tokens, hashing embeded HTML, + * escaped characters and handling code spans. + * @param string $str + * @return string + */ protected function parseSpan($str) { - # - # Take the string $str and parse it into tokens, hashing embeded HTML, - # escaped characters and handling code spans. - # $output = ''; - + $span_re = '{ ( \\\\'.$this->escape_chars_re.' @@ -1432,1720 +1752,145 @@ class Markdown implements MarkdownInterface { }xs'; while (1) { - # - # Each loop iteration seach for either the next tag, the next - # openning code span marker, or the next escaped character. - # Each token is then passed to handleSpanToken. - # + // Each loop iteration seach for either the next tag, the next + // openning code span marker, or the next escaped character. + // Each token is then passed to handleSpanToken. $parts = preg_split($span_re, $str, 2, PREG_SPLIT_DELIM_CAPTURE); - - # Create token from text preceding tag. + + // Create token from text preceding tag. if ($parts[0] != "") { $output .= $parts[0]; } - - # Check if we reach the end. + + // Check if we reach the end. if (isset($parts[1])) { $output .= $this->handleSpanToken($parts[1], $parts[2]); $str = $parts[2]; - } - else { + } else { break; } } - + return $output; } - - + + /** + * Handle $token provided by parseSpan by determining its nature and + * returning the corresponding value that should replace it. + * @param string $token + * @param string &$str + * @return string + */ protected function handleSpanToken($token, &$str) { - # - # Handle $token provided by parseSpan by determining its nature and - # returning the corresponding value that should replace it. - # switch ($token{0}) { case "\\": return $this->hashPart("&#". ord($token{1}). ";"); case "`": - # Search for end marker in remaining text. - if (preg_match('/^(.*?[^`])'.preg_quote($token).'(?!`)(.*)$/sm', + // Search for end marker in remaining text. + if (preg_match('/^(.*?[^`])'.preg_quote($token).'(?!`)(.*)$/sm', $str, $matches)) { $str = $matches[2]; $codespan = $this->makeCodeSpan($matches[1]); return $this->hashPart($codespan); } - return $token; // return as text since no ending marker found. + return $token; // Return as text since no ending marker found. default: return $this->hashPart($token); } } - + /** + * Remove one level of line-leading tabs or spaces + * @param string $text + * @return string + */ protected function outdent($text) { - # - # Remove one level of line-leading tabs or spaces - # - return preg_replace('/^(\t|[ ]{1,'.$this->tab_width.'})/m', '', $text); + return preg_replace('/^(\t|[ ]{1,' . $this->tab_width . '})/m', '', $text); } - # String length function for detab. `_initDetab` will create a function to - # hanlde UTF-8 if the default function does not exist. + /** + * String length function for detab. `_initDetab` will create a function to + * handle UTF-8 if the default function does not exist. + * @var string + */ protected $utf8_strlen = 'mb_strlen'; - + + /** + * Replace tabs with the appropriate amount of spaces. + * + * For each line we separate the line in blocks delemited by tab characters. + * Then we reconstruct every line by adding the appropriate number of space + * between each blocks. + * + * @param string $text + * @return string + */ protected function detab($text) { - # - # Replace tabs with the appropriate amount of space. - # - # For each line we separate the line in blocks delemited by - # tab characters. Then we reconstruct every line by adding the - # appropriate number of space between each blocks. - $text = preg_replace_callback('/^.*\t.*$/m', array($this, '_detab_callback'), $text); return $text; } + + /** + * Replace tabs callback + * @param string $matches + * @return string + */ protected function _detab_callback($matches) { $line = $matches[0]; - $strlen = $this->utf8_strlen; # strlen function for UTF-8. - - # Split in blocks. + $strlen = $this->utf8_strlen; // strlen function for UTF-8. + + // Split in blocks. $blocks = explode("\t", $line); - # Add each blocks to the line. + // Add each blocks to the line. $line = $blocks[0]; - unset($blocks[0]); # Do not add first block twice. + unset($blocks[0]); // Do not add first block twice. foreach ($blocks as $block) { - # Calculate amount of space, insert spaces, insert block. - $amount = $this->tab_width - + // Calculate amount of space, insert spaces, insert block. + $amount = $this->tab_width - $strlen($line, 'UTF-8') % $this->tab_width; $line .= str_repeat(" ", $amount) . $block; } return $line; } + + /** + * Check for the availability of the function in the `utf8_strlen` property + * (initially `mb_strlen`). If the function is not available, create a + * function that will loosely count the number of UTF-8 characters with a + * regular expression. + * @return void + */ protected function _initDetab() { - # - # Check for the availability of the function in the `utf8_strlen` property - # (initially `mb_strlen`). If the function is not available, create a - # function that will loosely count the number of UTF-8 characters with a - # regular expression. - # - if (function_exists($this->utf8_strlen)) return; + + if (function_exists($this->utf8_strlen)) { + return; + } + $this->utf8_strlen = create_function('$text', 'return preg_match_all( - "/[\\\\x00-\\\\xBF]|[\\\\xC0-\\\\xFF][\\\\x80-\\\\xBF]*/", + "/[\\\\x00-\\\\xBF]|[\\\\xC0-\\\\xFF][\\\\x80-\\\\xBF]*/", $text, $m);'); } - + /** + * Swap back in all the tags hashed by _HashHTMLBlocks. + * @param string $text + * @return string + */ protected function unhash($text) { - # - # Swap back in all the tags hashed by _HashHTMLBlocks. - # - return preg_replace_callback('/(.)\x1A[0-9]+\1/', + return preg_replace_callback('/(.)\x1A[0-9]+\1/', array($this, '_unhash_callback'), $text); } + + /** + * Unhashing callback + * @param array $matches + * @return string + */ protected function _unhash_callback($matches) { return $this->html_hashes[$matches[0]]; } - -} - - -# -# Temporary Markdown Extra Parser Implementation Class -# -# NOTE: DON'T USE THIS CLASS -# Currently the implementation of of Extra resides here in this temporary class. -# This makes it easier to propagate the changes between the three different -# packaging styles of PHP Markdown. When this issue is resolved, this -# MarkdownExtra_TmpImpl class here will disappear and \Michelf\MarkdownExtra -# will contain the code. So please use \Michelf\MarkdownExtra and ignore this -# one. -# - -abstract class _MarkdownExtra_TmpImpl extends \Michelf\Markdown { - - ### Configuration Variables ### - - # Prefix for footnote ids. - public $fn_id_prefix = ""; - - # Optional title attribute for footnote links and backlinks. - public $fn_link_title = ""; - public $fn_backlink_title = ""; - - # Optional class attribute for footnote links and backlinks. - public $fn_link_class = "footnote-ref"; - public $fn_backlink_class = "footnote-backref"; - - # Class name for table cell alignment (%% replaced left/center/right) - # For instance: 'go-%%' becomes 'go-left' or 'go-right' or 'go-center' - # If empty, the align attribute is used instead of a class name. - public $table_align_class_tmpl = ''; - - # Optional class prefix for fenced code block. - public $code_class_prefix = ""; - # Class attribute for code blocks goes on the `code` tag; - # setting this to true will put attributes on the `pre` tag instead. - public $code_attr_on_pre = false; - - # Predefined abbreviations. - public $predef_abbr = array(); - - - ### Parser Implementation ### - - public function __construct() { - # - # Constructor function. Initialize the parser object. - # - # Add extra escapable characters before parent constructor - # initialize the table. - $this->escape_chars .= ':|'; - - # Insert extra document, block, and span transformations. - # Parent constructor will do the sorting. - $this->document_gamut += array( - "doFencedCodeBlocks" => 5, - "stripFootnotes" => 15, - "stripAbbreviations" => 25, - "appendFootnotes" => 50, - ); - $this->block_gamut += array( - "doFencedCodeBlocks" => 5, - "doTables" => 15, - "doDefLists" => 45, - ); - $this->span_gamut += array( - "doFootnotes" => 5, - "doAbbreviations" => 70, - ); - - parent::__construct(); - } - - - # Extra variables used during extra transformations. - protected $footnotes = array(); - protected $footnotes_ordered = array(); - protected $footnotes_ref_count = array(); - protected $footnotes_numbers = array(); - protected $abbr_desciptions = array(); - protected $abbr_word_re = ''; - - # Give the current footnote number. - protected $footnote_counter = 1; - - - protected function setup() { - # - # Setting up Extra-specific variables. - # - parent::setup(); - - $this->footnotes = array(); - $this->footnotes_ordered = array(); - $this->footnotes_ref_count = array(); - $this->footnotes_numbers = array(); - $this->abbr_desciptions = array(); - $this->abbr_word_re = ''; - $this->footnote_counter = 1; - - foreach ($this->predef_abbr as $abbr_word => $abbr_desc) { - if ($this->abbr_word_re) - $this->abbr_word_re .= '|'; - $this->abbr_word_re .= preg_quote($abbr_word); - $this->abbr_desciptions[$abbr_word] = trim($abbr_desc); - } - } - - protected function teardown() { - # - # Clearing Extra-specific variables. - # - $this->footnotes = array(); - $this->footnotes_ordered = array(); - $this->footnotes_ref_count = array(); - $this->footnotes_numbers = array(); - $this->abbr_desciptions = array(); - $this->abbr_word_re = ''; - - parent::teardown(); - } - - - ### Extra Attribute Parser ### - - # Expression to use to catch attributes (includes the braces) - protected $id_class_attr_catch_re = '\{((?:[ ]*[#.a-z][-_:a-zA-Z0-9=]+){1,})[ ]*\}'; - # Expression to use when parsing in a context when no capture is desired - protected $id_class_attr_nocatch_re = '\{(?:[ ]*[#.a-z][-_:a-zA-Z0-9=]+){1,}[ ]*\}'; - - protected function doExtraAttributes($tag_name, $attr) { - # - # Parse attributes caught by the $this->id_class_attr_catch_re expression - # and return the HTML-formatted list of attributes. - # - # Currently supported attributes are .class and #id. - # - if (empty($attr)) return ""; - - # Split on components - preg_match_all('/[#.a-z][-_:a-zA-Z0-9=]+/', $attr, $matches); - $elements = $matches[0]; - - # handle classes and ids (only first id taken into account) - $classes = array(); - $attributes = array(); - $id = false; - foreach ($elements as $element) { - if ($element{0} == '.') { - $classes[] = substr($element, 1); - } else if ($element{0} == '#') { - if ($id === false) $id = substr($element, 1); - } else if (strpos($element, '=') > 0) { - $parts = explode('=', $element, 2); - $attributes[] = $parts[0] . '="' . $parts[1] . '"'; - } - } - - # compose attributes as string - $attr_str = ""; - if (!empty($id)) { - $attr_str .= ' id="'.$id.'"'; - } - if (!empty($classes)) { - $attr_str .= ' class="'.implode(" ", $classes).'"'; - } - if (!$this->no_markup && !empty($attributes)) { - $attr_str .= ' '.implode(" ", $attributes); - } - return $attr_str; - } - - - protected function stripLinkDefinitions($text) { - # - # Strips link definitions from text, stores the URLs and titles in - # hash references. - # - $less_than_tab = $this->tab_width - 1; - - # Link defs are in the form: ^[id]: url "optional title" - $text = preg_replace_callback('{ - ^[ ]{0,'.$less_than_tab.'}\[(.+)\][ ]?: # id = $1 - [ ]* - \n? # maybe *one* newline - [ ]* - (?: - <(.+?)> # url = $2 - | - (\S+?) # url = $3 - ) - [ ]* - \n? # maybe one newline - [ ]* - (?: - (?<=\s) # lookbehind for whitespace - ["(] - (.*?) # title = $4 - [")] - [ ]* - )? # title is optional - (?:[ ]* '.$this->id_class_attr_catch_re.' )? # $5 = extra id & class attr - (?:\n+|\Z) - }xm', - array($this, '_stripLinkDefinitions_callback'), - $text); - return $text; - } - protected function _stripLinkDefinitions_callback($matches) { - $link_id = strtolower($matches[1]); - $url = $matches[2] == '' ? $matches[3] : $matches[2]; - $this->urls[$link_id] = $url; - $this->titles[$link_id] =& $matches[4]; - $this->ref_attr[$link_id] = $this->doExtraAttributes("", $dummy =& $matches[5]); - return ''; # String that will replace the block - } - - - ### HTML Block Parser ### - - # Tags that are always treated as block tags: - protected $block_tags_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend|article|section|nav|aside|hgroup|header|footer|figcaption|figure'; - - # Tags treated as block tags only if the opening tag is alone on its line: - protected $context_block_tags_re = 'script|noscript|style|ins|del|iframe|object|source|track|param|math|svg|canvas|audio|video'; - - # Tags where markdown="1" default to span mode: - protected $contain_span_tags_re = 'p|h[1-6]|li|dd|dt|td|th|legend|address'; - - # Tags which must not have their contents modified, no matter where - # they appear: - protected $clean_tags_re = 'script|style|math|svg'; - - # Tags that do not need to be closed. - protected $auto_close_tags_re = 'hr|img|param|source|track'; - - - protected function hashHTMLBlocks($text) { - # - # Hashify HTML Blocks and "clean tags". - # - # We only want to do this for block-level HTML tags, such as headers, - # lists, and tables. That's because we still want to wrap

    s around - # "paragraphs" that are wrapped in non-block-level tags, such as anchors, - # phrase emphasis, and spans. The list of tags we're looking for is - # hard-coded. - # - # This works by calling _HashHTMLBlocks_InMarkdown, which then calls - # _HashHTMLBlocks_InHTML when it encounter block tags. When the markdown="1" - # attribute is found within a tag, _HashHTMLBlocks_InHTML calls back - # _HashHTMLBlocks_InMarkdown to handle the Markdown syntax within the tag. - # These two functions are calling each other. It's recursive! - # - if ($this->no_markup) return $text; - - # - # Call the HTML-in-Markdown hasher. - # - list($text, ) = $this->_hashHTMLBlocks_inMarkdown($text); - - return $text; - } - protected function _hashHTMLBlocks_inMarkdown($text, $indent = 0, - $enclosing_tag_re = '', $span = false) - { - # - # Parse markdown text, calling _HashHTMLBlocks_InHTML for block tags. - # - # * $indent is the number of space to be ignored when checking for code - # blocks. This is important because if we don't take the indent into - # account, something like this (which looks right) won't work as expected: - # - #

    - #
    - # Hello World. <-- Is this a Markdown code block or text? - #
    <-- Is this a Markdown code block or a real tag? - #
    - # - # If you don't like this, just don't indent the tag on which - # you apply the markdown="1" attribute. - # - # * If $enclosing_tag_re is not empty, stops at the first unmatched closing - # tag with that name. Nested tags supported. - # - # * If $span is true, text inside must treated as span. So any double - # newline will be replaced by a single newline so that it does not create - # paragraphs. - # - # Returns an array of that form: ( processed text , remaining text ) - # - if ($text === '') return array('', ''); - - # Regex to check for the presense of newlines around a block tag. - $newline_before_re = '/(?:^\n?|\n\n)*$/'; - $newline_after_re = - '{ - ^ # Start of text following the tag. - (?>[ ]*)? # Optional comment. - [ ]*\n # Must be followed by newline. - }xs'; - - # Regex to match any tag. - $block_tag_re = - '{ - ( # $2: Capture whole tag. - # Tag name. - '.$this->block_tags_re.' | - '.$this->context_block_tags_re.' | - '.$this->clean_tags_re.' | - (?!\s)'.$enclosing_tag_re.' - ) - (?: - (?=[\s"\'/a-zA-Z0-9]) # Allowed characters after tag name. - (?> - ".*?" | # Double quotes (can contain `>`) - \'.*?\' | # Single quotes (can contain `>`) - .+? # Anything but quotes and `>`. - )*? - )? - > # End of tag. - | - # HTML Comment - | - <\?.*?\?> | <%.*?%> # Processing instruction - | - # CData Block - '. ( !$span ? ' # If not in span. - | - # Indented code block - (?: ^[ ]*\n | ^ | \n[ ]*\n ) - [ ]{'.($indent+4).'}[^\n]* \n - (?> - (?: [ ]{'.($indent+4).'}[^\n]* | [ ]* ) \n - )* - | - # Fenced code block marker - (?<= ^ | \n ) - [ ]{0,'.($indent+3).'}(?:~{3,}|`{3,}) - [ ]* - (?: - \.?[-_:a-zA-Z0-9]+ # standalone class name - | - '.$this->id_class_attr_nocatch_re.' # extra attributes - )? - [ ]* - (?= \n ) - ' : '' ). ' # End (if not is span). - | - # Code span marker - # Note, this regex needs to go after backtick fenced - # code blocks but it should also be kept outside of the - # "if not in span" condition adding backticks to the parser - `+ - ) - }xs'; - - - $depth = 0; # Current depth inside the tag tree. - $parsed = ""; # Parsed text that will be returned. - - # - # Loop through every tag until we find the closing tag of the parent - # or loop until reaching the end of text if no parent tag specified. - # - do { - # - # Split the text using the first $tag_match pattern found. - # Text before pattern will be first in the array, text after - # pattern will be at the end, and between will be any catches made - # by the pattern. - # - $parts = preg_split($block_tag_re, $text, 2, - PREG_SPLIT_DELIM_CAPTURE); - - # If in Markdown span mode, add a empty-string span-level hash - # after each newline to prevent triggering any block element. - if ($span) { - $void = $this->hashPart("", ':'); - $newline = "$void\n"; - $parts[0] = $void . str_replace("\n", $newline, $parts[0]) . $void; - } - - $parsed .= $parts[0]; # Text before current tag. - - # If end of $text has been reached. Stop loop. - if (count($parts) < 3) { - $text = ""; - break; - } - - $tag = $parts[1]; # Tag to handle. - $text = $parts[2]; # Remaining text after current tag. - $tag_re = preg_quote($tag); # For use in a regular expression. - - # - # Check for: Fenced code block marker. - # Note: need to recheck the whole tag to disambiguate backtick - # fences from code spans - # - if (preg_match('{^\n?([ ]{0,'.($indent+3).'})(~{3,}|`{3,})[ ]*(?:\.?[-_:a-zA-Z0-9]+|'.$this->id_class_attr_nocatch_re.')?[ ]*\n?$}', $tag, $capture)) { - # Fenced code block marker: find matching end marker. - $fence_indent = strlen($capture[1]); # use captured indent in re - $fence_re = $capture[2]; # use captured fence in re - if (preg_match('{^(?>.*\n)*?[ ]{'.($fence_indent).'}'.$fence_re.'[ ]*(?:\n|$)}', $text, - $matches)) - { - # End marker found: pass text unchanged until marker. - $parsed .= $tag . $matches[0]; - $text = substr($text, strlen($matches[0])); - } - else { - # No end marker: just skip it. - $parsed .= $tag; - } - } - # - # Check for: Indented code block. - # - else if ($tag{0} == "\n" || $tag{0} == " ") { - # Indented code block: pass it unchanged, will be handled - # later. - $parsed .= $tag; - } - # - # Check for: Code span marker - # Note: need to check this after backtick fenced code blocks - # - else if ($tag{0} == "`") { - # Find corresponding end marker. - $tag_re = preg_quote($tag); - if (preg_match('{^(?>.+?|\n(?!\n))*?(?block_tags_re.')\b}', $tag) || - ( preg_match('{^<(?:'.$this->context_block_tags_re.')\b}', $tag) && - preg_match($newline_before_re, $parsed) && - preg_match($newline_after_re, $text) ) - ) - { - # Need to parse tag and following text using the HTML parser. - list($block_text, $text) = - $this->_hashHTMLBlocks_inHTML($tag . $text, "hashBlock", true); - - # Make sure it stays outside of any paragraph by adding newlines. - $parsed .= "\n\n$block_text\n\n"; - } - # - # Check for: Clean tag (like script, math) - # HTML Comments, processing instructions. - # - else if (preg_match('{^<(?:'.$this->clean_tags_re.')\b}', $tag) || - $tag{1} == '!' || $tag{1} == '?') - { - # Need to parse tag and following text using the HTML parser. - # (don't check for markdown attribute) - list($block_text, $text) = - $this->_hashHTMLBlocks_inHTML($tag . $text, "hashClean", false); - - $parsed .= $block_text; - } - # - # Check for: Tag with same name as enclosing tag. - # - else if ($enclosing_tag_re !== '' && - # Same name as enclosing tag. - preg_match('{^= 0); - - return array($parsed, $text); - } - protected function _hashHTMLBlocks_inHTML($text, $hash_method, $md_attr) { - # - # Parse HTML, calling _HashHTMLBlocks_InMarkdown for block tags. - # - # * Calls $hash_method to convert any blocks. - # * Stops when the first opening tag closes. - # * $md_attr indicate if the use of the `markdown="1"` attribute is allowed. - # (it is not inside clean tags) - # - # Returns an array of that form: ( processed text , remaining text ) - # - if ($text === '') return array('', ''); - - # Regex to match `markdown` attribute inside of a tag. - $markdown_attr_re = ' - { - \s* # Eat whitespace before the `markdown` attribute - markdown - \s*=\s* - (?> - (["\']) # $1: quote delimiter - (.*?) # $2: attribute value - \1 # matching delimiter - | - ([^\s>]*) # $3: unquoted attribute value - ) - () # $4: make $3 always defined (avoid warnings) - }xs'; - - # Regex to match any tag. - $tag_re = '{ - ( # $2: Capture whole tag. - - ".*?" | # Double quotes (can contain `>`) - \'.*?\' | # Single quotes (can contain `>`) - .+? # Anything but quotes and `>`. - )*? - )? - > # End of tag. - | - # HTML Comment - | - <\?.*?\?> | <%.*?%> # Processing instruction - | - # CData Block - ) - }xs'; - - $original_text = $text; # Save original text in case of faliure. - - $depth = 0; # Current depth inside the tag tree. - $block_text = ""; # Temporary text holder for current text. - $parsed = ""; # Parsed text that will be returned. - - # - # Get the name of the starting tag. - # (This pattern makes $base_tag_name_re safe without quoting.) - # - if (preg_match('/^<([\w:$]*)\b/', $text, $matches)) - $base_tag_name_re = $matches[1]; - - # - # Loop through every tag until we find the corresponding closing tag. - # - do { - # - # Split the text using the first $tag_match pattern found. - # Text before pattern will be first in the array, text after - # pattern will be at the end, and between will be any catches made - # by the pattern. - # - $parts = preg_split($tag_re, $text, 2, PREG_SPLIT_DELIM_CAPTURE); - - if (count($parts) < 3) { - # - # End of $text reached with unbalenced tag(s). - # In that case, we return original text unchanged and pass the - # first character as filtered to prevent an infinite loop in the - # parent function. - # - return array($original_text{0}, substr($original_text, 1)); - } - - $block_text .= $parts[0]; # Text before current tag. - $tag = $parts[1]; # Tag to handle. - $text = $parts[2]; # Remaining text after current tag. - - # - # Check for: Auto-close tag (like
    ) - # Comments and Processing Instructions. - # - if (preg_match('{^auto_close_tags_re.')\b}', $tag) || - $tag{1} == '!' || $tag{1} == '?') - { - # Just add the tag to the block as if it was text. - $block_text .= $tag; - } - else { - # - # Increase/decrease nested tag count. Only do so if - # the tag's name match base tag's. - # - if (preg_match('{^mode = $attr_m[2] . $attr_m[3]; - $span_mode = $this->mode == 'span' || $this->mode != 'block' && - preg_match('{^<(?:'.$this->contain_span_tags_re.')\b}', $tag); - - # Calculate indent before tag. - if (preg_match('/(?:^|\n)( *?)(?! ).*?$/', $block_text, $matches)) { - $strlen = $this->utf8_strlen; - $indent = $strlen($matches[1], 'UTF-8'); - } else { - $indent = 0; - } - - # End preceding block with this tag. - $block_text .= $tag; - $parsed .= $this->$hash_method($block_text); - - # Get enclosing tag name for the ParseMarkdown function. - # (This pattern makes $tag_name_re safe without quoting.) - preg_match('/^<([\w:$]*)\b/', $tag, $matches); - $tag_name_re = $matches[1]; - - # Parse the content using the HTML-in-Markdown parser. - list ($block_text, $text) - = $this->_hashHTMLBlocks_inMarkdown($text, $indent, - $tag_name_re, $span_mode); - - # Outdent markdown text. - if ($indent > 0) { - $block_text = preg_replace("/^[ ]{1,$indent}/m", "", - $block_text); - } - - # Append tag content to parsed text. - if (!$span_mode) $parsed .= "\n\n$block_text\n\n"; - else $parsed .= "$block_text"; - - # Start over with a new block. - $block_text = ""; - } - else $block_text .= $tag; - } - - } while ($depth > 0); - - # - # Hash last block text that wasn't processed inside the loop. - # - $parsed .= $this->$hash_method($block_text); - - return array($parsed, $text); - } - - - protected function hashClean($text) { - # - # Called whenever a tag must be hashed when a function inserts a "clean" tag - # in $text, it passes through this function and is automaticaly escaped, - # blocking invalid nested overlap. - # - return $this->hashPart($text, 'C'); - } - - - protected function doAnchors($text) { - # - # Turn Markdown link shortcuts into XHTML tags. - # - if ($this->in_anchor) return $text; - $this->in_anchor = true; - - # - # First, handle reference-style links: [link text] [id] - # - $text = preg_replace_callback('{ - ( # wrap whole match in $1 - \[ - ('.$this->nested_brackets_re.') # link text = $2 - \] - - [ ]? # one optional space - (?:\n[ ]*)? # one optional newline followed by spaces - - \[ - (.*?) # id = $3 - \] - ) - }xs', - array($this, '_doAnchors_reference_callback'), $text); - - # - # Next, inline-style links: [link text](url "optional title") - # - $text = preg_replace_callback('{ - ( # wrap whole match in $1 - \[ - ('.$this->nested_brackets_re.') # link text = $2 - \] - \( # literal paren - [ \n]* - (?: - <(.+?)> # href = $3 - | - ('.$this->nested_url_parenthesis_re.') # href = $4 - ) - [ \n]* - ( # $5 - ([\'"]) # quote char = $6 - (.*?) # Title = $7 - \6 # matching quote - [ \n]* # ignore any spaces/tabs between closing quote and ) - )? # title is optional - \) - (?:[ ]? '.$this->id_class_attr_catch_re.' )? # $8 = id/class attributes - ) - }xs', - array($this, '_doAnchors_inline_callback'), $text); - - # - # Last, handle reference-style shortcuts: [link text] - # These must come last in case you've also got [link text][1] - # or [link text](/foo) - # - $text = preg_replace_callback('{ - ( # wrap whole match in $1 - \[ - ([^\[\]]+) # link text = $2; can\'t contain [ or ] - \] - ) - }xs', - array($this, '_doAnchors_reference_callback'), $text); - - $this->in_anchor = false; - return $text; - } - protected function _doAnchors_reference_callback($matches) { - $whole_match = $matches[1]; - $link_text = $matches[2]; - $link_id =& $matches[3]; - - if ($link_id == "") { - # for shortcut links like [this][] or [this]. - $link_id = $link_text; - } - - # lower-case and turn embedded newlines into spaces - $link_id = strtolower($link_id); - $link_id = preg_replace('{[ ]?\n}', ' ', $link_id); - - if (isset($this->urls[$link_id])) { - $url = $this->urls[$link_id]; - $url = $this->encodeURLAttribute($url); - - $result = "titles[$link_id] ) ) { - $title = $this->titles[$link_id]; - $title = $this->encodeAttribute($title); - $result .= " title=\"$title\""; - } - if (isset($this->ref_attr[$link_id])) - $result .= $this->ref_attr[$link_id]; - - $link_text = $this->runSpanGamut($link_text); - $result .= ">$link_text"; - $result = $this->hashPart($result); - } - else { - $result = $whole_match; - } - return $result; - } - protected function _doAnchors_inline_callback($matches) { - $whole_match = $matches[1]; - $link_text = $this->runSpanGamut($matches[2]); - $url = $matches[3] == '' ? $matches[4] : $matches[3]; - $title =& $matches[7]; - $attr = $this->doExtraAttributes("a", $dummy =& $matches[8]); - - // if the URL was of the form it got caught by the HTML - // tag parser and hashed. Need to reverse the process before using the URL. - $unhashed = $this->unhash($url); - if ($unhashed != $url) - $url = preg_replace('/^<(.*)>$/', '\1', $unhashed); - - $url = $this->encodeURLAttribute($url); - - $result = "encodeAttribute($title); - $result .= " title=\"$title\""; - } - $result .= $attr; - - $link_text = $this->runSpanGamut($link_text); - $result .= ">$link_text"; - - return $this->hashPart($result); - } - - - protected function doImages($text) { - # - # Turn Markdown image shortcuts into tags. - # - # - # First, handle reference-style labeled images: ![alt text][id] - # - $text = preg_replace_callback('{ - ( # wrap whole match in $1 - !\[ - ('.$this->nested_brackets_re.') # alt text = $2 - \] - - [ ]? # one optional space - (?:\n[ ]*)? # one optional newline followed by spaces - - \[ - (.*?) # id = $3 - \] - - ) - }xs', - array($this, '_doImages_reference_callback'), $text); - - # - # Next, handle inline images: ![alt text](url "optional title") - # Don't forget: encode * and _ - # - $text = preg_replace_callback('{ - ( # wrap whole match in $1 - !\[ - ('.$this->nested_brackets_re.') # alt text = $2 - \] - \s? # One optional whitespace character - \( # literal paren - [ \n]* - (?: - <(\S*)> # src url = $3 - | - ('.$this->nested_url_parenthesis_re.') # src url = $4 - ) - [ \n]* - ( # $5 - ([\'"]) # quote char = $6 - (.*?) # title = $7 - \6 # matching quote - [ \n]* - )? # title is optional - \) - (?:[ ]? '.$this->id_class_attr_catch_re.' )? # $8 = id/class attributes - ) - }xs', - array($this, '_doImages_inline_callback'), $text); - - return $text; - } - protected function _doImages_reference_callback($matches) { - $whole_match = $matches[1]; - $alt_text = $matches[2]; - $link_id = strtolower($matches[3]); - - if ($link_id == "") { - $link_id = strtolower($alt_text); # for shortcut links like ![this][]. - } - - $alt_text = $this->encodeAttribute($alt_text); - if (isset($this->urls[$link_id])) { - $url = $this->encodeURLAttribute($this->urls[$link_id]); - $result = "\"$alt_text\"";titles[$link_id])) { - $title = $this->titles[$link_id]; - $title = $this->encodeAttribute($title); - $result .= " title=\"$title\""; - } - if (isset($this->ref_attr[$link_id])) - $result .= $this->ref_attr[$link_id]; - $result .= $this->empty_element_suffix; - $result = $this->hashPart($result); - } - else { - # If there's no such link ID, leave intact: - $result = $whole_match; - } - - return $result; - } - protected function _doImages_inline_callback($matches) { - $whole_match = $matches[1]; - $alt_text = $matches[2]; - $url = $matches[3] == '' ? $matches[4] : $matches[3]; - $title =& $matches[7]; - $attr = $this->doExtraAttributes("img", $dummy =& $matches[8]); - - $alt_text = $this->encodeAttribute($alt_text); - $url = $this->encodeURLAttribute($url); - $result = "\"$alt_text\"";encodeAttribute($title); - $result .= " title=\"$title\""; # $title already quoted - } - $result .= $attr; - $result .= $this->empty_element_suffix; - - return $this->hashPart($result); - } - - - protected function doHeaders($text) { - # - # Redefined to add id and class attribute support. - # - # Setext-style headers: - # Header 1 {#header1} - # ======== - # - # Header 2 {#header2 .class1 .class2} - # -------- - # - $text = preg_replace_callback( - '{ - (^.+?) # $1: Header text - (?:[ ]+ '.$this->id_class_attr_catch_re.' )? # $3 = id/class attributes - [ ]*\n(=+|-+)[ ]*\n+ # $3: Header footer - }mx', - array($this, '_doHeaders_callback_setext'), $text); - - # atx-style headers: - # # Header 1 {#header1} - # ## Header 2 {#header2} - # ## Header 2 with closing hashes ## {#header3.class1.class2} - # ... - # ###### Header 6 {.class2} - # - $text = preg_replace_callback('{ - ^(\#{1,6}) # $1 = string of #\'s - [ ]* - (.+?) # $2 = Header text - [ ]* - \#* # optional closing #\'s (not counted) - (?:[ ]+ '.$this->id_class_attr_catch_re.' )? # $3 = id/class attributes - [ ]* - \n+ - }xm', - array($this, '_doHeaders_callback_atx'), $text); - - return $text; - } - protected function _doHeaders_callback_setext($matches) { - if ($matches[3] == '-' && preg_match('{^- }', $matches[1])) - return $matches[0]; - $level = $matches[3]{0} == '=' ? 1 : 2; - $attr = $this->doExtraAttributes("h$level", $dummy =& $matches[2]); - $block = "".$this->runSpanGamut($matches[1]).""; - return "\n" . $this->hashBlock($block) . "\n\n"; - } - protected function _doHeaders_callback_atx($matches) { - $level = strlen($matches[1]); - $attr = $this->doExtraAttributes("h$level", $dummy =& $matches[3]); - $block = "".$this->runSpanGamut($matches[2]).""; - return "\n" . $this->hashBlock($block) . "\n\n"; - } - - - protected function doTables($text) { - # - # Form HTML tables. - # - $less_than_tab = $this->tab_width - 1; - # - # Find tables with leading pipe. - # - # | Header 1 | Header 2 - # | -------- | -------- - # | Cell 1 | Cell 2 - # | Cell 3 | Cell 4 - # - $text = preg_replace_callback(' - { - ^ # Start of a line - [ ]{0,'.$less_than_tab.'} # Allowed whitespace. - [|] # Optional leading pipe (present) - (.+) \n # $1: Header row (at least one pipe) - - [ ]{0,'.$less_than_tab.'} # Allowed whitespace. - [|] ([ ]*[-:]+[-| :]*) \n # $2: Header underline - - ( # $3: Cells - (?> - [ ]* # Allowed whitespace. - [|] .* \n # Row content. - )* - ) - (?=\n|\Z) # Stop at final double newline. - }xm', - array($this, '_doTable_leadingPipe_callback'), $text); - - # - # Find tables without leading pipe. - # - # Header 1 | Header 2 - # -------- | -------- - # Cell 1 | Cell 2 - # Cell 3 | Cell 4 - # - $text = preg_replace_callback(' - { - ^ # Start of a line - [ ]{0,'.$less_than_tab.'} # Allowed whitespace. - (\S.*[|].*) \n # $1: Header row (at least one pipe) - - [ ]{0,'.$less_than_tab.'} # Allowed whitespace. - ([-:]+[ ]*[|][-| :]*) \n # $2: Header underline - - ( # $3: Cells - (?> - .* [|] .* \n # Row content - )* - ) - (?=\n|\Z) # Stop at final double newline. - }xm', - array($this, '_DoTable_callback'), $text); - - return $text; - } - protected function _doTable_leadingPipe_callback($matches) { - $head = $matches[1]; - $underline = $matches[2]; - $content = $matches[3]; - - # Remove leading pipe for each row. - $content = preg_replace('/^ *[|]/m', '', $content); - - return $this->_doTable_callback(array($matches[0], $head, $underline, $content)); - } - protected function _doTable_makeAlignAttr($alignname) - { - if (empty($this->table_align_class_tmpl)) - return " align=\"$alignname\""; - - $classname = str_replace('%%', $alignname, $this->table_align_class_tmpl); - return " class=\"$classname\""; - } - protected function _doTable_callback($matches) { - $head = $matches[1]; - $underline = $matches[2]; - $content = $matches[3]; - - # Remove any tailing pipes for each line. - $head = preg_replace('/[|] *$/m', '', $head); - $underline = preg_replace('/[|] *$/m', '', $underline); - $content = preg_replace('/[|] *$/m', '', $content); - - # Reading alignement from header underline. - $separators = preg_split('/ *[|] */', $underline); - foreach ($separators as $n => $s) { - if (preg_match('/^ *-+: *$/', $s)) - $attr[$n] = $this->_doTable_makeAlignAttr('right'); - else if (preg_match('/^ *:-+: *$/', $s)) - $attr[$n] = $this->_doTable_makeAlignAttr('center'); - else if (preg_match('/^ *:-+ *$/', $s)) - $attr[$n] = $this->_doTable_makeAlignAttr('left'); - else - $attr[$n] = ''; - } - - # Parsing span elements, including code spans, character escapes, - # and inline HTML tags, so that pipes inside those gets ignored. - $head = $this->parseSpan($head); - $headers = preg_split('/ *[|] */', $head); - $col_count = count($headers); - $attr = array_pad($attr, $col_count, ''); - - # Write column headers. - $text = "\n"; - $text .= "\n"; - $text .= "\n"; - foreach ($headers as $n => $header) - $text .= " ".$this->runSpanGamut(trim($header))."\n"; - $text .= "\n"; - $text .= "\n"; - - # Split content by row. - $rows = explode("\n", trim($content, "\n")); - - $text .= "\n"; - foreach ($rows as $row) { - # Parsing span elements, including code spans, character escapes, - # and inline HTML tags, so that pipes inside those gets ignored. - $row = $this->parseSpan($row); - - # Split row by cell. - $row_cells = preg_split('/ *[|] */', $row, $col_count); - $row_cells = array_pad($row_cells, $col_count, ''); - - $text .= "\n"; - foreach ($row_cells as $n => $cell) - $text .= " ".$this->runSpanGamut(trim($cell))."\n"; - $text .= "\n"; - } - $text .= "\n"; - $text .= "
    "; - - return $this->hashBlock($text) . "\n"; - } - - - protected function doDefLists($text) { - # - # Form HTML definition lists. - # - $less_than_tab = $this->tab_width - 1; - - # Re-usable pattern to match any entire dl list: - $whole_list_re = '(?> - ( # $1 = whole list - ( # $2 - [ ]{0,'.$less_than_tab.'} - ((?>.*\S.*\n)+) # $3 = defined term - \n? - [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition - ) - (?s:.+?) - ( # $4 - \z - | - \n{2,} - (?=\S) - (?! # Negative lookahead for another term - [ ]{0,'.$less_than_tab.'} - (?: \S.*\n )+? # defined term - \n? - [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition - ) - (?! # Negative lookahead for another definition - [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition - ) - ) - ) - )'; // mx - - $text = preg_replace_callback('{ - (?>\A\n?|(?<=\n\n)) - '.$whole_list_re.' - }mx', - array($this, '_doDefLists_callback'), $text); - - return $text; - } - protected function _doDefLists_callback($matches) { - # Re-usable patterns to match list item bullets and number markers: - $list = $matches[1]; - - # Turn double returns into triple returns, so that we can make a - # paragraph for the last item in a list, if necessary: - $result = trim($this->processDefListItems($list)); - $result = "
    \n" . $result . "\n
    "; - return $this->hashBlock($result) . "\n\n"; - } - - - protected function processDefListItems($list_str) { - # - # Process the contents of a single definition list, splitting it - # into individual term and definition list items. - # - $less_than_tab = $this->tab_width - 1; - - # trim trailing blank lines: - $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str); - - # Process definition terms. - $list_str = preg_replace_callback('{ - (?>\A\n?|\n\n+) # leading line - ( # definition terms = $1 - [ ]{0,'.$less_than_tab.'} # leading whitespace - (?!\:[ ]|[ ]) # negative lookahead for a definition - # mark (colon) or more whitespace. - (?> \S.* \n)+? # actual term (not whitespace). - ) - (?=\n?[ ]{0,3}:[ ]) # lookahead for following line feed - # with a definition mark. - }xm', - array($this, '_processDefListItems_callback_dt'), $list_str); - - # Process actual definitions. - $list_str = preg_replace_callback('{ - \n(\n+)? # leading line = $1 - ( # marker space = $2 - [ ]{0,'.$less_than_tab.'} # whitespace before colon - \:[ ]+ # definition mark (colon) - ) - ((?s:.+?)) # definition text = $3 - (?= \n+ # stop at next definition mark, - (?: # next term or end of text - [ ]{0,'.$less_than_tab.'} \:[ ] | -
    | \z - ) - ) - }xm', - array($this, '_processDefListItems_callback_dd'), $list_str); - - return $list_str; - } - protected function _processDefListItems_callback_dt($matches) { - $terms = explode("\n", trim($matches[1])); - $text = ''; - foreach ($terms as $term) { - $term = $this->runSpanGamut(trim($term)); - $text .= "\n
    " . $term . "
    "; - } - return $text . "\n"; - } - protected function _processDefListItems_callback_dd($matches) { - $leading_line = $matches[1]; - $marker_space = $matches[2]; - $def = $matches[3]; - - if ($leading_line || preg_match('/\n{2,}/', $def)) { - # Replace marker with the appropriate whitespace indentation - $def = str_repeat(' ', strlen($marker_space)) . $def; - $def = $this->runBlockGamut($this->outdent($def . "\n\n")); - $def = "\n". $def ."\n"; - } - else { - $def = rtrim($def); - $def = $this->runSpanGamut($this->outdent($def)); - } - - return "\n
    " . $def . "
    \n"; - } - - - protected function doFencedCodeBlocks($text) { - # - # Adding the fenced code block syntax to regular Markdown: - # - # ~~~ - # Code block - # ~~~ - # - $less_than_tab = $this->tab_width; - - $text = preg_replace_callback('{ - (?:\n|\A) - # 1: Opening marker - ( - (?:~{3,}|`{3,}) # 3 or more tildes/backticks. - ) - [ ]* - (?: - \.?([-_:a-zA-Z0-9]+) # 2: standalone class name - | - '.$this->id_class_attr_catch_re.' # 3: Extra attributes - )? - [ ]* \n # Whitespace and newline following marker. - - # 4: Content - ( - (?> - (?!\1 [ ]* \n) # Not a closing marker. - .*\n+ - )+ - ) - - # Closing marker. - \1 [ ]* (?= \n ) - }xm', - array($this, '_doFencedCodeBlocks_callback'), $text); - - return $text; - } - protected function _doFencedCodeBlocks_callback($matches) { - $classname =& $matches[2]; - $attrs =& $matches[3]; - $codeblock = $matches[4]; - $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES); - $codeblock = preg_replace_callback('/^\n+/', - array($this, '_doFencedCodeBlocks_newlines'), $codeblock); - - if ($classname != "") { - if ($classname{0} == '.') - $classname = substr($classname, 1); - $attr_str = ' class="'.$this->code_class_prefix.$classname.'"'; - } else { - $attr_str = $this->doExtraAttributes($this->code_attr_on_pre ? "pre" : "code", $attrs); - } - $pre_attr_str = $this->code_attr_on_pre ? $attr_str : ''; - $code_attr_str = $this->code_attr_on_pre ? '' : $attr_str; - $codeblock = "$codeblock
    "; - - return "\n\n".$this->hashBlock($codeblock)."\n\n"; - } - protected function _doFencedCodeBlocks_newlines($matches) { - return str_repeat("empty_element_suffix", - strlen($matches[0])); - } - - - # - # Redefining emphasis markers so that emphasis by underscore does not - # work in the middle of a word. - # - protected $em_relist = array( - '' => '(?:(? '(? '(? '(?:(? '(? '(? '(?:(? '(? '(? tags - # - # Strip leading and trailing lines: - $text = preg_replace('/\A\n+|\n+\z/', '', $text); - - $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY); - - # - # Wrap

    tags and unhashify HTML blocks - # - foreach ($grafs as $key => $value) { - $value = trim($this->runSpanGamut($value)); - - # Check if this should be enclosed in a paragraph. - # Clean tag hashes & block tag hashes are left alone. - $is_p = !preg_match('/^B\x1A[0-9]+B|^C\x1A[0-9]+C$/', $value); - - if ($is_p) { - $value = "

    $value

    "; - } - $grafs[$key] = $value; - } - - # Join grafs in one text, then unhash HTML tags. - $text = implode("\n\n", $grafs); - - # Finish by removing any tag hashes still present in $text. - $text = $this->unhash($text); - - return $text; - } - - - ### Footnotes - - protected function stripFootnotes($text) { - # - # Strips link definitions from text, stores the URLs and titles in - # hash references. - # - $less_than_tab = $this->tab_width - 1; - - # Link defs are in the form: [^id]: url "optional title" - $text = preg_replace_callback('{ - ^[ ]{0,'.$less_than_tab.'}\[\^(.+?)\][ ]?: # note_id = $1 - [ ]* - \n? # maybe *one* newline - ( # text = $2 (no blank lines allowed) - (?: - .+ # actual text - | - \n # newlines but - (?!\[.+?\][ ]?:\s)# negative lookahead for footnote or link definition marker. - (?!\n+[ ]{0,3}\S)# ensure line is not blank and followed - # by non-indented content - )* - ) - }xm', - array($this, '_stripFootnotes_callback'), - $text); - return $text; - } - protected function _stripFootnotes_callback($matches) { - $note_id = $this->fn_id_prefix . $matches[1]; - $this->footnotes[$note_id] = $this->outdent($matches[2]); - return ''; # String that will replace the block - } - - - protected function doFootnotes($text) { - # - # Replace footnote references in $text [^id] with a special text-token - # which will be replaced by the actual footnote marker in appendFootnotes. - # - if (!$this->in_anchor) { - $text = preg_replace('{\[\^(.+?)\]}', "F\x1Afn:\\1\x1A:", $text); - } - return $text; - } - - - protected function appendFootnotes($text) { - # - # Append footnote list to text. - # - $text = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}', - array($this, '_appendFootnotes_callback'), $text); - - if (!empty($this->footnotes_ordered)) { - $text .= "\n\n"; - $text .= "
    \n"; - $text .= "empty_element_suffix ."\n"; - $text .= "
      \n\n"; - - $attr = ""; - if ($this->fn_backlink_class != "") { - $class = $this->fn_backlink_class; - $class = $this->encodeAttribute($class); - $attr .= " class=\"$class\""; - } - if ($this->fn_backlink_title != "") { - $title = $this->fn_backlink_title; - $title = $this->encodeAttribute($title); - $attr .= " title=\"$title\""; - } - $num = 0; - - while (!empty($this->footnotes_ordered)) { - $footnote = reset($this->footnotes_ordered); - $note_id = key($this->footnotes_ordered); - unset($this->footnotes_ordered[$note_id]); - $ref_count = $this->footnotes_ref_count[$note_id]; - unset($this->footnotes_ref_count[$note_id]); - unset($this->footnotes[$note_id]); - - $footnote .= "\n"; # Need to append newline before parsing. - $footnote = $this->runBlockGamut("$footnote\n"); - $footnote = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}', - array($this, '_appendFootnotes_callback'), $footnote); - - $attr = str_replace("%%", ++$num, $attr); - $note_id = $this->encodeAttribute($note_id); - - # Prepare backlink, multiple backlinks if multiple references - $backlink = ""; - for ($ref_num = 2; $ref_num <= $ref_count; ++$ref_num) { - $backlink .= " "; - } - # Add backlink to last paragraph; create new paragraph if needed. - if (preg_match('{

      $}', $footnote)) { - $footnote = substr($footnote, 0, -4) . " $backlink

      "; - } else { - $footnote .= "\n\n

      $backlink

      "; - } - - $text .= "
    1. \n"; - $text .= $footnote . "\n"; - $text .= "
    2. \n\n"; - } - - $text .= "
    \n"; - $text .= "
    "; - } - return $text; - } - protected function _appendFootnotes_callback($matches) { - $node_id = $this->fn_id_prefix . $matches[1]; - - # Create footnote marker only if it has a corresponding footnote *and* - # the footnote hasn't been used by another marker. - if (isset($this->footnotes[$node_id])) { - $num =& $this->footnotes_numbers[$node_id]; - if (!isset($num)) { - # Transfer footnote content to the ordered list and give it its - # number - $this->footnotes_ordered[$node_id] = $this->footnotes[$node_id]; - $this->footnotes_ref_count[$node_id] = 1; - $num = $this->footnote_counter++; - $ref_count_mark = ''; - } else { - $ref_count_mark = $this->footnotes_ref_count[$node_id] += 1; - } - - $attr = ""; - if ($this->fn_link_class != "") { - $class = $this->fn_link_class; - $class = $this->encodeAttribute($class); - $attr .= " class=\"$class\""; - } - if ($this->fn_link_title != "") { - $title = $this->fn_link_title; - $title = $this->encodeAttribute($title); - $attr .= " title=\"$title\""; - } - - $attr = str_replace("%%", $num, $attr); - $node_id = $this->encodeAttribute($node_id); - - return - "". - "$num". - ""; - } - - return "[^".$matches[1]."]"; - } - - - ### Abbreviations ### - - protected function stripAbbreviations($text) { - # - # Strips abbreviations from text, stores titles in hash references. - # - $less_than_tab = $this->tab_width - 1; - - # Link defs are in the form: [id]*: url "optional title" - $text = preg_replace_callback('{ - ^[ ]{0,'.$less_than_tab.'}\*\[(.+?)\][ ]?: # abbr_id = $1 - (.*) # text = $2 (no blank lines allowed) - }xm', - array($this, '_stripAbbreviations_callback'), - $text); - return $text; - } - protected function _stripAbbreviations_callback($matches) { - $abbr_word = $matches[1]; - $abbr_desc = $matches[2]; - if ($this->abbr_word_re) - $this->abbr_word_re .= '|'; - $this->abbr_word_re .= preg_quote($abbr_word); - $this->abbr_desciptions[$abbr_word] = trim($abbr_desc); - return ''; # String that will replace the block - } - - - protected function doAbbreviations($text) { - # - # Find defined abbreviations in text and wrap them in elements. - # - if ($this->abbr_word_re) { - // cannot use the /x modifier because abbr_word_re may - // contain significant spaces: - $text = preg_replace_callback('{'. - '(?abbr_word_re.')'. - '(?![\w\x1A])'. - '}', - array($this, '_doAbbreviations_callback'), $text); - } - return $text; - } - protected function _doAbbreviations_callback($matches) { - $abbr = $matches[0]; - if (isset($this->abbr_desciptions[$abbr])) { - $desc = $this->abbr_desciptions[$abbr]; - if (empty($desc)) { - return $this->hashPart("$abbr"); - } else { - $desc = $this->encodeAttribute($desc); - return $this->hashPart("$abbr"); - } - } else { - return $matches[0]; - } - } - } diff --git a/library/php-markdown/Michelf/MarkdownExtra.inc.php b/library/php-markdown/Michelf/MarkdownExtra.inc.php index e11b1ef97..d09bd7a48 100644 --- a/library/php-markdown/Michelf/MarkdownExtra.inc.php +++ b/library/php-markdown/Michelf/MarkdownExtra.inc.php @@ -1,10 +1,10 @@ -# -# Original Markdown -# Copyright (c) 2004-2006 John Gruber -# -# +/** + * Markdown Extra - A text-to-HTML conversion tool for web writers + * + * @package php-markdown + * @author Michel Fortin + * @copyright 2004-2016 Michel Fortin + * @copyright (Original Markdown) 2004-2006 John Gruber + */ + namespace Michelf; +/** + * Markdown Extra Parser Class + */ +class MarkdownExtra extends \Michelf\Markdown { + /** + * Configuration variables + */ -# Just force Michelf/Markdown.php to load. This is needed to load -# the temporary implementation class. See below for details. -\Michelf\Markdown::MARKDOWNLIB_VERSION; + /** + * Prefix for footnote ids. + * @var string + */ + public $fn_id_prefix = ""; + + /** + * Optional title attribute for footnote links and backlinks. + * @var string + */ + public $fn_link_title = ""; + public $fn_backlink_title = ""; + + /** + * Optional class attribute for footnote links and backlinks. + * @var string + */ + public $fn_link_class = "footnote-ref"; + public $fn_backlink_class = "footnote-backref"; -# -# Markdown Extra Parser Class -# -# Note: Currently the implementation resides in the temporary class -# \Michelf\MarkdownExtra_TmpImpl (in the same file as \Michelf\Markdown). -# This makes it easier to propagate the changes between the three different -# packaging styles of PHP Markdown. Once this issue is resolved, the -# _MarkdownExtra_TmpImpl will disappear and this one will contain the code. -# + /** + * Content to be displayed within footnote backlinks. The default is '↩'; + * the U+FE0E on the end is a Unicode variant selector used to prevent iOS + * from displaying the arrow character as an emoji. + * @var string + */ + public $fn_backlink_html = '↩︎'; -class MarkdownExtra extends \Michelf\_MarkdownExtra_TmpImpl { + /** + * Class name for table cell alignment (%% replaced left/center/right) + * For instance: 'go-%%' becomes 'go-left' or 'go-right' or 'go-center' + * If empty, the align attribute is used instead of a class name. + * @var string + */ + public $table_align_class_tmpl = ''; - ### Parser Implementation ### + /** + * Optional class prefix for fenced code block. + * @var string + */ + public $code_class_prefix = ""; - # Temporarily, the implemenation is in the _MarkdownExtra_TmpImpl class. - # See note above. + /** + * Class attribute for code blocks goes on the `code` tag; + * setting this to true will put attributes on the `pre` tag instead. + * @var boolean + */ + public $code_attr_on_pre = false; + /** + * Predefined abbreviations. + * @var array + */ + public $predef_abbr = array(); + + /** + * Parser implementation + */ + + /** + * Constructor function. Initialize the parser object. + * @return void + */ + public function __construct() { + // Add extra escapable characters before parent constructor + // initialize the table. + $this->escape_chars .= ':|'; + + // Insert extra document, block, and span transformations. + // Parent constructor will do the sorting. + $this->document_gamut += array( + "doFencedCodeBlocks" => 5, + "stripFootnotes" => 15, + "stripAbbreviations" => 25, + "appendFootnotes" => 50, + ); + $this->block_gamut += array( + "doFencedCodeBlocks" => 5, + "doTables" => 15, + "doDefLists" => 45, + ); + $this->span_gamut += array( + "doFootnotes" => 5, + "doAbbreviations" => 70, + ); + + $this->enhanced_ordered_list = true; + parent::__construct(); + } + + + /** + * Extra variables used during extra transformations. + * @var array + */ + protected $footnotes = array(); + protected $footnotes_ordered = array(); + protected $footnotes_ref_count = array(); + protected $footnotes_numbers = array(); + protected $abbr_desciptions = array(); + /** @var string */ + protected $abbr_word_re = ''; + + /** + * Give the current footnote number. + * @var integer + */ + protected $footnote_counter = 1; + + /** + * Setting up Extra-specific variables. + */ + protected function setup() { + parent::setup(); + + $this->footnotes = array(); + $this->footnotes_ordered = array(); + $this->footnotes_ref_count = array(); + $this->footnotes_numbers = array(); + $this->abbr_desciptions = array(); + $this->abbr_word_re = ''; + $this->footnote_counter = 1; + + foreach ($this->predef_abbr as $abbr_word => $abbr_desc) { + if ($this->abbr_word_re) + $this->abbr_word_re .= '|'; + $this->abbr_word_re .= preg_quote($abbr_word); + $this->abbr_desciptions[$abbr_word] = trim($abbr_desc); + } + } + + /** + * Clearing Extra-specific variables. + */ + protected function teardown() { + $this->footnotes = array(); + $this->footnotes_ordered = array(); + $this->footnotes_ref_count = array(); + $this->footnotes_numbers = array(); + $this->abbr_desciptions = array(); + $this->abbr_word_re = ''; + + parent::teardown(); + } + + + /** + * Extra attribute parser + */ + + /** + * Expression to use to catch attributes (includes the braces) + * @var string + */ + protected $id_class_attr_catch_re = '\{((?>[ ]*[#.a-z][-_:a-zA-Z0-9=]+){1,})[ ]*\}'; + + /** + * Expression to use when parsing in a context when no capture is desired + * @var string + */ + protected $id_class_attr_nocatch_re = '\{(?>[ ]*[#.a-z][-_:a-zA-Z0-9=]+){1,}[ ]*\}'; + + /** + * Parse attributes caught by the $this->id_class_attr_catch_re expression + * and return the HTML-formatted list of attributes. + * + * Currently supported attributes are .class and #id. + * + * In addition, this method also supports supplying a default Id value, + * which will be used to populate the id attribute in case it was not + * overridden. + * @param string $tag_name + * @param string $attr + * @param mixed $defaultIdValue + * @param array $classes + * @return string + */ + protected function doExtraAttributes($tag_name, $attr, $defaultIdValue = null, $classes = array()) { + if (empty($attr) && !$defaultIdValue && empty($classes)) return ""; + + // Split on components + preg_match_all('/[#.a-z][-_:a-zA-Z0-9=]+/', $attr, $matches); + $elements = $matches[0]; + + // Handle classes and IDs (only first ID taken into account) + $attributes = array(); + $id = false; + foreach ($elements as $element) { + if ($element{0} == '.') { + $classes[] = substr($element, 1); + } else if ($element{0} == '#') { + if ($id === false) $id = substr($element, 1); + } else if (strpos($element, '=') > 0) { + $parts = explode('=', $element, 2); + $attributes[] = $parts[0] . '="' . $parts[1] . '"'; + } + } + + if (!$id) $id = $defaultIdValue; + + // Compose attributes as string + $attr_str = ""; + if (!empty($id)) { + $attr_str .= ' id="'.$this->encodeAttribute($id) .'"'; + } + if (!empty($classes)) { + $attr_str .= ' class="'. implode(" ", $classes) . '"'; + } + if (!$this->no_markup && !empty($attributes)) { + $attr_str .= ' '.implode(" ", $attributes); + } + return $attr_str; + } + + /** + * Strips link definitions from text, stores the URLs and titles in + * hash references. + * @param string $text + * @return string + */ + protected function stripLinkDefinitions($text) { + $less_than_tab = $this->tab_width - 1; + + // Link defs are in the form: ^[id]: url "optional title" + $text = preg_replace_callback('{ + ^[ ]{0,'.$less_than_tab.'}\[(.+)\][ ]?: # id = $1 + [ ]* + \n? # maybe *one* newline + [ ]* + (?: + <(.+?)> # url = $2 + | + (\S+?) # url = $3 + ) + [ ]* + \n? # maybe one newline + [ ]* + (?: + (?<=\s) # lookbehind for whitespace + ["(] + (.*?) # title = $4 + [")] + [ ]* + )? # title is optional + (?:[ ]* '.$this->id_class_attr_catch_re.' )? # $5 = extra id & class attr + (?:\n+|\Z) + }xm', + array($this, '_stripLinkDefinitions_callback'), + $text); + return $text; + } + + /** + * Strip link definition callback + * @param array $matches + * @return string + */ + protected function _stripLinkDefinitions_callback($matches) { + $link_id = strtolower($matches[1]); + $url = $matches[2] == '' ? $matches[3] : $matches[2]; + $this->urls[$link_id] = $url; + $this->titles[$link_id] =& $matches[4]; + $this->ref_attr[$link_id] = $this->doExtraAttributes("", $dummy =& $matches[5]); + return ''; // String that will replace the block + } + + + /** + * HTML block parser + */ + + /** + * Tags that are always treated as block tags + * @var string + */ + protected $block_tags_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend|article|section|nav|aside|hgroup|header|footer|figcaption|figure'; + + /** + * Tags treated as block tags only if the opening tag is alone on its line + * @var string + */ + protected $context_block_tags_re = 'script|noscript|style|ins|del|iframe|object|source|track|param|math|svg|canvas|audio|video'; + + /** + * Tags where markdown="1" default to span mode: + * @var string + */ + protected $contain_span_tags_re = 'p|h[1-6]|li|dd|dt|td|th|legend|address'; + + /** + * Tags which must not have their contents modified, no matter where + * they appear + * @var string + */ + protected $clean_tags_re = 'script|style|math|svg'; + + /** + * Tags that do not need to be closed. + * @var string + */ + protected $auto_close_tags_re = 'hr|img|param|source|track'; + + /** + * Hashify HTML Blocks and "clean tags". + * + * We only want to do this for block-level HTML tags, such as headers, + * lists, and tables. That's because we still want to wrap

    s around + * "paragraphs" that are wrapped in non-block-level tags, such as anchors, + * phrase emphasis, and spans. The list of tags we're looking for is + * hard-coded. + * + * This works by calling _HashHTMLBlocks_InMarkdown, which then calls + * _HashHTMLBlocks_InHTML when it encounter block tags. When the markdown="1" + * attribute is found within a tag, _HashHTMLBlocks_InHTML calls back + * _HashHTMLBlocks_InMarkdown to handle the Markdown syntax within the tag. + * These two functions are calling each other. It's recursive! + * @param string $text + * @return string + */ + protected function hashHTMLBlocks($text) { + if ($this->no_markup) { + return $text; + } + + // Call the HTML-in-Markdown hasher. + list($text, ) = $this->_hashHTMLBlocks_inMarkdown($text); + + return $text; + } + + /** + * Parse markdown text, calling _HashHTMLBlocks_InHTML for block tags. + * + * * $indent is the number of space to be ignored when checking for code + * blocks. This is important because if we don't take the indent into + * account, something like this (which looks right) won't work as expected: + * + *

    + *
    + * Hello World. <-- Is this a Markdown code block or text? + *
    <-- Is this a Markdown code block or a real tag? + *
    + * + * If you don't like this, just don't indent the tag on which + * you apply the markdown="1" attribute. + * + * * If $enclosing_tag_re is not empty, stops at the first unmatched closing + * tag with that name. Nested tags supported. + * + * * If $span is true, text inside must treated as span. So any double + * newline will be replaced by a single newline so that it does not create + * paragraphs. + * + * Returns an array of that form: ( processed text , remaining text ) + * + * @param string $text + * @param integer $indent + * @param string $enclosing_tag_re + * @param boolean $span + * @return array + */ + protected function _hashHTMLBlocks_inMarkdown($text, $indent = 0, + $enclosing_tag_re = '', $span = false) + { + + if ($text === '') return array('', ''); + + // Regex to check for the presense of newlines around a block tag. + $newline_before_re = '/(?:^\n?|\n\n)*$/'; + $newline_after_re = + '{ + ^ # Start of text following the tag. + (?>[ ]*)? # Optional comment. + [ ]*\n # Must be followed by newline. + }xs'; + + // Regex to match any tag. + $block_tag_re = + '{ + ( # $2: Capture whole tag. + # Tag name. + ' . $this->block_tags_re . ' | + ' . $this->context_block_tags_re . ' | + ' . $this->clean_tags_re . ' | + (?!\s)'.$enclosing_tag_re . ' + ) + (?: + (?=[\s"\'/a-zA-Z0-9]) # Allowed characters after tag name. + (?> + ".*?" | # Double quotes (can contain `>`) + \'.*?\' | # Single quotes (can contain `>`) + .+? # Anything but quotes and `>`. + )*? + )? + > # End of tag. + | + # HTML Comment + | + <\?.*?\?> | <%.*?%> # Processing instruction + | + # CData Block + ' . ( !$span ? ' # If not in span. + | + # Indented code block + (?: ^[ ]*\n | ^ | \n[ ]*\n ) + [ ]{' . ($indent + 4) . '}[^\n]* \n + (?> + (?: [ ]{' . ($indent + 4) . '}[^\n]* | [ ]* ) \n + )* + | + # Fenced code block marker + (?<= ^ | \n ) + [ ]{0,' . ($indent + 3) . '}(?:~{3,}|`{3,}) + [ ]* + (?: \.?[-_:a-zA-Z0-9]+ )? # standalone class name + [ ]* + (?: ' . $this->id_class_attr_nocatch_re . ' )? # extra attributes + [ ]* + (?= \n ) + ' : '' ) . ' # End (if not is span). + | + # Code span marker + # Note, this regex needs to go after backtick fenced + # code blocks but it should also be kept outside of the + # "if not in span" condition adding backticks to the parser + `+ + ) + }xs'; + + + $depth = 0; // Current depth inside the tag tree. + $parsed = ""; // Parsed text that will be returned. + + // Loop through every tag until we find the closing tag of the parent + // or loop until reaching the end of text if no parent tag specified. + do { + // Split the text using the first $tag_match pattern found. + // Text before pattern will be first in the array, text after + // pattern will be at the end, and between will be any catches made + // by the pattern. + $parts = preg_split($block_tag_re, $text, 2, + PREG_SPLIT_DELIM_CAPTURE); + + // If in Markdown span mode, add a empty-string span-level hash + // after each newline to prevent triggering any block element. + if ($span) { + $void = $this->hashPart("", ':'); + $newline = "\n$void"; + $parts[0] = $void . str_replace("\n", $newline, $parts[0]) . $void; + } + + $parsed .= $parts[0]; // Text before current tag. + + // If end of $text has been reached. Stop loop. + if (count($parts) < 3) { + $text = ""; + break; + } + + $tag = $parts[1]; // Tag to handle. + $text = $parts[2]; // Remaining text after current tag. + $tag_re = preg_quote($tag); // For use in a regular expression. + + // Check for: Fenced code block marker. + // Note: need to recheck the whole tag to disambiguate backtick + // fences from code spans + if (preg_match('{^\n?([ ]{0,' . ($indent + 3) . '})(~{3,}|`{3,})[ ]*(?:\.?[-_:a-zA-Z0-9]+)?[ ]*(?:' . $this->id_class_attr_nocatch_re . ')?[ ]*\n?$}', $tag, $capture)) { + // Fenced code block marker: find matching end marker. + $fence_indent = strlen($capture[1]); // use captured indent in re + $fence_re = $capture[2]; // use captured fence in re + if (preg_match('{^(?>.*\n)*?[ ]{' . ($fence_indent) . '}' . $fence_re . '[ ]*(?:\n|$)}', $text, + $matches)) + { + // End marker found: pass text unchanged until marker. + $parsed .= $tag . $matches[0]; + $text = substr($text, strlen($matches[0])); + } + else { + // No end marker: just skip it. + $parsed .= $tag; + } + } + // Check for: Indented code block. + else if ($tag{0} == "\n" || $tag{0} == " ") { + // Indented code block: pass it unchanged, will be handled + // later. + $parsed .= $tag; + } + // Check for: Code span marker + // Note: need to check this after backtick fenced code blocks + else if ($tag{0} == "`") { + // Find corresponding end marker. + $tag_re = preg_quote($tag); + if (preg_match('{^(?>.+?|\n(?!\n))*?(?block_tags_re . ')\b}', $tag) || + ( preg_match('{^<(?:' . $this->context_block_tags_re . ')\b}', $tag) && + preg_match($newline_before_re, $parsed) && + preg_match($newline_after_re, $text) ) + ) + { + // Need to parse tag and following text using the HTML parser. + list($block_text, $text) = + $this->_hashHTMLBlocks_inHTML($tag . $text, "hashBlock", true); + + // Make sure it stays outside of any paragraph by adding newlines. + $parsed .= "\n\n$block_text\n\n"; + } + // Check for: Clean tag (like script, math) + // HTML Comments, processing instructions. + else if (preg_match('{^<(?:' . $this->clean_tags_re . ')\b}', $tag) || + $tag{1} == '!' || $tag{1} == '?') + { + // Need to parse tag and following text using the HTML parser. + // (don't check for markdown attribute) + list($block_text, $text) = + $this->_hashHTMLBlocks_inHTML($tag . $text, "hashClean", false); + + $parsed .= $block_text; + } + // Check for: Tag with same name as enclosing tag. + else if ($enclosing_tag_re !== '' && + // Same name as enclosing tag. + preg_match('{^= 0); + + return array($parsed, $text); + } + + /** + * Parse HTML, calling _HashHTMLBlocks_InMarkdown for block tags. + * + * * Calls $hash_method to convert any blocks. + * * Stops when the first opening tag closes. + * * $md_attr indicate if the use of the `markdown="1"` attribute is allowed. + * (it is not inside clean tags) + * + * Returns an array of that form: ( processed text , remaining text ) + * @param string $text + * @param string $hash_method + * @param string $md_attr + * @return array + */ + protected function _hashHTMLBlocks_inHTML($text, $hash_method, $md_attr) { + if ($text === '') return array('', ''); + + // Regex to match `markdown` attribute inside of a tag. + $markdown_attr_re = ' + { + \s* # Eat whitespace before the `markdown` attribute + markdown + \s*=\s* + (?> + (["\']) # $1: quote delimiter + (.*?) # $2: attribute value + \1 # matching delimiter + | + ([^\s>]*) # $3: unquoted attribute value + ) + () # $4: make $3 always defined (avoid warnings) + }xs'; + + // Regex to match any tag. + $tag_re = '{ + ( # $2: Capture whole tag. + + ".*?" | # Double quotes (can contain `>`) + \'.*?\' | # Single quotes (can contain `>`) + .+? # Anything but quotes and `>`. + )*? + )? + > # End of tag. + | + # HTML Comment + | + <\?.*?\?> | <%.*?%> # Processing instruction + | + # CData Block + ) + }xs'; + + $original_text = $text; // Save original text in case of faliure. + + $depth = 0; // Current depth inside the tag tree. + $block_text = ""; // Temporary text holder for current text. + $parsed = ""; // Parsed text that will be returned. + + // Get the name of the starting tag. + // (This pattern makes $base_tag_name_re safe without quoting.) + if (preg_match('/^<([\w:$]*)\b/', $text, $matches)) + $base_tag_name_re = $matches[1]; + + // Loop through every tag until we find the corresponding closing tag. + do { + // Split the text using the first $tag_match pattern found. + // Text before pattern will be first in the array, text after + // pattern will be at the end, and between will be any catches made + // by the pattern. + $parts = preg_split($tag_re, $text, 2, PREG_SPLIT_DELIM_CAPTURE); + + if (count($parts) < 3) { + // End of $text reached with unbalenced tag(s). + // In that case, we return original text unchanged and pass the + // first character as filtered to prevent an infinite loop in the + // parent function. + return array($original_text{0}, substr($original_text, 1)); + } + + $block_text .= $parts[0]; // Text before current tag. + $tag = $parts[1]; // Tag to handle. + $text = $parts[2]; // Remaining text after current tag. + + // Check for: Auto-close tag (like
    ) + // Comments and Processing Instructions. + if (preg_match('{^auto_close_tags_re . ')\b}', $tag) || + $tag{1} == '!' || $tag{1} == '?') + { + // Just add the tag to the block as if it was text. + $block_text .= $tag; + } + else { + // Increase/decrease nested tag count. Only do so if + // the tag's name match base tag's. + if (preg_match('{^mode = $attr_m[2] . $attr_m[3]; + $span_mode = $this->mode == 'span' || $this->mode != 'block' && + preg_match('{^<(?:' . $this->contain_span_tags_re . ')\b}', $tag); + + // Calculate indent before tag. + if (preg_match('/(?:^|\n)( *?)(?! ).*?$/', $block_text, $matches)) { + $strlen = $this->utf8_strlen; + $indent = $strlen($matches[1], 'UTF-8'); + } else { + $indent = 0; + } + + // End preceding block with this tag. + $block_text .= $tag; + $parsed .= $this->$hash_method($block_text); + + // Get enclosing tag name for the ParseMarkdown function. + // (This pattern makes $tag_name_re safe without quoting.) + preg_match('/^<([\w:$]*)\b/', $tag, $matches); + $tag_name_re = $matches[1]; + + // Parse the content using the HTML-in-Markdown parser. + list ($block_text, $text) + = $this->_hashHTMLBlocks_inMarkdown($text, $indent, + $tag_name_re, $span_mode); + + // Outdent markdown text. + if ($indent > 0) { + $block_text = preg_replace("/^[ ]{1,$indent}/m", "", + $block_text); + } + + // Append tag content to parsed text. + if (!$span_mode) $parsed .= "\n\n$block_text\n\n"; + else $parsed .= "$block_text"; + + // Start over with a new block. + $block_text = ""; + } + else $block_text .= $tag; + } + + } while ($depth > 0); + + // Hash last block text that wasn't processed inside the loop. + $parsed .= $this->$hash_method($block_text); + + return array($parsed, $text); + } + + /** + * Called whenever a tag must be hashed when a function inserts a "clean" tag + * in $text, it passes through this function and is automaticaly escaped, + * blocking invalid nested overlap. + * @param string $text + * @return string + */ + protected function hashClean($text) { + return $this->hashPart($text, 'C'); + } + + /** + * Turn Markdown link shortcuts into XHTML tags. + * @param string $text + * @return string + */ + protected function doAnchors($text) { + if ($this->in_anchor) { + return $text; + } + $this->in_anchor = true; + + // First, handle reference-style links: [link text] [id] + $text = preg_replace_callback('{ + ( # wrap whole match in $1 + \[ + (' . $this->nested_brackets_re . ') # link text = $2 + \] + + [ ]? # one optional space + (?:\n[ ]*)? # one optional newline followed by spaces + + \[ + (.*?) # id = $3 + \] + ) + }xs', + array($this, '_doAnchors_reference_callback'), $text); + + // Next, inline-style links: [link text](url "optional title") + $text = preg_replace_callback('{ + ( # wrap whole match in $1 + \[ + (' . $this->nested_brackets_re . ') # link text = $2 + \] + \( # literal paren + [ \n]* + (?: + <(.+?)> # href = $3 + | + (' . $this->nested_url_parenthesis_re . ') # href = $4 + ) + [ \n]* + ( # $5 + ([\'"]) # quote char = $6 + (.*?) # Title = $7 + \6 # matching quote + [ \n]* # ignore any spaces/tabs between closing quote and ) + )? # title is optional + \) + (?:[ ]? ' . $this->id_class_attr_catch_re . ' )? # $8 = id/class attributes + ) + }xs', + array($this, '_doAnchors_inline_callback'), $text); + + // Last, handle reference-style shortcuts: [link text] + // These must come last in case you've also got [link text][1] + // or [link text](/foo) + $text = preg_replace_callback('{ + ( # wrap whole match in $1 + \[ + ([^\[\]]+) # link text = $2; can\'t contain [ or ] + \] + ) + }xs', + array($this, '_doAnchors_reference_callback'), $text); + + $this->in_anchor = false; + return $text; + } + + /** + * Callback for reference anchors + * @param array $matches + * @return string + */ + protected function _doAnchors_reference_callback($matches) { + $whole_match = $matches[1]; + $link_text = $matches[2]; + $link_id =& $matches[3]; + + if ($link_id == "") { + // for shortcut links like [this][] or [this]. + $link_id = $link_text; + } + + // lower-case and turn embedded newlines into spaces + $link_id = strtolower($link_id); + $link_id = preg_replace('{[ ]?\n}', ' ', $link_id); + + if (isset($this->urls[$link_id])) { + $url = $this->urls[$link_id]; + $url = $this->encodeURLAttribute($url); + + $result = "titles[$link_id] ) ) { + $title = $this->titles[$link_id]; + $title = $this->encodeAttribute($title); + $result .= " title=\"$title\""; + } + if (isset($this->ref_attr[$link_id])) + $result .= $this->ref_attr[$link_id]; + + $link_text = $this->runSpanGamut($link_text); + $result .= ">$link_text"; + $result = $this->hashPart($result); + } + else { + $result = $whole_match; + } + return $result; + } + + /** + * Callback for inline anchors + * @param array $matches + * @return string + */ + protected function _doAnchors_inline_callback($matches) { + $whole_match = $matches[1]; + $link_text = $this->runSpanGamut($matches[2]); + $url = $matches[3] == '' ? $matches[4] : $matches[3]; + $title =& $matches[7]; + $attr = $this->doExtraAttributes("a", $dummy =& $matches[8]); + + // if the URL was of the form it got caught by the HTML + // tag parser and hashed. Need to reverse the process before using the URL. + $unhashed = $this->unhash($url); + if ($unhashed != $url) + $url = preg_replace('/^<(.*)>$/', '\1', $unhashed); + + $url = $this->encodeURLAttribute($url); + + $result = "encodeAttribute($title); + $result .= " title=\"$title\""; + } + $result .= $attr; + + $link_text = $this->runSpanGamut($link_text); + $result .= ">$link_text"; + + return $this->hashPart($result); + } + + /** + * Turn Markdown image shortcuts into tags. + * @param string $text + * @return string + */ + protected function doImages($text) { + // First, handle reference-style labeled images: ![alt text][id] + $text = preg_replace_callback('{ + ( # wrap whole match in $1 + !\[ + (' . $this->nested_brackets_re . ') # alt text = $2 + \] + + [ ]? # one optional space + (?:\n[ ]*)? # one optional newline followed by spaces + + \[ + (.*?) # id = $3 + \] + + ) + }xs', + array($this, '_doImages_reference_callback'), $text); + + // Next, handle inline images: ![alt text](url "optional title") + // Don't forget: encode * and _ + $text = preg_replace_callback('{ + ( # wrap whole match in $1 + !\[ + (' . $this->nested_brackets_re . ') # alt text = $2 + \] + \s? # One optional whitespace character + \( # literal paren + [ \n]* + (?: + <(\S*)> # src url = $3 + | + (' . $this->nested_url_parenthesis_re . ') # src url = $4 + ) + [ \n]* + ( # $5 + ([\'"]) # quote char = $6 + (.*?) # title = $7 + \6 # matching quote + [ \n]* + )? # title is optional + \) + (?:[ ]? ' . $this->id_class_attr_catch_re . ' )? # $8 = id/class attributes + ) + }xs', + array($this, '_doImages_inline_callback'), $text); + + return $text; + } + + /** + * Callback for referenced images + * @param array $matches + * @return string + */ + protected function _doImages_reference_callback($matches) { + $whole_match = $matches[1]; + $alt_text = $matches[2]; + $link_id = strtolower($matches[3]); + + if ($link_id == "") { + $link_id = strtolower($alt_text); // for shortcut links like ![this][]. + } + + $alt_text = $this->encodeAttribute($alt_text); + if (isset($this->urls[$link_id])) { + $url = $this->encodeURLAttribute($this->urls[$link_id]); + $result = "\"$alt_text\"";titles[$link_id])) { + $title = $this->titles[$link_id]; + $title = $this->encodeAttribute($title); + $result .= " title=\"$title\""; + } + if (isset($this->ref_attr[$link_id])) + $result .= $this->ref_attr[$link_id]; + $result .= $this->empty_element_suffix; + $result = $this->hashPart($result); + } + else { + // If there's no such link ID, leave intact: + $result = $whole_match; + } + + return $result; + } + + /** + * Callback for inline images + * @param array $matches + * @return string + */ + protected function _doImages_inline_callback($matches) { + $whole_match = $matches[1]; + $alt_text = $matches[2]; + $url = $matches[3] == '' ? $matches[4] : $matches[3]; + $title =& $matches[7]; + $attr = $this->doExtraAttributes("img", $dummy =& $matches[8]); + + $alt_text = $this->encodeAttribute($alt_text); + $url = $this->encodeURLAttribute($url); + $result = "\"$alt_text\"";encodeAttribute($title); + $result .= " title=\"$title\""; // $title already quoted + } + $result .= $attr; + $result .= $this->empty_element_suffix; + + return $this->hashPart($result); + } + + /** + * Process markdown headers. Redefined to add ID and class attribute support. + * @param string $text + * @return string + */ + protected function doHeaders($text) { + // Setext-style headers: + // Header 1 {#header1} + // ======== + // + // Header 2 {#header2 .class1 .class2} + // -------- + // + $text = preg_replace_callback( + '{ + (^.+?) # $1: Header text + (?:[ ]+ ' . $this->id_class_attr_catch_re . ' )? # $3 = id/class attributes + [ ]*\n(=+|-+)[ ]*\n+ # $3: Header footer + }mx', + array($this, '_doHeaders_callback_setext'), $text); + + // atx-style headers: + // # Header 1 {#header1} + // ## Header 2 {#header2} + // ## Header 2 with closing hashes ## {#header3.class1.class2} + // ... + // ###### Header 6 {.class2} + // + $text = preg_replace_callback('{ + ^(\#{1,6}) # $1 = string of #\'s + [ ]* + (.+?) # $2 = Header text + [ ]* + \#* # optional closing #\'s (not counted) + (?:[ ]+ ' . $this->id_class_attr_catch_re . ' )? # $3 = id/class attributes + [ ]* + \n+ + }xm', + array($this, '_doHeaders_callback_atx'), $text); + + return $text; + } + + /** + * Callback for setext headers + * @param array $matches + * @return string + */ + protected function _doHeaders_callback_setext($matches) { + if ($matches[3] == '-' && preg_match('{^- }', $matches[1])) { + return $matches[0]; + } + + $level = $matches[3]{0} == '=' ? 1 : 2; + + $defaultId = is_callable($this->header_id_func) ? call_user_func($this->header_id_func, $matches[1]) : null; + + $attr = $this->doExtraAttributes("h$level", $dummy =& $matches[2], $defaultId); + $block = "" . $this->runSpanGamut($matches[1]) . ""; + return "\n" . $this->hashBlock($block) . "\n\n"; + } + + /** + * Callback for atx headers + * @param array $matches + * @return string + */ + protected function _doHeaders_callback_atx($matches) { + $level = strlen($matches[1]); + + $defaultId = is_callable($this->header_id_func) ? call_user_func($this->header_id_func, $matches[2]) : null; + $attr = $this->doExtraAttributes("h$level", $dummy =& $matches[3], $defaultId); + $block = "" . $this->runSpanGamut($matches[2]) . ""; + return "\n" . $this->hashBlock($block) . "\n\n"; + } + + /** + * Form HTML tables. + * @param string $text + * @return string + */ + protected function doTables($text) { + $less_than_tab = $this->tab_width - 1; + // Find tables with leading pipe. + // + // | Header 1 | Header 2 + // | -------- | -------- + // | Cell 1 | Cell 2 + // | Cell 3 | Cell 4 + $text = preg_replace_callback(' + { + ^ # Start of a line + [ ]{0,' . $less_than_tab . '} # Allowed whitespace. + [|] # Optional leading pipe (present) + (.+) \n # $1: Header row (at least one pipe) + + [ ]{0,' . $less_than_tab . '} # Allowed whitespace. + [|] ([ ]*[-:]+[-| :]*) \n # $2: Header underline + + ( # $3: Cells + (?> + [ ]* # Allowed whitespace. + [|] .* \n # Row content. + )* + ) + (?=\n|\Z) # Stop at final double newline. + }xm', + array($this, '_doTable_leadingPipe_callback'), $text); + + // Find tables without leading pipe. + // + // Header 1 | Header 2 + // -------- | -------- + // Cell 1 | Cell 2 + // Cell 3 | Cell 4 + $text = preg_replace_callback(' + { + ^ # Start of a line + [ ]{0,' . $less_than_tab . '} # Allowed whitespace. + (\S.*[|].*) \n # $1: Header row (at least one pipe) + + [ ]{0,' . $less_than_tab . '} # Allowed whitespace. + ([-:]+[ ]*[|][-| :]*) \n # $2: Header underline + + ( # $3: Cells + (?> + .* [|] .* \n # Row content + )* + ) + (?=\n|\Z) # Stop at final double newline. + }xm', + array($this, '_DoTable_callback'), $text); + + return $text; + } + + /** + * Callback for removing the leading pipe for each row + * @param array $matches + * @return string + */ + protected function _doTable_leadingPipe_callback($matches) { + $head = $matches[1]; + $underline = $matches[2]; + $content = $matches[3]; + + $content = preg_replace('/^ *[|]/m', '', $content); + + return $this->_doTable_callback(array($matches[0], $head, $underline, $content)); + } + + /** + * Make the align attribute in a table + * @param string $alignname + * @return string + */ + protected function _doTable_makeAlignAttr($alignname) + { + if (empty($this->table_align_class_tmpl)) { + return " align=\"$alignname\""; + } + + $classname = str_replace('%%', $alignname, $this->table_align_class_tmpl); + return " class=\"$classname\""; + } + + /** + * Calback for processing tables + * @param array $matches + * @return string + */ + protected function _doTable_callback($matches) { + $head = $matches[1]; + $underline = $matches[2]; + $content = $matches[3]; + + // Remove any tailing pipes for each line. + $head = preg_replace('/[|] *$/m', '', $head); + $underline = preg_replace('/[|] *$/m', '', $underline); + $content = preg_replace('/[|] *$/m', '', $content); + + // Reading alignement from header underline. + $separators = preg_split('/ *[|] */', $underline); + foreach ($separators as $n => $s) { + if (preg_match('/^ *-+: *$/', $s)) + $attr[$n] = $this->_doTable_makeAlignAttr('right'); + else if (preg_match('/^ *:-+: *$/', $s)) + $attr[$n] = $this->_doTable_makeAlignAttr('center'); + else if (preg_match('/^ *:-+ *$/', $s)) + $attr[$n] = $this->_doTable_makeAlignAttr('left'); + else + $attr[$n] = ''; + } + + // Parsing span elements, including code spans, character escapes, + // and inline HTML tags, so that pipes inside those gets ignored. + $head = $this->parseSpan($head); + $headers = preg_split('/ *[|] */', $head); + $col_count = count($headers); + $attr = array_pad($attr, $col_count, ''); + + // Write column headers. + $text = "\n"; + $text .= "\n"; + $text .= "\n"; + foreach ($headers as $n => $header) + $text .= " " . $this->runSpanGamut(trim($header)) . "\n"; + $text .= "\n"; + $text .= "\n"; + + // Split content by row. + $rows = explode("\n", trim($content, "\n")); + + $text .= "\n"; + foreach ($rows as $row) { + // Parsing span elements, including code spans, character escapes, + // and inline HTML tags, so that pipes inside those gets ignored. + $row = $this->parseSpan($row); + + // Split row by cell. + $row_cells = preg_split('/ *[|] */', $row, $col_count); + $row_cells = array_pad($row_cells, $col_count, ''); + + $text .= "\n"; + foreach ($row_cells as $n => $cell) + $text .= " " . $this->runSpanGamut(trim($cell)) . "\n"; + $text .= "\n"; + } + $text .= "\n"; + $text .= "
    "; + + return $this->hashBlock($text) . "\n"; + } + + /** + * Form HTML definition lists. + * @param string $text + * @return string + */ + protected function doDefLists($text) { + $less_than_tab = $this->tab_width - 1; + + // Re-usable pattern to match any entire dl list: + $whole_list_re = '(?> + ( # $1 = whole list + ( # $2 + [ ]{0,' . $less_than_tab . '} + ((?>.*\S.*\n)+) # $3 = defined term + \n? + [ ]{0,' . $less_than_tab . '}:[ ]+ # colon starting definition + ) + (?s:.+?) + ( # $4 + \z + | + \n{2,} + (?=\S) + (?! # Negative lookahead for another term + [ ]{0,' . $less_than_tab . '} + (?: \S.*\n )+? # defined term + \n? + [ ]{0,' . $less_than_tab . '}:[ ]+ # colon starting definition + ) + (?! # Negative lookahead for another definition + [ ]{0,' . $less_than_tab . '}:[ ]+ # colon starting definition + ) + ) + ) + )'; // mx + + $text = preg_replace_callback('{ + (?>\A\n?|(?<=\n\n)) + ' . $whole_list_re . ' + }mx', + array($this, '_doDefLists_callback'), $text); + + return $text; + } + + /** + * Callback for processing definition lists + * @param array $matches + * @return string + */ + protected function _doDefLists_callback($matches) { + // Re-usable patterns to match list item bullets and number markers: + $list = $matches[1]; + + // Turn double returns into triple returns, so that we can make a + // paragraph for the last item in a list, if necessary: + $result = trim($this->processDefListItems($list)); + $result = "
    \n" . $result . "\n
    "; + return $this->hashBlock($result) . "\n\n"; + } + + /** + * Process the contents of a single definition list, splitting it + * into individual term and definition list items. + * @param string $list_str + * @return string + */ + protected function processDefListItems($list_str) { + + $less_than_tab = $this->tab_width - 1; + + // Trim trailing blank lines: + $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str); + + // Process definition terms. + $list_str = preg_replace_callback('{ + (?>\A\n?|\n\n+) # leading line + ( # definition terms = $1 + [ ]{0,' . $less_than_tab . '} # leading whitespace + (?!\:[ ]|[ ]) # negative lookahead for a definition + # mark (colon) or more whitespace. + (?> \S.* \n)+? # actual term (not whitespace). + ) + (?=\n?[ ]{0,3}:[ ]) # lookahead for following line feed + # with a definition mark. + }xm', + array($this, '_processDefListItems_callback_dt'), $list_str); + + // Process actual definitions. + $list_str = preg_replace_callback('{ + \n(\n+)? # leading line = $1 + ( # marker space = $2 + [ ]{0,' . $less_than_tab . '} # whitespace before colon + \:[ ]+ # definition mark (colon) + ) + ((?s:.+?)) # definition text = $3 + (?= \n+ # stop at next definition mark, + (?: # next term or end of text + [ ]{0,' . $less_than_tab . '} \:[ ] | +
    | \z + ) + ) + }xm', + array($this, '_processDefListItems_callback_dd'), $list_str); + + return $list_str; + } + + /** + * Callback for
    elements in definition lists + * @param array $matches + * @return string + */ + protected function _processDefListItems_callback_dt($matches) { + $terms = explode("\n", trim($matches[1])); + $text = ''; + foreach ($terms as $term) { + $term = $this->runSpanGamut(trim($term)); + $text .= "\n
    " . $term . "
    "; + } + return $text . "\n"; + } + + /** + * Callback for
    elements in definition lists + * @param array $matches + * @return string + */ + protected function _processDefListItems_callback_dd($matches) { + $leading_line = $matches[1]; + $marker_space = $matches[2]; + $def = $matches[3]; + + if ($leading_line || preg_match('/\n{2,}/', $def)) { + // Replace marker with the appropriate whitespace indentation + $def = str_repeat(' ', strlen($marker_space)) . $def; + $def = $this->runBlockGamut($this->outdent($def . "\n\n")); + $def = "\n". $def ."\n"; + } + else { + $def = rtrim($def); + $def = $this->runSpanGamut($this->outdent($def)); + } + + return "\n
    " . $def . "
    \n"; + } + + /** + * Adding the fenced code block syntax to regular Markdown: + * + * ~~~ + * Code block + * ~~~ + * + * @param string $text + * @return string + */ + protected function doFencedCodeBlocks($text) { + + $less_than_tab = $this->tab_width; + + $text = preg_replace_callback('{ + (?:\n|\A) + # 1: Opening marker + ( + (?:~{3,}|`{3,}) # 3 or more tildes/backticks. + ) + [ ]* + (?: + \.?([-_:a-zA-Z0-9]+) # 2: standalone class name + )? + [ ]* + (?: + ' . $this->id_class_attr_catch_re . ' # 3: Extra attributes + )? + [ ]* \n # Whitespace and newline following marker. + + # 4: Content + ( + (?> + (?!\1 [ ]* \n) # Not a closing marker. + .*\n+ + )+ + ) + + # Closing marker. + \1 [ ]* (?= \n ) + }xm', + array($this, '_doFencedCodeBlocks_callback'), $text); + + return $text; + } + + /** + * Callback to process fenced code blocks + * @param array $matches + * @return string + */ + protected function _doFencedCodeBlocks_callback($matches) { + $classname =& $matches[2]; + $attrs =& $matches[3]; + $codeblock = $matches[4]; + + if ($this->code_block_content_func) { + $codeblock = call_user_func($this->code_block_content_func, $codeblock, $classname); + } else { + $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES); + } + + $codeblock = preg_replace_callback('/^\n+/', + array($this, '_doFencedCodeBlocks_newlines'), $codeblock); + + $classes = array(); + if ($classname != "") { + if ($classname{0} == '.') + $classname = substr($classname, 1); + $classes[] = $this->code_class_prefix . $classname; + } + $attr_str = $this->doExtraAttributes($this->code_attr_on_pre ? "pre" : "code", $attrs, null, $classes); + $pre_attr_str = $this->code_attr_on_pre ? $attr_str : ''; + $code_attr_str = $this->code_attr_on_pre ? '' : $attr_str; + $codeblock = "$codeblock
    "; + + return "\n\n".$this->hashBlock($codeblock)."\n\n"; + } + + /** + * Replace new lines in fenced code blocks + * @param array $matches + * @return string + */ + protected function _doFencedCodeBlocks_newlines($matches) { + return str_repeat("empty_element_suffix", + strlen($matches[0])); + } + + /** + * Redefining emphasis markers so that emphasis by underscore does not + * work in the middle of a word. + * @var array + */ + protected $em_relist = array( + '' => '(?:(? '(? '(? '(?:(? '(? '(? '(?:(? '(? '(? tags + * @return string HTML output + */ + protected function formParagraphs($text, $wrap_in_p = true) { + // Strip leading and trailing lines: + $text = preg_replace('/\A\n+|\n+\z/', '', $text); + + $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY); + + // Wrap

    tags and unhashify HTML blocks + foreach ($grafs as $key => $value) { + $value = trim($this->runSpanGamut($value)); + + // Check if this should be enclosed in a paragraph. + // Clean tag hashes & block tag hashes are left alone. + $is_p = $wrap_in_p && !preg_match('/^B\x1A[0-9]+B|^C\x1A[0-9]+C$/', $value); + + if ($is_p) { + $value = "

    $value

    "; + } + $grafs[$key] = $value; + } + + // Join grafs in one text, then unhash HTML tags. + $text = implode("\n\n", $grafs); + + // Finish by removing any tag hashes still present in $text. + $text = $this->unhash($text); + + return $text; + } + + + /** + * Footnotes - Strips link definitions from text, stores the URLs and + * titles in hash references. + * @param string $text + * @return string + */ + protected function stripFootnotes($text) { + $less_than_tab = $this->tab_width - 1; + + // Link defs are in the form: [^id]: url "optional title" + $text = preg_replace_callback('{ + ^[ ]{0,' . $less_than_tab . '}\[\^(.+?)\][ ]?: # note_id = $1 + [ ]* + \n? # maybe *one* newline + ( # text = $2 (no blank lines allowed) + (?: + .+ # actual text + | + \n # newlines but + (?!\[.+?\][ ]?:\s)# negative lookahead for footnote or link definition marker. + (?!\n+[ ]{0,3}\S)# ensure line is not blank and followed + # by non-indented content + )* + ) + }xm', + array($this, '_stripFootnotes_callback'), + $text); + return $text; + } + + /** + * Callback for stripping footnotes + * @param array $matches + * @return string + */ + protected function _stripFootnotes_callback($matches) { + $note_id = $this->fn_id_prefix . $matches[1]; + $this->footnotes[$note_id] = $this->outdent($matches[2]); + return ''; // String that will replace the block + } + + /** + * Replace footnote references in $text [^id] with a special text-token + * which will be replaced by the actual footnote marker in appendFootnotes. + * @param string $text + * @return string + */ + protected function doFootnotes($text) { + if (!$this->in_anchor) { + $text = preg_replace('{\[\^(.+?)\]}', "F\x1Afn:\\1\x1A:", $text); + } + return $text; + } + + /** + * Append footnote list to text + * @param string $text + * @return string + */ + protected function appendFootnotes($text) { + $text = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}', + array($this, '_appendFootnotes_callback'), $text); + + if (!empty($this->footnotes_ordered)) { + $text .= "\n\n"; + $text .= "
    \n"; + $text .= "empty_element_suffix . "\n"; + $text .= "
      \n\n"; + + $attr = ""; + if ($this->fn_backlink_class != "") { + $class = $this->fn_backlink_class; + $class = $this->encodeAttribute($class); + $attr .= " class=\"$class\""; + } + if ($this->fn_backlink_title != "") { + $title = $this->fn_backlink_title; + $title = $this->encodeAttribute($title); + $attr .= " title=\"$title\""; + } + $backlink_text = $this->fn_backlink_html; + $num = 0; + + while (!empty($this->footnotes_ordered)) { + $footnote = reset($this->footnotes_ordered); + $note_id = key($this->footnotes_ordered); + unset($this->footnotes_ordered[$note_id]); + $ref_count = $this->footnotes_ref_count[$note_id]; + unset($this->footnotes_ref_count[$note_id]); + unset($this->footnotes[$note_id]); + + $footnote .= "\n"; // Need to append newline before parsing. + $footnote = $this->runBlockGamut("$footnote\n"); + $footnote = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}', + array($this, '_appendFootnotes_callback'), $footnote); + + $attr = str_replace("%%", ++$num, $attr); + $note_id = $this->encodeAttribute($note_id); + + // Prepare backlink, multiple backlinks if multiple references + $backlink = "$backlink_text"; + for ($ref_num = 2; $ref_num <= $ref_count; ++$ref_num) { + $backlink .= " $backlink_text"; + } + // Add backlink to last paragraph; create new paragraph if needed. + if (preg_match('{

      $}', $footnote)) { + $footnote = substr($footnote, 0, -4) . " $backlink

      "; + } else { + $footnote .= "\n\n

      $backlink

      "; + } + + $text .= "
    1. \n"; + $text .= $footnote . "\n"; + $text .= "
    2. \n\n"; + } + + $text .= "
    \n"; + $text .= "
    "; + } + return $text; + } + + /** + * Callback for appending footnotes + * @param array $matches + * @return string + */ + protected function _appendFootnotes_callback($matches) { + $node_id = $this->fn_id_prefix . $matches[1]; + + // Create footnote marker only if it has a corresponding footnote *and* + // the footnote hasn't been used by another marker. + if (isset($this->footnotes[$node_id])) { + $num =& $this->footnotes_numbers[$node_id]; + if (!isset($num)) { + // Transfer footnote content to the ordered list and give it its + // number + $this->footnotes_ordered[$node_id] = $this->footnotes[$node_id]; + $this->footnotes_ref_count[$node_id] = 1; + $num = $this->footnote_counter++; + $ref_count_mark = ''; + } else { + $ref_count_mark = $this->footnotes_ref_count[$node_id] += 1; + } + + $attr = ""; + if ($this->fn_link_class != "") { + $class = $this->fn_link_class; + $class = $this->encodeAttribute($class); + $attr .= " class=\"$class\""; + } + if ($this->fn_link_title != "") { + $title = $this->fn_link_title; + $title = $this->encodeAttribute($title); + $attr .= " title=\"$title\""; + } + + $attr = str_replace("%%", $num, $attr); + $node_id = $this->encodeAttribute($node_id); + + return + "". + "$num". + ""; + } + + return "[^" . $matches[1] . "]"; + } + + + /** + * Abbreviations - strips abbreviations from text, stores titles in hash + * references. + * @param string $text + * @return string + */ + protected function stripAbbreviations($text) { + $less_than_tab = $this->tab_width - 1; + + // Link defs are in the form: [id]*: url "optional title" + $text = preg_replace_callback('{ + ^[ ]{0,' . $less_than_tab . '}\*\[(.+?)\][ ]?: # abbr_id = $1 + (.*) # text = $2 (no blank lines allowed) + }xm', + array($this, '_stripAbbreviations_callback'), + $text); + return $text; + } + + /** + * Callback for stripping abbreviations + * @param array $matches + * @return string + */ + protected function _stripAbbreviations_callback($matches) { + $abbr_word = $matches[1]; + $abbr_desc = $matches[2]; + if ($this->abbr_word_re) { + $this->abbr_word_re .= '|'; + } + $this->abbr_word_re .= preg_quote($abbr_word); + $this->abbr_desciptions[$abbr_word] = trim($abbr_desc); + return ''; // String that will replace the block + } + + /** + * Find defined abbreviations in text and wrap them in elements. + * @param string $text + * @return string + */ + protected function doAbbreviations($text) { + if ($this->abbr_word_re) { + // cannot use the /x modifier because abbr_word_re may + // contain significant spaces: + $text = preg_replace_callback('{' . + '(?abbr_word_re . ')' . + '(?![\w\x1A])' . + '}', + array($this, '_doAbbreviations_callback'), $text); + } + return $text; + } + + /** + * Callback for processing abbreviations + * @param array $matches + * @return string + */ + protected function _doAbbreviations_callback($matches) { + $abbr = $matches[0]; + if (isset($this->abbr_desciptions[$abbr])) { + $desc = $this->abbr_desciptions[$abbr]; + if (empty($desc)) { + return $this->hashPart("$abbr"); + } else { + $desc = $this->encodeAttribute($desc); + return $this->hashPart("$abbr"); + } + } else { + return $matches[0]; + } + } } - diff --git a/library/php-markdown/Michelf/MarkdownInterface.inc.php b/library/php-markdown/Michelf/MarkdownInterface.inc.php index a023ed4e3..c4e9ac7f6 100644 --- a/library/php-markdown/Michelf/MarkdownInterface.inc.php +++ b/library/php-markdown/Michelf/MarkdownInterface.inc.php @@ -1,9 +1,9 @@ -# -# Original Markdown -# Copyright (c) 2004-2006 John Gruber -# -# +/** + * Markdown - A text-to-HTML conversion tool for web writers + * + * @package php-markdown + * @author Michel Fortin + * @copyright 2004-2016 Michel Fortin + * @copyright (Original Markdown) 2004-2006 John Gruber + */ + namespace Michelf; - -# -# Markdown Parser Interface -# - +/** + * Markdown Parser Interface + */ interface MarkdownInterface { + /** + * Initialize the parser and return the result of its transform method. + * This will work fine for derived classes too. + * + * @api + * + * @param string $text + * @return string + */ + public static function defaultTransform($text); - # - # Initialize the parser and return the result of its transform method. - # This will work fine for derived classes too. - # - public static function defaultTransform($text); - - # - # Main function. Performs some preprocessing on the input text - # and pass it through the document gamut. - # - public function transform($text); - + /** + * Main function. Performs some preprocessing on the input text + * and pass it through the document gamut. + * + * @api + * + * @param string $text + * @return string + */ + public function transform($text); } diff --git a/library/php-markdown/Readme.md b/library/php-markdown/Readme.md index 80504c27c..63e8c1ece 100644 --- a/library/php-markdown/Readme.md +++ b/library/php-markdown/Readme.md @@ -1,13 +1,13 @@ PHP Markdown ============ -PHP Markdown Lib 1.4.1 - 4 May 2013 +PHP Markdown Lib 1.7.0 - 29 Oct 2016 by Michel Fortin - + based on Markdown by John Gruber - + Introduction @@ -25,10 +25,10 @@ software tool, originally written in Perl, that converts the plain text markup to HTML. PHP Markdown is a port to PHP of the original Markdown program by John Gruber. -* [Full documentation of the Markdown syntax]() - - Daring Fireball (John Gruber) -* [Markdown Extra syntax additions]() - - Michel Fortin +* [Full documentation of the Markdown syntax]() + — Daring Fireball (John Gruber) +* [Markdown Extra syntax additions]() + — Michel Fortin Requirement @@ -83,7 +83,7 @@ configuration variables: To learn more, see the full list of [configuration variables]. - [configuration variables]: http://michelf.ca/projects/php-markdown/configuration/ + [configuration variables]: https://michelf.ca/projects/php-markdown/configuration/ ### Usage without an autoloader @@ -149,7 +149,7 @@ Development and Testing ----------------------- Pull requests for fixing bugs are welcome. Proposed new features are -going meticulously reviewed -- taking into account backward compatibility, +going to be meticulously reviewed -- taking into account backward compatibility, potential side effects, and future extensibility -- before deciding on acceptance or rejection. @@ -174,11 +174,80 @@ PHP Markdown, please visit [michelf.ca/donate] or send Bitcoin to Version History --------------- -Unreleased +PHP Markdown Lib 1.7.0 (29 Oct 2016) -* Added the ability to insert custom HTML attributes everywhere an extra - attribute block is allowed (links, images, headers). Credits to - Peter Droogmans for providing the implementation. +* Added a `hard_wrap` configuration variable to make all newline characters + in the text become `
    ` tags in the HTML output. By default, according + to the standard Markdown syntax these newlines are ignored unless they a + preceded by two spaces. Thanks to Jonathan Cohlmeyer for the implementation. + +* Improved the parsing of list items to fix problematic cases that came to + light with the addition of `hard_wrap`. This should have no effect on the + output except span-level list items that ended with two spaces (and thus + ended with a line break). + +* Added a `code_span_content_func` configuration variable which takes a + function that will convert the content of the code span to HTML. This can + be useful to implement syntax highlighting. Although contrary to its + code block equivalent, there is no syntax for specifying a language. + Credits to styxit for the implementation. + +* Fixed a Markdown Extra issue where two-space-at-end-of-line hard breaks + wouldn't work inside of HTML block elements such as `

    ` + where the element expects only span-level content. + +* In the parser code, switched to PHPDoc comment format. Thanks to + Robbie Averill for the help. + + +PHP Markdown Lib 1.6.0 (23 Dec 2015) + +Note: this version was incorrectly released as 1.5.1 on Dec 22, a number +that contradicted the versioning policy. + +* For fenced code blocks in Markdown Extra, can now set a class name for the + code block's language before the special attribute block. Previously, this + class name was only allowed in the absence of the special attribute block. + +* Added a `code_block_content_func` configuration variable which takes a + function that will convert the content of the code block to HTML. This is + most useful for syntax highlighting. For fenced code blocks in Markdown + Extra, the function has access to the language class name (the one outside + of the special attribute block). Credits to Mario Konrad for providing the + implementation. + +* The curled arrow character for the backlink in footnotes is now followed + by a Unicode variant selector to prevent it from being displayed in emoji + form on iOS. + + Note that in older browsers the variant selector is often interpreted as a + separate character, making it visible after the arrow. So there is now a + also a `fn_backlink_html` configuration variable that can be used to set + the link text to something else. Credits to Dana for providing the + implementation. + +* Fixed an issue in MarkdownExtra where long header lines followed by a + special attribute block would hit the backtrack limit an cause an empty + string to be returned. + + +PHP Markdown Lib 1.5.0 (1 Mar 2015) + +* Added the ability start ordered lists with a number different from 1 and + and have that reflected in the HTML output. This can be enabled with + the `enhanced_ordered_lists` configuration variable for the Markdown + parser; it is enabled by default for Markdown Extra. + Credits to Matt Gorle for providing the implementation. + +* Added the ability to insert custom HTML attributes with simple values + everywhere an extra attribute block is allowed (links, images, headers). + The value must be unquoted, cannot contains spaces and is limited to + alphanumeric ASCII characters. + Credits to Peter Droogmans for providing the implementation. + +* Added a `header_id_func` configuration variable which takes a function + that can generate an `id` attribute value from the header text. + Credits to Evert Pot for providing the implementation. * Added a `url_filter_func` configuration variable which takes a function that can rewrite any link or image URL to something different. @@ -239,7 +308,7 @@ PHP Markdown Extra 1.2.6: * Plugin interface for WordPress and other systems is no longer present in the Lib package. The classic package is still available if you need it: - + * Added `public` and `protected` protection attributes, plus a section about what is "public API" and what isn't in the Readme file. @@ -277,13 +346,13 @@ Copyright and License --------------------- PHP Markdown Lib -Copyright (c) 2004-2014 Michel Fortin - +Copyright (c) 2004-2016 Michel Fortin + All rights reserved. Based on Markdown Copyright (c) 2003-2005 John Gruber - + All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/library/php-markdown/Readme.php b/library/php-markdown/Readme.php index d007b119f..89449dea4 100644 --- a/library/php-markdown/Readme.php +++ b/library/php-markdown/Readme.php @@ -1,18 +1,18 @@ diff --git a/library/php-markdown/composer.json b/library/php-markdown/composer.json index 45abc6774..c219e543c 100644 --- a/library/php-markdown/composer.json +++ b/library/php-markdown/composer.json @@ -2,19 +2,19 @@ "name": "michelf/php-markdown", "type": "library", "description": "PHP Markdown", - "homepage": "http://michelf.ca/projects/php-markdown/", + "homepage": "https://michelf.ca/projects/php-markdown/", "keywords": ["markdown"], "license": "BSD-3-Clause", "authors": [ { "name": "Michel Fortin", "email": "michel.fortin@michelf.ca", - "homepage": "http://michelf.ca/", + "homepage": "https://michelf.ca/", "role": "Developer" }, { "name": "John Gruber", - "homepage": "http://daringfireball.net/" + "homepage": "https://daringfireball.net/" } ], "require": { @@ -22,10 +22,5 @@ }, "autoload": { "psr-0": { "Michelf": "" } - }, - "extra": { - "branch-alias": { - "dev-lib": "1.4.x-dev" - } } } From 839a457ab2390284da167d4289d44a24ff7f7cd8 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 23 Mar 2017 21:23:00 -0400 Subject: [PATCH 12/23] Enable hard_wrap for Markdown parser to match Diaspora parsing --- library/markdown.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/markdown.php b/library/markdown.php index a8152c2ab..e0b65ba0a 100644 --- a/library/markdown.php +++ b/library/markdown.php @@ -15,7 +15,9 @@ function Markdown($text) { $stamp1 = microtime(true); # Read file and pass content through the Markdown parser - $html = MarkdownExtra::defaultTransform($text); + $MarkdownParser = new MarkdownExtra(); + $MarkdownParser->hard_wrap = true; + $html = $MarkdownParser->transform($text); $a->save_timestamp($stamp1, "parser"); From b9f4a6e4f09aabdc99751602e37f7e9e1b57f8d6 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 25 Mar 2017 01:54:16 -0400 Subject: [PATCH 13/23] Code cleanup + standards --- include/oembed.php | 14 +++++++------- view/templates/admin_site.tpl | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/oembed.php b/include/oembed.php index a1945894f..d0a2bc864 100755 --- a/include/oembed.php +++ b/include/oembed.php @@ -17,10 +17,10 @@ function oembed_replacecb($matches){ /** * @brief Get data from an URL to embed its content. - * + * * @param string $embedurl The URL from which the data should be fetched. * @param bool $no_rich_type If set to true rich type content won't be fetched. - * + * * @return bool|object Returns object with embed content or false if no embedable * content exists */ @@ -41,8 +41,8 @@ function oembed_fetch_url($embedurl, $no_rich_type = false){ // These media files should now be caught in bbcode.php // left here as a fallback in case this is called from another source - $noexts = array("mp3","mp4","ogg","ogv","oga","ogm","webm"); - $ext = pathinfo(strtolower($embedurl),PATHINFO_EXTENSION); + $noexts = array("mp3", "mp4", "ogg", "ogv", "oga", "ogm", "webm"); + $ext = pathinfo(strtolower($embedurl), PATHINFO_EXTENSION); if (is_null($txt)) { @@ -85,10 +85,10 @@ function oembed_fetch_url($embedurl, $no_rich_type = false){ } } - $txt=trim($txt); + $txt = trim($txt); - if ($txt[0]!="{") { - $txt='{"type":"error"}'; + if ($txt[0] != "{") { + $txt = '{"type":"error"}'; } else { //save in cache $j = json_decode($txt); if ($j->type != "error") { diff --git a/view/templates/admin_site.tpl b/view/templates/admin_site.tpl index fdba195f1..5dfef2b97 100644 --- a/view/templates/admin_site.tpl +++ b/view/templates/admin_site.tpl @@ -1,6 +1,6 @@

    {{$title}} - {{$page}}

    - +
    @@ -59,7 +59,7 @@ {{include file="field_checkbox.tpl" field=$hide_help}} {{include file="field_select.tpl" field=$singleuser}}
    - +

    {{$registration}}

    {{include file="field_input.tpl" field=$register_text}} {{include file="field_select.tpl" field=$register_policy}} @@ -74,7 +74,7 @@ {{include file="field_input.tpl" field=$maximagelength}} {{include file="field_input.tpl" field=$jpegimagequality}}
    - +

    {{$corporate}}

    {{include file="field_input.tpl" field=$allowed_sites}} {{include file="field_input.tpl" field=$allowed_email}} @@ -107,11 +107,11 @@ {{include file="field_checkbox.tpl" field=$thread_allow}} {{include file="field_checkbox.tpl" field=$newuser_private}} {{include file="field_checkbox.tpl" field=$enotify_no_content}} - {{include file="field_checkbox.tpl" field=$private_addons}} + {{include file="field_checkbox.tpl" field=$private_addons}} {{include file="field_checkbox.tpl" field=$disable_embedded}} {{include file="field_checkbox.tpl" field=$allow_users_remote_self}}
    - +

    {{$advanced}}

    {{include file="field_select.tpl" field=$rino}} {{include file="field_checkbox.tpl" field=$no_utf}} @@ -156,7 +156,7 @@
    - + {{* separate form for relocate... *}}
    @@ -165,5 +165,5 @@
    - +
    From 29fb28de436d8cd8576e0eabf49e8a513f0333f4 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 25 Mar 2017 01:57:19 -0400 Subject: [PATCH 14/23] Remove Embedly integration - Remove admin setting input --- include/oembed.php | 11 ----------- mod/admin.php | 5 ----- view/templates/admin_site.tpl | 1 - 3 files changed, 17 deletions(-) diff --git a/include/oembed.php b/include/oembed.php index d0a2bc864..0b8b71366 100755 --- a/include/oembed.php +++ b/include/oembed.php @@ -74,17 +74,6 @@ function oembed_fetch_url($embedurl, $no_rich_type = false){ } } - if ($txt==false || $txt=="") { - $embedly = Config::get("system", "embedly"); - if ($embedly != "") { - // try embedly service - $ourl = "https://api.embed.ly/1/oembed?key=".$embedly."&url=".urlencode($embedurl); - $txt = fetch_url($ourl); - - logger("oembed_fetch_url: ".$txt, LOGGER_DEBUG); - } - } - $txt = trim($txt); if ($txt[0] != "{") { diff --git a/mod/admin.php b/mod/admin.php index b1bc8de5f..77093f085 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -666,7 +666,6 @@ function admin_page_site_post(App $a) { $proxy_disabled = ((x($_POST,'proxy_disabled')) ? True : False); $only_tag_search = ((x($_POST,'only_tag_search')) ? True : False); $rino = ((x($_POST,'rino')) ? intval($_POST['rino']) : 0); - $embedly = ((x($_POST,'embedly')) ? notags(trim($_POST['embedly'])) : ''); $worker_queues = ((x($_POST,'worker_queues')) ? intval($_POST['worker_queues']) : 4); $worker_dont_fork = ((x($_POST,'worker_dont_fork')) ? True : False); $worker_fastlane = ((x($_POST,'worker_fastlane')) ? True : False); @@ -821,9 +820,6 @@ function admin_page_site_post(App $a) { set_config('system','rino_encrypt', $rino); } - set_config('system','embedly', $embedly); - - info(t('Site settings updated.').EOL); goaway('admin/site'); return; // NOTREACHED @@ -1053,7 +1049,6 @@ function admin_page_site(App $a) { '$relocate_url' => array('relocate_url', t("New base url"), App::get_baseurl(), t("Change base url for this server. Sends relocate message to all DFRN contacts of all users.")), '$rino' => array('rino', t("RINO Encryption"), intval(get_config('system','rino_encrypt')), t("Encryption layer between nodes."), array("Disabled", "RINO1 (deprecated)", "RINO2")), - '$embedly' => array('embedly', t("Embedly API key"), get_config('system','embedly'), t("Embedly is used to fetch additional data for web pages. This is an optional parameter.")), '$worker_queues' => array('worker_queues', t("Maximum number of parallel workers"), get_config('system','worker_queues'), t("On shared hosters set this to 2. On larger systems, values of 10 are great. Default value is 4.")), '$worker_dont_fork' => array('worker_dont_fork', t("Don't use 'proc_open' with the worker"), get_config('system','worker_dont_fork'), t("Enable this if your system doesn't allow the use of 'proc_open'. This can happen on shared hosters. If this is enabled you should increase the frequency of poller calls in your crontab.")), diff --git a/view/templates/admin_site.tpl b/view/templates/admin_site.tpl index 5dfef2b97..138d04311 100644 --- a/view/templates/admin_site.tpl +++ b/view/templates/admin_site.tpl @@ -128,7 +128,6 @@ {{include file="field_input.tpl" field=$basepath}} {{include file="field_checkbox.tpl" field=$suppress_tags}} {{include file="field_checkbox.tpl" field=$nodeinfo}} - {{include file="field_input.tpl" field=$embedly}}

    {{$portable_contacts}}

    From 86ed5caab73923449b015df5005221d129ef276b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 25 Mar 2017 02:03:47 -0400 Subject: [PATCH 15/23] Cleanup + Standards for library/markdown.php --- library/markdown.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/library/markdown.php b/library/markdown.php index e0b65ba0a..769bdb121 100644 --- a/library/markdown.php +++ b/library/markdown.php @@ -1,20 +1,12 @@ hard_wrap = true; $html = $MarkdownParser->transform($text); @@ -23,4 +15,3 @@ function Markdown($text) { return $html; } -?> From 50603001381089d451272ae6a1ab82798f2e2c4c Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Mar 2017 10:07:45 +0000 Subject: [PATCH 16/23] Reset the static variable $baseurl --- include/Probe.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/Probe.php b/include/Probe.php index 929e89a68..d08460a56 100644 --- a/include/Probe.php +++ b/include/Probe.php @@ -56,6 +56,9 @@ class Probe { */ private function xrd($host) { + // Reset the static variable + self::$baseurl = ''; + $ssl_url = "https://".$host."/.well-known/host-meta"; $url = "http://".$host."/.well-known/host-meta"; From a419bbe7c7f756d7b676f2d6a40722f29d7deb8a Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Mar 2017 10:11:02 +0000 Subject: [PATCH 17/23] Hypolite is right (not always, but in this case) --- include/Probe.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/Probe.php b/include/Probe.php index d08460a56..c91b2cdf2 100644 --- a/include/Probe.php +++ b/include/Probe.php @@ -62,8 +62,6 @@ class Probe { $ssl_url = "https://".$host."/.well-known/host-meta"; $url = "http://".$host."/.well-known/host-meta"; - $baseurl = "http://".$host; - $xrd_timeout = Config::get('system','xrd_timeout', 20); $redirects = 0; @@ -110,7 +108,7 @@ class Probe { $xrd_data["lrdd"] = $attributes["template"]; } - self::$baseurl = $baseurl; + self::$baseurl = "http://".$host; return $xrd_data; } From 0d49143a63a2867e069d9ad84a7afac1a8326403 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Mar 2017 10:51:00 +0000 Subject: [PATCH 18/23] "no_utf" wasn't in use anymore - it is now removed completely --- doc/Settings.md | 7 ------- include/user.php | 7 ------- mod/admin.php | 3 --- view/templates/admin_site.tpl | 1 - view/theme/frost-mobile/templates/admin_site.tpl | 1 - view/theme/frost/templates/admin_site.tpl | 1 - 6 files changed, 20 deletions(-) diff --git a/doc/Settings.md b/doc/Settings.md index 9590ad42d..5055ed7d2 100644 --- a/doc/Settings.md +++ b/doc/Settings.md @@ -152,13 +152,6 @@ Value is in seconds. Default is 60 seconds. Set to 0 for unlimited (not recommended). -#### UTF-8 Regular Expressions - -During registrations, full names are checked using UTF-8 regular expressions. -This requires PHP to have been compiled with a special setting to allow UTF-8 expressions. -If you are completely unable to register accounts, set no_utf to true. -The default is set to false (meaning UTF8 regular expressions are supported and working). - #### Verify SSL Certitificates By default Friendica allows SSL communication between websites that have "self-signed" SSL certificates. diff --git a/include/user.php b/include/user.php index df871c546..cd77707dd 100644 --- a/include/user.php +++ b/include/user.php @@ -97,13 +97,6 @@ function create_user($arr) { if(mb_strlen($username) < 3) $result['message'] .= t('Name too short.') . EOL; - // I don't really like having this rule, but it cuts down - // on the number of auto-registrations by Russian spammers - - // Using preg_match was completely unreliable, due to mixed UTF-8 regex support - // $no_utf = get_config('system','no_utf'); - // $pat = (($no_utf) ? '/^[a-zA-Z]* [a-zA-Z]*$/' : '/^\p{L}* \p{L}*$/u' ); - // So now we are just looking for a space in the full name. $loose_reg = get_config('system','no_regfullname'); diff --git a/mod/admin.php b/mod/admin.php index 77093f085..aee1017b3 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -629,7 +629,6 @@ function admin_page_site_post(App $a) { $no_multi_reg = ((x($_POST,'no_multi_reg')) ? True : False); $no_openid = !((x($_POST,'no_openid')) ? True : False); $no_regfullname = !((x($_POST,'no_regfullname')) ? True : False); - $no_utf = !((x($_POST,'no_utf')) ? True : False); $community_page_style = ((x($_POST,'community_page_style')) ? intval(trim($_POST['community_page_style'])) : 0); $max_author_posts_community_page = ((x($_POST,'max_author_posts_community_page')) ? intval(trim($_POST['max_author_posts_community_page'])) : 0); @@ -786,7 +785,6 @@ function admin_page_site_post(App $a) { set_config('system','no_regfullname', $no_regfullname); set_config('system','community_page_style', $community_page_style); set_config('system','max_author_posts_community_page', $max_author_posts_community_page); - set_config('system','no_utf', $no_utf); set_config('system','verifyssl', $verifyssl); set_config('system','proxyuser', $proxyuser); set_config('system','proxy', $proxy); @@ -1008,7 +1006,6 @@ function admin_page_site(App $a) { '$no_multi_reg' => array('no_multi_reg', t("Block multiple registrations"), get_config('system','block_extended_register'), t("Disallow users to register additional accounts for use as pages.")), '$no_openid' => array('no_openid', t("OpenID support"), !get_config('system','no_openid'), t("OpenID support for registration and logins.")), '$no_regfullname' => array('no_regfullname', t("Fullname check"), !get_config('system','no_regfullname'), t("Force users to register with a space between firstname and lastname in Full name, as an antispam measure")), - '$no_utf' => array('no_utf', t("UTF-8 Regular expressions"), !get_config('system','no_utf'), t("Use PHP UTF8 regular expressions")), '$community_page_style' => array('community_page_style', t("Community Page Style"), get_config('system','community_page_style'), t("Type of community page to show. 'Global community' shows every public posting from an open distributed network that arrived on this server."), $community_page_style_choices), '$max_author_posts_community_page' => array('max_author_posts_community_page', t("Posts per user on community page"), get_config('system','max_author_posts_community_page'), t("The maximum number of posts per user on the community page. (Not valid for 'Global Community')")), '$ostatus_disabled' => array('ostatus_disabled', t("Enable OStatus support"), !get_config('system','ostatus_disabled'), t("Provide built-in OStatus \x28StatusNet, GNU Social etc.\x29 compatibility. All communications in OStatus are public, so privacy warnings will be occasionally displayed.")), diff --git a/view/templates/admin_site.tpl b/view/templates/admin_site.tpl index 138d04311..86efb26a2 100644 --- a/view/templates/admin_site.tpl +++ b/view/templates/admin_site.tpl @@ -114,7 +114,6 @@

    {{$advanced}}

    {{include file="field_select.tpl" field=$rino}} - {{include file="field_checkbox.tpl" field=$no_utf}} {{include file="field_checkbox.tpl" field=$verifyssl}} {{include file="field_input.tpl" field=$proxy}} {{include file="field_input.tpl" field=$proxyuser}} diff --git a/view/theme/frost-mobile/templates/admin_site.tpl b/view/theme/frost-mobile/templates/admin_site.tpl index 08b068c40..fb3694f59 100644 --- a/view/theme/frost-mobile/templates/admin_site.tpl +++ b/view/theme/frost-mobile/templates/admin_site.tpl @@ -52,7 +52,6 @@

    {{$advanced}}

    - {{include file="field_checkbox.tpl" field=$no_utf}} {{include file="field_checkbox.tpl" field=$verifyssl}} {{include file="field_input.tpl" field=$proxy}} {{include file="field_input.tpl" field=$proxyuser}} diff --git a/view/theme/frost/templates/admin_site.tpl b/view/theme/frost/templates/admin_site.tpl index e9f472a24..535242a3e 100644 --- a/view/theme/frost/templates/admin_site.tpl +++ b/view/theme/frost/templates/admin_site.tpl @@ -52,7 +52,6 @@

    {{$advanced}}

    - {{include file="field_checkbox.tpl" field=$no_utf}} {{include file="field_checkbox.tpl" field=$verifyssl}} {{include file="field_input.tpl" field=$proxy}} {{include file="field_input.tpl" field=$proxyuser}} From a9dfd1fcb7c835d661ab95f9b0898f7912184777 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Mar 2017 11:31:31 +0000 Subject: [PATCH 19/23] One more API call added --- include/api.php | 1 + 1 file changed, 1 insertion(+) diff --git a/include/api.php b/include/api.php index 08bd835f3..e22c3ba78 100644 --- a/include/api.php +++ b/include/api.php @@ -2721,6 +2721,7 @@ use \Friendica\Core\Config; return api_format_data('config', $type, array('config' => $config)); } + api_register_func('api/gnusocial/config','api_statusnet_config',false); api_register_func('api/statusnet/config','api_statusnet_config',false); function api_statusnet_version($type) { From adf9ed64bb832ab5a26d2be1c311cf38a5baeb60 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Mar 2017 14:16:21 +0000 Subject: [PATCH 20/23] Avoid API error when calling "/api/friendica/profile/show" --- include/api.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/api.php b/include/api.php index 701e527cc..78871828f 100644 --- a/include/api.php +++ b/include/api.php @@ -3963,7 +3963,7 @@ use \Friendica\Core\Config; $multi_profiles = feature_enabled(api_user(),'multi_profiles'); $directory = get_config('system', 'directory'); -// get data of the specified profile id or all profiles of the user if not specified + // get data of the specified profile id or all profiles of the user if not specified if ($profileid != 0) { $r = q("SELECT * FROM `profile` WHERE `uid` = %d AND `id` = %d", intval(api_user()), @@ -3971,11 +3971,10 @@ use \Friendica\Core\Config; // error message if specified gid is not in database if (!dbm::is_result($r)) throw new BadRequestException("profile_id not available"); - } - else + } else { $r = q("SELECT * FROM `profile` WHERE `uid` = %d", intval(api_user())); - + } // loop through all returned profiles and retrieve data and users $k = 0; foreach ($r as $rr) { @@ -4002,9 +4001,11 @@ use \Friendica\Core\Config; } // return settings, authenticated user and profiles data + $self = q("SELECT `nurl` FROM `contact` WHERE `uid`= %d AND `self` LIMIT 1", intval(api_user())); + $result = array('multi_profiles' => $multi_profiles ? true : false, 'global_dir' => $directory, - 'friendica_owner' => api_get_user($a, intval(api_user())), + 'friendica_owner' => api_get_user($a, $self[0]['nurl']), 'profiles' => $profiles); return api_format_data("friendica_profiles", $type, array('$result' => $result)); } From 444de52859baf1fe00cb95dc20ead5203f15fbea Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Mar 2017 16:56:04 +0000 Subject: [PATCH 21/23] Use of exceptions --- include/socgraph.php | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/include/socgraph.php b/include/socgraph.php index ec0f3854d..f576798fc 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -172,10 +172,13 @@ function poco_load_worker($cid, $uid, $zcid, $url) { "contact-type" => $contact_type, "generation" => $generation); - if (sanitized_gcontact($gcontact)) { + try { + $gcontact = sanitize_gcontact($gcontact); $gcid = update_gcontact($gcontact); link_gcontact($gcid, $uid, $cid, $zcid); + } catch (Exception $e) { + logger($e->getMessage(), LOGGER_DEBUG); } } logger("poco_load: loaded $total entries",LOGGER_DEBUG); @@ -200,20 +203,20 @@ function poco_load_worker($cid, $uid, $zcid, $url) { * 4: ... * */ -function sanitized_gcontact(&$gcontact) { +function sanitize_gcontact(&$gcontact) { if ($gcontact['url'] == "") { - return false; + throw new Exception('URL is empty'); } $urlparts = parse_url($gcontact['url']); if (!isset($urlparts["scheme"])) { - return false; + throw new Exception("This (".$gcontact['url'].") doesn't seem to be an url."); } if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com", "identi.ca", "alpha.app.net"))) { - return false; + throw new Exception('Contact from a non federated network ignored. ('.$gcontact['url'].')'); } // Don't store the statusnet connector as network @@ -279,7 +282,7 @@ function sanitized_gcontact(&$gcontact) { $data = Probe::uri($gcontact['url']); if ($data["network"] == NETWORK_PHANTOM) { - return false; + throw new Exception('Probing for URL '.$gcontact['url'].' failed'); } $orig_profile = $gcontact['url']; @@ -299,11 +302,11 @@ function sanitized_gcontact(&$gcontact) { } if (!isset($gcontact['name']) OR !isset($gcontact['photo'])) { - return false; + throw new Exception('No name and photo for URL '.$gcontact['url']); } if (!in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) { - return false; + throw new Exception('No federated network ('.$gcontact['network'].') detected for URL '.$gcontact['url']); } if (!isset($gcontact['server_url'])) { @@ -321,7 +324,7 @@ function sanitized_gcontact(&$gcontact) { $gcontact['server_url'] = ""; } - return true; + return $gcontact; } /** @@ -606,10 +609,13 @@ function poco_last_updated($profile, $force = false) { $gcontact["server_url"] = $data["baseurl"]; - if (sanitized_gcontact($gcontact)) { + try { + $gcontact = sanitize_gcontact($gcontact); update_gcontact($gcontact); poco_last_updated($data["url"], $force); + } catch (Exception $e) { + logger($e->getMessage(), LOGGER_DEBUG); } logger("Profile ".$profile." was deleted", LOGGER_DEBUG); @@ -1888,8 +1894,11 @@ function poco_discover_server($data, $default_generation = 0) { "contact-type" => $contact_type, "generation" => $generation); - if (sanitized_gcontact($gcontact)) { + try { + $gcontact = sanitize_gcontact($gcontact); update_gcontact($gcontact); + } catch (Exception $e) { + logger($e->getMessage(), LOGGER_DEBUG); } logger("Done for profile ".$profile_url, LOGGER_DEBUG); From 585c2119d7d0d6f9db5a8d0a5dee5521144e9d48 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Mar 2017 18:00:56 +0000 Subject: [PATCH 22/23] Remove the "call by reference" --- include/socgraph.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/socgraph.php b/include/socgraph.php index f576798fc..642d03d89 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -194,6 +194,7 @@ function poco_load_worker($cid, $uid, $zcid, $url) { * @brief Sanitize the given gcontact data * * @param array $gcontact array with gcontact data + * @throw Exception * * Generation: * 0: No definition @@ -203,7 +204,7 @@ function poco_load_worker($cid, $uid, $zcid, $url) { * 4: ... * */ -function sanitize_gcontact(&$gcontact) { +function sanitize_gcontact($gcontact) { if ($gcontact['url'] == "") { throw new Exception('URL is empty'); From 18a2b48d1d69d65f84c56e39a052b47de35b0dac Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Mar 2017 19:31:32 +0000 Subject: [PATCH 23/23] Remove unneeded linefeeds --- view/theme/frio/php/default.php | 10 ++-------- view/theme/frio/php/standard.php | 11 ++--------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/view/theme/frio/php/default.php b/view/theme/frio/php/default.php index 56118cc90..87ccf3dc1 100644 --- a/view/theme/frio/php/default.php +++ b/view/theme/frio/php/default.php @@ -4,20 +4,17 @@ * @brief The default site template */ ?> - - - - <?php if(x($page,'title')) echo $page['title'] ?> - + @@ -31,8 +28,6 @@ if(x($page,'htmlhead')) echo $page['htmlhead']; } ?> - - - diff --git a/view/theme/frio/php/standard.php b/view/theme/frio/php/standard.php index 491fa9d5b..9e3d2ca63 100644 --- a/view/theme/frio/php/standard.php +++ b/view/theme/frio/php/standard.php @@ -4,21 +4,17 @@ * @brief The default site template */ ?> - - <?php if(x($page,'title')) echo $page['title'] ?> - + - - "; @@ -54,7 +50,6 @@ "; ?> - @@ -80,7 +75,7 @@ }) }) } - + $('textarea').enterKey(function() {$(this).closest('form').submit(); }, 'ctrl') $('input').enterKey(function() {$(this).closest('form').submit(); }, 'ctrl') @@ -126,7 +121,5 @@ $("nav").bind('nav-update', function(e,data) - -