Tidy binding using DL

Discussion in 'Ruby' started by Kevin Howe, Sep 16, 2004.

  1. Kevin Howe

    Kevin Howe Guest

    After reading the recent post re: Ruby's DL functions I decided to see if I
    could come up with a Tidy binding. So far I have it setting/getting options,
    doing CleanAndRepair and optionally saving to a file. However I'm not sure
    how to get it to save to a string to be returned. The function in question
    is tidyStringSave:

    http://tidy.sourceforge.net/docs/api/group__Save.html#a3
    int tidySaveString(TidyDoc tdoc, tmbstr buffer, uint * buflen)

    I had envisioned it originally working like this:

    output = String.new
    Tidylib.tidySaveString(@tdoc, output)

    But it's looking for a buffer object not a regular string. It's the last 2
    parameters that I'm unsure of. What Ruby objects would I need to create/pass
    to make this work?
     
    Kevin Howe, Sep 16, 2004
    #1
    1. Advertising

  2. Kevin Howe said:
    > http://tidy.sourceforge.net/docs/api/group__Save.html#a3
    > int tidySaveString(TidyDoc tdoc, tmbstr buffer, uint * buflen)


    Try the following code, since 'tmbstr' in tidy header files is
    considered as 'char *' or 'const char *'.

    -- test.rb --
    require 'dl/import'

    module Tidylib
    extend DL::Importable

    dlload "libtidy.so"

    extern "void *tidyCreate()"
    extern "int tidyParseString(void*,char*)"
    extern "int tidySaveString(void*, char*, int ref)"

    module_function

    def create()
    tidyCreate()
    end

    def parse_string(doc, str)
    tidyParseString(doc, str)
    end

    def save_string(doc, out)
    tidySaveString(doc, out, out.size)
    return @args[2]
    end
    end

    out = " "
    doc = Tidylib.create()
    Tidylib.parse_string(doc, <<EOS)
    <html>
    </html>
    EOS
    p Tidylib.save_string(doc, out)

    --
    Takaaki Tateishi <>
     
    Takaaki Tateishi, Sep 16, 2004
    #2
    1. Advertising

  3. Kevin Howe

    Kevin Howe Guest

    > Try the following code, since 'tmbstr' in tidy header files is
    > considered as 'char *' or 'const char *'.


    This runs without errors, but returns a value of 68, with the "out" string
    remaining unchanged.
     
    Kevin Howe, Sep 16, 2004
    #3
  4. Kevin Howe said:
    > This runs without errors, but returns a value of 68, with the "out" string
    > remaining unchanged.


    I corrected that as follows.

    require 'dl/import'

    module Tidylib
    extend DL::Importable

    dlload "libtidy.so"

    extern "void *tidyCreate()"
    extern "int tidyParseString(void*, char*)"
    extern "int tidyCleanAndRepair(void*)"
    extern "int tidySaveString(void*, char*, unsigned int ref)"
    extern "int tidySaveStdout(void*)"
    extern "int tidyRunDiagnostics(void*)"

    module_function

    def create()
    tidyCreate()
    end

    def parse_string(doc, str)
    tidyParseString(doc, str)
    end

    def clean_and_repair(doc)
    tidyCleanAndRepair(doc)
    end

    def save_string(doc)
    str = " " * 1024
    ret = tidySaveString(doc, str, str.size)
    len = @args[2]
    str = @args[1]
    str[0,len]
    end

    def save_stdout(doc)
    tidySaveStdout(doc)
    end

    def run_diagnostics(doc)
    tidyRunDiagnostics(doc)
    end
    end

    doc = Tidylib.create()
    Tidylib.parse_string(doc, <<EOS)
    <title></title>
    EOS
    Tidylib.clean_and_repair(doc)
    Tidylib.run_diagnostics(doc)
    puts Tidylib.save_string(doc)

    --
    Takaaki Tateishi <>
     
    Takaaki Tateishi, Sep 16, 2004
    #4
  5. Kevin Howe

    Kevin Howe Guest

    > Try the following code, since 'tmbstr' in tidy header files is
    > considered as 'char *' or 'const char *'


    Go it. The extern statement was correct, but the buffer string still wasn't
    big enough to hold the results. The following takes care of it:

    buffer = String.new # string buffer
    8192.times { buffer << ' ' } # must be long enough to hold results
    Tidylib.tidySaveString(@doc, buffer, buffer.length)
    output = buffer.strip
     
    Kevin Howe, Sep 16, 2004
    #5
  6. Kevin Howe wrote:

    > buffer = String.new # string buffer
    > 8192.times { buffer << ' ' } # must be long enough to hold results


    Why not simply ' ' * 8192?

    Regards,
    Florian Gross
     
    Florian Gross, Sep 17, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    381
    Magnus Lycka
    Feb 27, 2006
  2. Replies:
    16
    Views:
    1,930
    Jukka K. Korpela
    Mar 19, 2007
  3. Jukka K. Korpela

    Re: Using Html Tidy

    Jukka K. Korpela, Aug 1, 2013, in forum: HTML
    Replies:
    2
    Views:
    260
    Gloops
    Aug 1, 2013
  4. Jukka K. Korpela

    Re: Using Html Tidy

    Jukka K. Korpela, Aug 1, 2013, in forum: HTML
    Replies:
    1
    Views:
    261
    dorayme
    Aug 1, 2013
  5. Gloops

    Re: Using Html Tidy

    Gloops, Aug 1, 2013, in forum: HTML
    Replies:
    0
    Views:
    285
    Gloops
    Aug 1, 2013
Loading...

Share This Page