| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class AkHtmlToText |
|---|
| 20 |
{ |
|---|
| 21 |
public function convert() |
|---|
| 22 |
{ |
|---|
| 23 |
require_once(AK_VENDOR_DIR.DS.'TextParsers'.DS.'html2text.php'); |
|---|
| 24 |
$Converter = new html2text(true, 0, false); |
|---|
| 25 |
$markdown = str_replace('__AK:AMP__','&', $Converter->load_string(str_replace('&','__AK:AMP__', $this->source))); |
|---|
| 26 |
|
|---|
| 27 |
require_once(AK_VENDOR_DIR.DS.'TextParsers'.DS.'smartypants.php'); |
|---|
| 28 |
$Smartypants = new SmartyPantsTypographer_Parser(); |
|---|
| 29 |
$markdown = Ak::html_entity_decode(strip_tags($Smartypants->transform($markdown))); |
|---|
| 30 |
|
|---|
| 31 |
return trim($this->_simplifyMarkdown($markdown)); |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
public function _simplifyMarkdown($markdown) |
|---|
| 37 |
{ |
|---|
| 38 |
$markdown = trim($markdown); |
|---|
| 39 |
if(strstr($markdown,"\n")){ |
|---|
| 40 |
if(preg_match_all('/([ \t]*[#]{1,2})(.*)(\{#[a-z0-9_]+}|$)+/i',$markdown,$matches)){ |
|---|
| 41 |
foreach ($matches[0] as $k=>$match){ |
|---|
| 42 |
$simple_markdown = trim($matches[2][$k]); |
|---|
| 43 |
if($match[0] == '#'){ |
|---|
| 44 |
if($simple_markdown[0] == '#'){ |
|---|
| 45 |
$markdown = str_replace($match,'##'.$simple_markdown,$markdown); |
|---|
| 46 |
}else{ |
|---|
| 47 |
$separator = strlen($matches[1][$k]) === 1 ? '=' : '-'; |
|---|
| 48 |
$simple_markdown .= "\n".str_repeat($separator,strlen($simple_markdown)); |
|---|
| 49 |
$markdown = str_replace($match,$simple_markdown,$markdown); |
|---|
| 50 |
} |
|---|
| 51 |
} |
|---|
| 52 |
} |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
return $markdown; |
|---|
| 56 |
} |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
?> |
|---|
| 60 |
|
|---|