Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将纯文本列表转换为html_Html_Ruby_Parsing - Fatal编程技术网

将纯文本列表转换为html

将纯文本列表转换为html,html,ruby,parsing,Html,Ruby,Parsing,我有这样一个纯文本列表: I am the first top-level list item I am his son Me too Second one here His son His daughter I am the son of the one above Me too because of the indentation Another one 我是第一个顶级列表项 我是他的儿子 我也是 这里是第二个 他的儿子 他的女儿 我是上面那个人的儿子 我

我有这样一个纯文本列表:

I am the first top-level list item I am his son Me too Second one here His son His daughter I am the son of the one above Me too because of the indentation Another one 我是第一个顶级列表项 我是他的儿子 我也是 这里是第二个 他的儿子 他的女儿 我是上面那个人的儿子 我也是因为压痕 另一个 我想把它变成:

<ul>
  <li>I am the first top-level list-item
    <ul>
      <li>I am his son</li>
      <li>Me too</li>
    </ul>
  </li>
  <li>Second one here
    <ul>
      <li>His son</li>
      <li>His daughter
        <ul>
          <li>I am the son of the one above</li>
          <li>Me too because of the indentation</li>
        </ul>
      </li>
      <li>Another one</li>
    </ul>
  </li>
</ul>
  • 我是第一个顶级列表项
    • 我是他的儿子
    • 我也是
  • 这里是第二个
    • 他的儿子
    • 他的女儿
      • 我是上面那个人的儿子
      • 我也是因为压痕
    • 另一个

你会怎么做呢?

你可以通过做一些简单的查找和替换来做到这一点。mac上的TextWrangler、Windows上的Notepad++以及linux上的gedit等程序(不确定其find功能与复杂功能的配合程度)可以搜索换行符,并用其他功能替换它们。从最高级的材料开始,然后按自己的方式工作(从前面没有空格的材料开始,然后按自己的方式工作)。你可能需要做一些实验才能得到正确的东西。如果这是你想经常做的事情,你可能会制作一个小脚本,但我怀疑是这样。

我从未使用过ruby,但通常的算法保持不变:

  • 创建如下数据结构:
    节点=>(文本=>字符串,子节点=>节点数组)
  • 读一行
  • 检查缩进是否高于当前缩进
  • 如果是,则将该行附加到当前节点的子节点,并在节点处于活动状态时递归调用该方法。从2点继续
  • 检查缩进是否等于当前缩进
  • 如果是,请将该行附加到活动节点。从2点继续
  • 检查缩进是否低于当前缩进
  • 如果是,则从方法返回
  • 重复上述步骤,直到EOF
  • 对于输出:

    1. print <ul>
    2. Take the first node, print <li>node.Text
    3. If there are child nodes (count of node.Children > 0) recurse to 1.
    4. print </li>
    5. take next node, continue from 2.
    6. print </ul>
    
    1。打印
      2.取第一个节点,打印
    • node.Text 3.如果存在子节点(节点计数。子节点>0),则递归为1。 4.印刷品
    • 5.取下一个节点,从2继续。 6.打印

    此代码确实按预期工作,但标题打印在新行上

    require "rubygems"
    require "builder"
    
    def get_indent(line)
      line.to_s =~ /(\s*)(.*)/
      $1.size
    end
    
    def create_list(lines, list_indent = -1, 
           b = Builder::XmlMarkup.new(:indent => 2, :target => $stdout))
      while not lines.empty?
        line_indent = get_indent lines.first
    
        if line_indent == list_indent
          b.li {
            b.text! lines.shift.strip + $/
            if get_indent(lines.first) > line_indent
              create_list(lines, line_indent, b)
            end
          }
        elsif line_indent < list_indent
          break
        else
          b.ul {
            create_list(lines, line_indent, b)
          }
        end
      end
    end
    
    需要“rubygems”
    需要“建设者”
    def get_缩进(行)
    line.to_s=~/(\s*)(.*)行/
    1.5美元尺码
    结束
    def create_list(行,列表缩进=-1,
    b=Builder::XmlMarkup.new(:indent=>2,:target=>$stdout))
    而不是空的?
    行缩进=先获取行缩进
    如果行缩进==列表缩进
    b、 李{
    b、 text!lines.shift.strip+$/
    如果获取缩进(第一行)>行缩进
    创建列表(行、行缩进、b)
    结束
    }
    elsif行缩进<列表缩进
    打破
    其他的
    b、 保险商实验室{
    创建列表(行、行缩进、b)
    }
    结束
    结束
    结束
    
    将输入转换为,然后将其呈现为HTML

    require 'haml'
    
    def text_to_html(input)
      indent = -1
      haml = input.gsub(/^( *)/) do |match|
        line_indent = $1.length
        repl = line_indent > indent ? "#{$1}%ul\n" : ''
        indent = line_indent
        repl << "  #{$1}%li "
      end
      Haml::Engine.new(haml).render
    end
    
    puts text_to_html(<<END)
    I am the first top-level list item
      I am his son
      Me too
    Second one here
      His son
      His daughter
        I am the son of the one above
        Me too because of the indentation
      Another one
    END
    
    require'haml'
    def text_to_html(输入)
    缩进=-1
    haml=input.gsub(/^(*)不匹配|
    行缩进=$1.5长度
    repl=行缩进>缩进?“{$1}%ul\n”:”
    缩进=行缩进
    回复老话题,但是。。。
    看起来我找到了一种使Glenn Jackman代码html有效的方法(避免使用孩子的
    )。
    我使用带制表符缩进的字符串

        require 'haml'
        class String
           def text2htmllist
             tabs = -1
             topUL=true
             addme=''
    
             haml = self.gsub(/^([\t]*)/) do |match|
               line_tabs = match.length
    
               if ( line_tabs > tabs )
                    if topUL
                        repl = "#{match}#{addme}%ul\n"
                        topUL=false
                    else
                        repl = "#{match}#{addme}%li\n"
                        addme += "\t"
                        repl += "#{match}#{addme}%ul\n"
                    end
               else
                  repl = ''
                  addme = addme.gsub(/^[\t]/,'') if ( line_tabs < tabs ) #remove one \t 
               end
               tabs = line_tabs
               repl << "\t#{match}#{addme}%li "
    
             end
             puts haml
             Haml::Engine.new(haml).render
           end
        end #String class
    
        str = <<FIM
        I am the first top-level list item
            I am his son
            Me too
        Second one here
            His son
            His daughter
                I am the son of the one above
                Me too because of the indentation
            Another one
        FIM
    
        puts str.text2htmllist
    
    require'haml'
    类字符串
    def text2htmlist
    制表符=-1
    topUL=true
    地址=“”
    haml=self.gsub(/^([\t]*)/)不匹配|
    line_tabs=match.length
    如果(行选项卡>选项卡)
    如果托普尔
    repl=“#{match}{addme}%ul\n”
    topUL=false
    其他的
    repl=“#{match}{addme}%li\n”
    addme+=“\t”
    repl+=“{match}{addme}%ul\n”
    结束
    其他的
    回复=“”
    addme=addme.gsub(/^[\t]/,“”)如果(行选项卡<选项卡)#删除一个\t
    结束
    制表符=行\制表符
    
    人们不再喜欢为自己弄明白这类事情了吗?他给了那些确实喜欢为自己弄明白这些事情的人一个机会!你的纯文本格式是固定的,还是可以稍微修改一下?@ax:你有什么想法@罗布斯托:据我所知,这是一个问编程相关问题的地方。我问了这个与编程有关的问题,因为我自己无法解决。@morbusg:对不起,我不是故意刁难的。我的意思是,这是一个让编程变得有趣的问题。这是一个谜题类型乐趣的绿洲,在沙漠中找出为什么这个或那个API调用不起作用,等等。我想他是在问如何在ruby中做到这一点。啊,没有看到标签。我只是看了一下标题和描述。显然,我只能选择一个答案作为接受答案,所以尽管你的答案是一个,但很抱歉我不能将其标记为一个(我选择Yossi's,因为它是ruby的)。我已经将此答案标记为接受,但后来我意识到有一个小问题:如果有一个包含的列表,列表项不应该关闭。这会在列表项有子列表时关闭列表项。例如,某物。。。与某物相比
        require 'haml'
        class String
           def text2htmllist
             tabs = -1
             topUL=true
             addme=''
    
             haml = self.gsub(/^([\t]*)/) do |match|
               line_tabs = match.length
    
               if ( line_tabs > tabs )
                    if topUL
                        repl = "#{match}#{addme}%ul\n"
                        topUL=false
                    else
                        repl = "#{match}#{addme}%li\n"
                        addme += "\t"
                        repl += "#{match}#{addme}%ul\n"
                    end
               else
                  repl = ''
                  addme = addme.gsub(/^[\t]/,'') if ( line_tabs < tabs ) #remove one \t 
               end
               tabs = line_tabs
               repl << "\t#{match}#{addme}%li "
    
             end
             puts haml
             Haml::Engine.new(haml).render
           end
        end #String class
    
        str = <<FIM
        I am the first top-level list item
            I am his son
            Me too
        Second one here
            His son
            His daughter
                I am the son of the one above
                Me too because of the indentation
            Another one
        FIM
    
        puts str.text2htmllist
    
    %ul
        %li I am the first top-level list item
        %li
            %ul
                %li I am his son
                %li Me too
        %li Second one here
        %li
            %ul
                %li His son
                %li His daughter
                %li
                    %ul
                        %li I am the son of the one above
                        %li Me too because of the indentation
                %li Another one
    <ul>
      <li>I am the first top-level list item</li>
      <li>
        <ul>
          <li>I am his son</li>
          <li>Me too</li>
        </ul>
      </li>
      <li>Second one here</li>
      <li>
        <ul>
          <li>His son</li>
          <li>His daughter</li>
          <li>
            <ul>
              <li>I am the son of the one above</li>
              <li>Me too because of the indentation</li>
            </ul>
          </li>
          <li>Another one</li>
        </ul>
      </li>
    </ul>