Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Tidy binding using DL

Reply
Thread Tools

Tidy binding using DL

 
 
Kevin Howe
Guest
Posts: n/a
 
      09-15-2004
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...__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?


 
Reply With Quote
 
 
 
 
Takaaki Tateishi
Guest
Posts: n/a
 
      09-16-2004
Kevin Howe said:
> http://tidy.sourceforge.net/docs/api...__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 <(E-Mail Removed)>




 
Reply With Quote
 
 
 
 
Kevin Howe
Guest
Posts: n/a
 
      09-16-2004
> 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.


 
Reply With Quote
 
Takaaki Tateishi
Guest
Posts: n/a
 
      09-16-2004
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 <(E-Mail Removed)>


 
Reply With Quote
 
Kevin Howe
Guest
Posts: n/a
 
      09-16-2004
> 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


 
Reply With Quote
 
Florian Gross
Guest
Posts: n/a
 
      09-17-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML Tidy in ASP.NET Christoph Schneegans ASP .Net 2 04-27-2009 11:00 PM
Tidy using unicode does not validate groups2@reenie.org HTML 16 03-19-2007 02:33 PM
Using ElementTree to tidy up an XML string to my liking peterbe@gmail.com Python 6 02-27-2006 03:53 PM
sending command line arguements to HTML Tidy d davis Perl 0 04-27-2004 02:23 PM
Data Binding - using inline code vs. functions vs. straight binding Jordan ASP .Net 2 02-10-2004 08:32 PM



Advertisments