我很想做你在这个网站上编辑帖子时会发生什么.
我正在使用wmd作为我的markdown编辑器,当然当我转到编辑时,我得到它生成的HTML而不是像stackoverflow那样的降价.现在,有没有办法存储两者?或者它是否足够可靠,只需将HTML转换回markdown即可在wmd编辑器中显示?
谢谢!
我需要将HTML转换为等效的Markdown结构化文本.
由于我使用PHP编程,有些人表示Markdownify可以完成这项工作,但不幸的是,代码没有更新,实际上它没有用.在sourceforge.net/projects/markdownify有一个"注意:不支持 - 你想维护这个项目吗?联系我!Markdownify是一个用PHP编写的HTML到Markdown转换器.看它是html2text.php的继承者,因为它有更好的设计,更好的性能和更少的角落情况."
根据我的发现,我只有两个不错的选择:
Python:Aaron Swartz的html2text.py
Ruby:Singpolyma的html2markdown.rb,基于Nokogiri
所以,从PHP,我需要传递HTML代码,调用Ruby/Python脚本并接收输出.
(顺便说一句,一个民众在这里提出了一个类似的问题("如何从php调用ruby脚本?"),但我的案例没有实用信息).
按照Tin Man的提示(下图),我得到了这个:
PHP代码:
$t='<p><b>Hello</b><i>world!</i></p>';
$scaped=preg_quote($t,"/");
$program='python html2md.py';
//exec($program.' '.$scaped,$n); print_r($n); exit; //Works!!!
$input=$t;
$descriptorspec=array(
array('pipe','r'),//stdin is a pipe that the child will read from
array('pipe','w'),//stdout is a pipe that the child will write to
array('file','./error-output.txt','a')//stderr is a file to write to
);
$process=proc_open($program,$descriptorspec,$pipes);
if(is_resource($process)){
fwrite($pipes[0],$input);
fclose($pipes[0]);
$r=stream_get_contents($pipes[1]);
fclose($pipes[1]);
$return_value=proc_close($process);
echo "command returned $return_value\n";
print_r($pipes);
print_r($r);
}
Run Code Online (Sandbox Code Playgroud)
Python代码:
#! /usr/bin/env python
import html2text
import …Run Code Online (Sandbox Code Playgroud)