代码之家  ›  专栏  ›  技术社区  ›  nzifnab

仅在换行符处插入换行符,使换行符不超过80个字符

  •  -1
  • nzifnab  · 技术社区  · 6 年前

    以下是一些示例内容的外观:

    """
    Mittens the cat ate a salad on Friday morning. He's a cat, so I'm not really sure why he was eating a salad, but that's what he was doing. Do cats normally like salad? It wasn't salmon-flavored or anything crazy like that.
    
    I can think of three reasons why a cat might eat salad:
    1. The cat is insane
    2. The cat likes mice, and just last week we noticed that a mice was eating some salad, so maybe the cat decided to shortcut the food chain and eat a salad instead of getting the nutrients of the salad from the consumption of the mouse.
    3. Cats are weird.
    """
    

    运行完newline插入器之后,它将如下所示:

    """
    Mittens the cat ate a salad on Friday morning. He's a cat, so I'm not really
    sure why he was eating a salad, but that's what he was doing. Do cats normally
    like salad? It wasn't salmon-flavored or anything crazy like that.
    
    I can think of three reasons why a cat might eat salad:
    1. The cat is insane
    2. The cat likes mice, and just last week we noticed that a mice was eating some
    salad, so maybe the cat decided to shortcut the food chain and eat a salad
    instead of getting the nutrients of the salad from the consumption of the mouse.
    3. Cats are weird.
    """
    

    我发现有几个问题可以解决在N个字符处添加新行的问题。我知道我可以在空间上分开 计数

    我的直觉告诉我,使用regex和look aheads/look behinds有一个很好的解决方案。

    content = """
    this is some content with words and stuff
    and here is another line things
    """
    content = content.gsub(%r{(.{10}) }, "\\1\n")
    puts content
    

    哪些输出:

    this is some
    content with
    words and stuff
    and here is
    another line
    things
    

    但它让线几乎消失了

    2 回复  |  直到 6 年前
        1
  •  2
  •   sawa    6 年前

    puts content.gsub(/(.{1,10})(?:\s+|$)/, "\\1\n")
    # >>this is
    # >>some
    # >>content
    # >>with words
    # >>and stuff
    # >>and here
    # >>is another
    # >>line
    # >>things
    

    囊性纤维变性。 https://apidock.com/rails/ActionView/Helpers/TextHelper/word_wrap

        2
  •  1
  •   nzifnab    6 年前

    我最终选择了穿越路线:

    def wordwrap(content, line_length)
      words = content.scan(/(?:\A|\s)[^\s]*/)
      remaining = line_length
      words.each do |word|
        if word.length > remaining
          word.gsub!(/^\s/, "")
          remaining = line_length - word.length
          word.insert(0, "\n")
        else
          if word =~ /^\n/
            remaining = line_length - word.length - 1
          else
            remaining -= word.length
          end
        end
      end
    
      words.join
    end
    

    line_length 角色。

    这比我预想的要混乱一点,但它完成了任务。

        3
  •  0
  •   Cary Swoveland    5 年前
    r = /.{,80}[\n ]/ 
    

    puts content.gsub(r) { |s| s[0..-2] << "\n" }
    

    显示以下内容:

    Mittens the cat ate a salad on Friday morning. He's a cat, so I'm not really
    sure why he was eating a salad, but that's what he was doing. Do cats normally
    like salad? It wasn't salmon-flavored or anything crazy like that.
    
    I can think of three reasons why a cat might eat salad:
    1. The cat is insane.
    2. The cat likes mice, and just last week we noticed that a mice was eating some
    salad, so maybe the cat decided to shortcut the food chain and eat a salad
    instead of getting the nutrients of the salad from the consumption of the mouse.
    3. Cats are weird.
    

    正则表达式最多可匹配80个字符,后跟换行符或空格。无论匹配的最后一个字符是空格还是换行符,最后一个字符都将替换为块中的换行符。

    要使此操作生效,字符串在一行中不能有多个空格,并且字符串必须以换行符结尾(即。, "...weird.\n" ). 如果这些条件不能保持简单的预处理步骤,则可以保证:

    mod_content = content.squeeze(' ').chomp << "\n"