How to run Ruby / Python scripts from PHP passing and accepting parameters?

I need to turn HTML into equivalent Markdown-structured text.

OBS .: Quick and easy way to do this using PHP and Python .

As I program in PHP, some people tell Markdownify to do this work, but unfortunately the code is not updating, but in fact it does not work . Sourceforge.net/projects/markdownify has a "NOTE: unsupported - do you want to save this project? Contact me! Markdownify is an HTML converter for Markdown, written in PHP. See it as a successor to html2text.php, because it has a better design. better performance and smaller corner cases. "

From what I could find, I have only two good options:

  • Python: Aaron Swartz html2text.py

  • Ruby: Singpolyma html2markdown.rb based on Nokogiri

So, from PHP, I need to pass the HTML code, call Ruby / Python Script and get the output back.

(By the way, people asked a similar question here ("how to call ruby ​​Script from php?"), But without practical information for my case).

Following the prompt of the tin man (below), I got to this:

PHP code:

$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); } 

Python Code:

 #! /usr/bin/env python import html2text import sys print html2text.html2text(sys.argv[1]) #print "Hi!" #works!!! 

With the above, I get the following:

command returned 1 array ([0] => Resource Identifier No. 17 1 => Resource Identifier # 18)

And the file "error-output.txt" says:

Traceback (last last call): File "html2md.py", line 5, in print html2text.html2text (sys.argv 1 ) IndexError: index index is out of range

Any ideas ???


Ruby code ( still parsed )

 #!/usr/bin/env ruby require_relative 'html2markdown' puts HTML2Markdown.new("<h1>#{ ARGF.read }</h1>").to_s 

Just for the record, I tried using the simplest "exec ()" PHP before, but I had problems with some special characters that are very common for the HTML language.

PHP code:

 echo exec('./hi.rb'); echo exec('./hi.py'); 

Ruby Code:

 #!/usr/bin/ruby puts "Hello World!" 

Python Code:

 #!usr/bin/python import sys print sys.argv[1] 

Both work fine. But when the line is a little more complicated:

 $h='<p><b>Hello</b><i>world!</i></p>'; echo exec("python hi.py $h"); 

This did not work.

This is because html string requires special characters. I got this using this:

 $t='<p><b>Hello</b><i>world!</i></p>'; $scaped=preg_quote($t,"/"); 

Now it works, as I said here .

I'm confused: Fedora 14 ruby ​​1.8.7 Python 2.7 perl 5.12.2 PHP 5.3.4 nginx 0.8.53

+8
python html ruby php markdown
source share
5 answers

Ask PHP to open a Ruby or Python script through proc_open by laying the HTML in STDIN in the script. The Ruby / Python script reads and processes the data and returns it via STDOUT back to the PHP script, and then exits. This is the usual way to do things with popen functionality in Perl, Ruby, or Python, and good, because it gives you access to STDERR in case something strikes pieces and doesn't require temporary files, but it's a little more complicated.

Alternative ways to do this could be to write data from PHP to a temporary file, and then use system , exec or something similar to calling a Ruby / Python script to open and process it, and print the output using their STDOUT.

EDIT:

See @Jonke's answer for "Best Practices with STDIN in Ruby?" for example, how to simply read STDIN and write to STDOUT with Ruby. " As you read from stdin in python " has some good examples for this language.

This is a simple example showing how to invoke a Ruby script by passing it a string through the PHP STDIN channel and reading the Ruby script STDOUT:

Save this as "test.php":

 <?php $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", "./error-output.txt", "a") // stderr is a file to write to ); $process = proc_open('ruby ./test.rb', $descriptorspec, $pipes); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt fwrite($pipes[0], 'hello world'); fclose($pipes[0]); echo stream_get_contents($pipes[1]); fclose($pipes[1]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $return_value = proc_close($process); echo "command returned $return_value\n"; } ?> 

Save this as "test.rb":

 #!/usr/bin/env ruby puts "<b>#{ ARGF.read }</b>" 

Running a PHP script gives:

 Greg:Desktop greg$ php test.php <b>hello world</b> command returned 0 

The PHP script opens the Ruby interpreter, which opens the Ruby script. Then, PHP sends him a "hello world." Ruby wraps the resulting text with a bold tag and displays it, which is captured by PHP and then output. There are no temporary files, nothing was transferred on the command line, you could transfer a lot of data if necessary, and it will be pretty fast. Python or Perl could easily be used instead of Ruby.

EDIT:

If you have:

 HTML2Markdown.new('<h1>HTMLcode</h1>').to_s 

as an example of code, you can start developing a Ruby solution with:

 #!/usr/bin/env ruby require_relative 'html2markdown' puts HTML2Markdown.new("<h1>#{ ARGF.read }</h1>").to_s 

Suppose you have already downloaded the HTML2Markdown code and saved it in the current directory and are using Ruby 1.9.2.

+12
source share

In Python PHP, pass var as a command line argument, get it from sys.argv (a list of command line arguments passed to Python), and then Python prints the output, which PHP then repeats. Example:

 #!usr/bin/python import sys print "Hello ", sys.argv[1] # 2nd element, since the first is the script name 

PHP:

 <?php echo exec('python script.py Rafe'); ?> 

In Ruby, the procedure should be basically the same.

+5
source share

Use the variable in the Ruby code and pass it as an argument to the Ruby script from the PHP code. Then, the Ruby script returns the processed code to stdout, which PHP can read.

+2
source share

I think your question is wrong. Your problem is how to convert from HTML to Markdown. I'm right?

Try http://milianw.de/projects/markdownify/ I think this could help you =)

0
source share

Another very strange approach would be similar to the one I used.

 Php file -> output.txt ruby file -> read from output.txt Ruby file-> result.txt Php file -> read from result.txt simple add exec(rubyfile.rb); 

Not recommended, but it will work for sure.

0
source share

Source: https://habr.com/ru/post/650775/


All Articles