jfisteus / html2xhtml

Command-line HTML to XHTML converter
http://www.it.uc3m.es/jaf/html2xhtml/
Other
43 stars 23 forks source link

node.js example of using HTTP API #5

Closed denistrofimov closed 10 years ago

denistrofimov commented 11 years ago
function html2xhtml(data,callback){
    var options = {
        host: 'www.it.uc3m.es',
        port: 80,
        path: '/jaf/cgi-bin/html2xhtml.cgi',
        method: 'POST'
    };

    options.headers = {
        'Content-Type': 'text/html',
        'Content-Length': data.length
    };

    var req = http.request(options, function(res) {
        res.setEncoding('utf8');
        var body = '';
        res.on('data', function (chunk) {
            body = body + chunk;
        });
        res.on('error',function(err){
            console.log(err);
        });
        res.on('end',function(){
            if(callback){
                callback(body);
            }
        });
    });
    req.on('error',function(err){
        console.log(err);
    });
    req.write(data);
    req.end();
}

Usage:

var data = require('fs').readFileSync('index.html');
html2xhtml(data,function(fixed){
    require('fs').writeFileSync('index2.html',fixed);
});

May be you want to create separate node.js module in npm registry with this code? Ask me, i can and want to help

jfisteus commented 11 years ago

Thanks for sharing this code. I've just created the directory web-api-clients with a couple of examples of clients (python and bash+curl) I already had. If you want, you can place there your node.js code and create a pull request in order to get it merged in the project. Remember to add yourself to the list of contributors inside the AUTHORS file.

If you want to create and maintain a module in npm registry, it would be nice.

gaurangaero commented 9 years ago

host :www.it.uc3m.es is not working could you please help me to find another way in inegrate html2xtml in python code

jfisteus commented 9 years ago

Unfortunately, the web server that hosts the service is temporarily down. It won't be up again until next Monday, when my university opens again after Easter holidays. The server has been quite unreliable lately, because it is unattended on weekends and during the university's holyday periods. I don't have better alternatives for hosting it right now.

Besides waiting to Monday, I can suggest two solutions:

I hope this helps.

gaurangaero commented 9 years ago

hello jfisteus, I am new to python coding, I have use your code for input of some wiki development her please find portion of my code. I am using selenium webdriver for web input.

could you please help me how I can integrate binary of html2xhtml in program? your code for the input:

start here

            headers = {"Content-type": "text/html",
           "Accept": "application/xhtml+xml"}
    params = urllib.urlencode({'tablength': 4,
                           'linelength': 100,
                           'output-charset': 'UTF-8'})
    url = "/jaf/cgi-bin/html2xhtml.cgi?" + params

    # read the input HTML file (first command-line argument)
    in_file = open('Folder_Intex.htm', 'r')
    in_data = in_file.read()

    # connect to the server and send the POST request
    conn = httplib.HTTPConnection("www.it.uc3m.es:80")
    conn.request("POST", url, in_data, headers)
    response = conn.getresponse()

    wiki_p = response.read()
    #conn.close()

it inputs here :+1:

           driver.find_element_by_link_text("HTML Source").send_keys(Keys.RETURN)
    clipboard.copy(wiki_p)
                                      driver.find_element_by_xpath(r'//textarea[@class="qkrPageEditor"]').send_keys(Keys.CONTROL + "v")
    driver.find_element_by_link_text("Rich Text").send_keys(Keys.RETURN)
    time.sleep(10)
    driver.find_element_by_xpath("//input[@value='Save and Close']").send_keys(Keys.RETURN)
    time.sleep(10)
jfisteus commented 9 years ago

Assuming you have html2xhtml compiled and in your PATH:

import subprocess
with open('Folder_index.htm') as in_file):
    wiki_p = subprocess.check_output(['html2xhtml'], stdin=in_file)

If it isn't in your PATH, just write its full or relative path in the first argument of the function call:

wiki_p = subprocess.check_output(['path/to/html2xhtml'], stdin=in_file)

If you need to pass command-line arguments, add each one as a string in the list of the first argument:

wiki_p = subprocess.check_output(['path/to/html2xhtml', '-t', '1.1'], stdin=in_file)
gaurangaero commented 9 years ago

hello jfisteus,

millions of thanks for this help now my code is working correctly.... Gaurang