开发者

Interpret/Render output from puts() as HTML

开发者 https://www.devze.com 2023-03-11 04:35 出处:网络
When I run my ruby script, I want the output to be rendered as HTML, preferably with a browser (e.g. Chrome). However, I would very much prefer if I didn\'t have to start a webservice, because I\'m no

When I run my ruby script, I want the output to be rendered as HTML, preferably with a browser (e.g. Chrome). However, I would very much prefer if I didn't have to start a webservice, because I'm not making a website. I've tried sinatra, and the problem with it, is that I have to restart the server every time I do changes to my code, plus it features requests开发者_JAVA技巧 (like GET/POST-arguments) which I don't really need.

I simply prefer the output from my Ruby program to appear as HTML as opposed to console-text -- since html allows for more creative/expressive output. Is there a good/simple/effective way to do this? (I'm using notepad++ to edit my code, so if its possible to combine the above with it somehow, that would be awesome).

Thanks alot :)


Using the gem shotgun you can run a Sinatra app that automatically reloads changes without restarting the server.

Alternatively, using a library like awesome_print which has HTML formatting, you could write a function which takes the output and saves it to a file. Then open the file in Chrome.

If you don't want to have to manually refresh the page in Chrome, you could take a look at guard-livereload (https://github.com/guard/guard-livereload) which will monitor a given file using the guard gem and reload Chrome. Ryan Bates has a screenshot on guard here, http://railscasts.com/episodes/264-guard.

Here's a function that overrides Kernel#puts to print the string to STDOUT and write the HTML formatted version of it to output.html.

require 'awesome_print'

module Kernel
    alias :old_puts :puts
    def puts(string)
        old_puts string
        File.open("output.html", "w") do |file|
            file.puts string.ai(:html => true)
        end
    end
end

puts "test"
0

精彩评论

暂无评论...
验证码 换一张
取 消