如何使用ruby脚本填充html文件

Abo*_*our 1 html ruby

我有一个html文件有一般设计(一些div),我需要用一些HTML代码填充这个div使用ruby脚本.
有什么建议?

我有page.html

<html>
<title>html Page</title>
<body>

<div id="main">
</div>

<div id="side">
</div>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我在里面收集一些数据并对它进行某种处理,我希望以一种漂亮的格式呈现它**
所以我想设置它的id为id = main的一些html代码就像这样

<html>
<title>html Page</title>
<body>

<div id="main">
<h1>you have 30 files in games folder</h1>
</div>

<div id="side">
</div>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

**为什么我不使用ROR?因为我不想建立一个网站,我只需要构建一个桌面工具,但它的表示层是由浏览器解释的html代码,以避免使用图形库


我的问题不是"我怎么能写这个html文件"我可以处理它.
我的问题,如果我想在主div中的html文件中创建一个表,我将在ruby脚本中写入整个html代码将其打印到html文件,是否有任何lib或gem我可以告诉它我想要的一个包含3行和2列的表,它会生成html代码吗?

Noa*_*ark 6

您是否正在尝试创建动态网站?为此使用Rails.你想创建一个静态网站吗?像杰基尔这样的东西可能是最好的.您是否想要创建一些简单的.html文件,您可以在某处FTP?Jekyll可能是一个不错的选择,甚至手动编写一个快速的小型HTML生成器可能是一个更好的选择.

更新:

这是你想要的?

hash = {
:games          => "you have 30 files in games folder",
:puppies        => "you have 12 puppies in your pocket",
:pictures   => "You have 9 files in pictures folder",
}

array = [

['run','x','y'],
[1,10,3],
[2,12,9],
[3,14,7],

]

hash.each do |key, value|
myfile = File.new("#{key}.html", "w+") 
myfile.puts "<html>"
myfile.puts "<title>html Page</title>"
myfile.puts "<body>"

myfile.puts "<div id=\"main\">"
myfile.puts "<h1>#{value}</h1>"

myfile.puts "<table border=\"1\">"
array.each do |row|
    myfile.puts "<tr>"
    row.each do |cell|
        myfile.puts "<td> #{cell} </td>"
    end
    myfile.puts "<tr>"
end

myfile.puts "</div>"

myfile.puts "<div id=\"side\">"
myfile.puts "</div>"

myfile.puts "</body>"
myfile.puts "</html>"
Run Code Online (Sandbox Code Playgroud)

结束


Phr*_*ogz 6

我历来使用ERB和REXML这样的事情,因为它们都附带Ruby(删除gem依赖项).您可以将一个XML文件(内容)与一个.erb文件(用于布局)组合在一起,并进行简单的合并.这是我为此编写的一个脚本(其中大部分是参数处理和使用一些便捷方法扩展REXML):

USAGE = <<ENDUSAGE
Usage:
   rubygen source_xml [-t template_file] [-o output_file]

   -t,--template  The ERB template file to merge (default: xml_name.erb)

   -o,--output    The output file name to write  (default: template.txt)
                  If the template_file is named "somefile_XXX.yyy",
                  the output_file will default instead to "somefile.XXX"
ENDUSAGE

ARGS = {}
UNFLAGGED_ARGS = [ :source_xml ]
next_arg = UNFLAGGED_ARGS.first
ARGV.each{ |arg|
   case arg
     when '-t','--template'
       next_arg = :template_file
     when '-o','--output'
       next_arg = :output_file
     else
       if next_arg
         ARGS[next_arg] = arg
         UNFLAGGED_ARGS.delete( next_arg )
       end
       next_arg = UNFLAGGED_ARGS.first
   end
}

if !ARGS[:source_xml]
   puts USAGE
   exit
end

extension_match = /\.[^.]+$/
template_match  = /_([^._]+)\.[^.]+$/
xml_file      = ARGS[ :source_xml   ]
template_file = ARGS[ :template_file] || xml_file.sub( extension_match, '.erb' )
output_file   = ARGS[ :output_file  ] || ( ( template_file =~ template_match ) ? template_file.sub( template_match, '.\\1' ) : template_file.sub( extension_match, '.txt' ) )

require 'rexml/document'
include REXML

class REXML::Element
  # Find all descendant nodes with a specified tag name and/or attributes
  def find_all( tag_name='*', attributes_to_match={} )
    self.each_element( ".//#{REXML::Element.xpathfor(tag_name,attributes_to_match)}" ){}
  end
  # Find all child nodes with a specified tag name and/or attributes
  def kids( tag_name='*', attributes_to_match={} )
    self.each_element( "./#{REXML::Element.xpathfor(tag_name,attributes_to_match)}" ){}
  end
  def self.xpathfor( tag_name='*', attributes_to_match={} )
    out = "#{tag_name}"
    unless attributes_to_match.empty?
      out << "["
      out << attributes_to_match.map{ |key,val|
        if val == :not_empty
          "@#{key}"
        else
          "@#{key}='#{val}'"
        end
      }.join( ' and ' )
      out << "]"
    end
    out
  end

  # A hash to tag extra data onto a node during processing
  def _mydata
    @_mydata ||= {}
  end
end

start_time = Time.new
  @xmldoc = Document.new( IO.read( xml_file ), :ignore_whitespace_nodes => :all )
  @root = @xmldoc.root
  @root = @root.first if @root.is_a?( Array )
end_time = Time.new
puts "%.2fs to parse XML file (#{xml_file})" % ( end_time - start_time )

require 'erb'
File.open( output_file, 'w' ){ |o|
  start_time = Time.new
  output_code = ERB.new( IO.read( template_file ), nil, '>', 'output' ).result( binding )
  end_time = Time.new
  puts "%.2fs to run template   (#{template_file})" % ( end_time - start_time )

  start_time = Time.new
  o << output_code
}
end_time = Time.new
puts "%.2fs to write output   (#{output_file})" % ( end_time - start_time )
puts " "
Run Code Online (Sandbox Code Playgroud)

这可以用于HTML或自动源代码生成.

但是,这些天我主张使用HamlNokogiri(如果你想要结构化的XML标记)或YAML(如果你想要简单的编辑内容),因为这些将使你的标记更清洁,你的模板逻辑更简单.

编辑:这是一个简单的文件,将YAML与Haml合并.最后四行完成所有工作:

#!/usr/bin/env ruby
require 'yaml'; require 'haml'; require 'trollop'
EXTENSION = /\.[^.]+$/

opts = Trollop.options do
  banner "Usage:\nyamlhaml [opts] <sourcefile.yaml>"
  opt :haml,   "The Haml file to use (default: sourcefile.haml)", type:String
  opt :output, "The file to create   (default: sourcefile.html)", type:String
end
opts[:source] = ARGV.shift
Trollop.die "Please specify an input Yaml file" unless opts[:source]
Trollop.die "Could not find #{opts[:source]}"   unless File.exist?(opts[:source])

opts[:haml]   ||= opts[:source].sub( EXTENSION, '.haml' )
opts[:output] ||= opts[:source].sub( EXTENSION, '.html' )
Trollop.die "Could not find #{opts[:haml]}" unless File.exist?(opts[:haml])

@data = YAML.load(IO.read(opts[:source]))
File.open( opts[:output], 'w' ) do |output|
  output << Haml::Engine.new(IO.read(opts[:haml])).render(self)
end
Run Code Online (Sandbox Code Playgroud)

这是一个示例YAML文件:

title: Hello World
main: "<h1>you have 30 files in games folder</h1>"
side: "I dunno, something goes here."
Run Code Online (Sandbox Code Playgroud)

...和示例Haml文件:

!!! 5
%html
  %head
    %title= @data['title']
  %body
    #main= @data['main']
    #side= @data['side']
Run Code Online (Sandbox Code Playgroud)

......最后是他们制作的HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <div id='main'><h1>you have 30 files in games folder</h1></div>
    <div id='side'>I dunno, something goes here.</div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)