fix nested quotes and alter linebreak parsing
This commit is contained in:
		@@ -22,25 +22,27 @@ function babycode.to_html(s, escape_html)
 | 
				
			|||||||
      return "\1CODE:"..#code_blocks.."\1"
 | 
					      return "\1CODE:"..#code_blocks.."\1"
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  text = text:gsub("  %s?\r?\n", "<br>")
 | 
				
			||||||
 | 
					  -- normalize newlines
 | 
				
			||||||
 | 
					  text = text:gsub("\r?\n\r?\n+", "<br>")
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
  local url_tags = {}
 | 
					  local url_tags = {}
 | 
				
			||||||
  -- replace `[url=https://example.com]Example[/url] tags
 | 
					  -- replace `[url=https://example.com]Example[/url] tags
 | 
				
			||||||
  text = text:gsub("%[url=([^%]]+)%](.-)%[/url%]", function(url, label)
 | 
					  text = text:gsub("%[url=([^%]]+)%](.-)%[/url%]", function(url, label)
 | 
				
			||||||
    table.insert(url_tags, {url = url, label = label})
 | 
					    table.insert(url_tags, {url = url, label = label})
 | 
				
			||||||
    return "\1URL:"..#url_tags.."\1"
 | 
					    return "\1URL:"..#url_tags.."\1"
 | 
				
			||||||
  end)
 | 
					  end)
 | 
				
			||||||
  
 | 
					 | 
				
			||||||
  -- replace `[url]https://example.com[/url] tags
 | 
					 | 
				
			||||||
  -- text = text:gsub("%[url%]([^%]]+)%[/url%]", function(url)
 | 
					 | 
				
			||||||
  --   return '<a href="'..url..'">'..url..'</a>'
 | 
					 | 
				
			||||||
  -- end)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  -- bold, italics, strikethrough
 | 
					  -- bold, italics, strikethrough
 | 
				
			||||||
  text = text:gsub("%[b%](.-)%[/b%]", "<strong>%1</strong>")
 | 
					  text = text:gsub("%[b%](.-)%[/b%]", "<strong>%1</strong>")
 | 
				
			||||||
  text = text:gsub("%[i%](.-)%[/i%]", "<em>%1</em>")
 | 
					  text = text:gsub("%[i%](.-)%[/i%]", "<em>%1</em>")
 | 
				
			||||||
  text = text:gsub("%[s%](.-)%[/s%]", "<del>%1</del>")
 | 
					  text = text:gsub("%[s%](.-)%[/s%]", "<del>%1</del>")
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  text = text:gsub("%[quote%](.-)%[/quote%]", "<blockquote>%1</blockquote>")
 | 
					  -- these can be nested, so replace open and closed separately
 | 
				
			||||||
 | 
					  -- text = text:gsub("%[quote%]", "<blockquote>")
 | 
				
			||||||
 | 
					  -- text = text:gsub("%[/quote%]", "</blockquote>")
 | 
				
			||||||
 | 
					  text = text:gsub("%[(/?)quote%]", "<%1blockquote>")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  -- replace loose links
 | 
					  -- replace loose links
 | 
				
			||||||
  text = text:gsub("(https?://[%w-_%.%?%.:/%+=&~%@#%%]+[%w-/])", function(url)
 | 
					  text = text:gsub("(https?://[%w-_%.%?%.:/%+=&~%@#%%]+[%w-/])", function(url)
 | 
				
			||||||
@@ -57,9 +59,6 @@ function babycode.to_html(s, escape_html)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  -- rule
 | 
					  -- rule
 | 
				
			||||||
  text = text:gsub("\n+%-%-%-", "<hr>")
 | 
					  text = text:gsub("\n+%-%-%-", "<hr>")
 | 
				
			||||||
  
 | 
					 | 
				
			||||||
  -- normalize newlines, replace them with <br>
 | 
					 | 
				
			||||||
  text = text:gsub("\r?\n\r?\n+", "<br>")--:gsub("\r?\n", "<br>")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  -- replace code block placeholders back with their original contents
 | 
					  -- replace code block placeholders back with their original contents
 | 
				
			||||||
  text = text:gsub("\1CODE:(%d+)\1", function(n)
 | 
					  text = text:gsub("\1CODE:(%d+)\1", function(n)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -73,4 +73,22 @@ return {
 | 
				
			|||||||
    schema.add_column("users", "signature_original_markup", types.text{default = ""})
 | 
					    schema.add_column("users", "signature_original_markup", types.text{default = ""})
 | 
				
			||||||
    schema.add_column("users", "signature_rendered", types.text{default = ""})
 | 
					    schema.add_column("users", "signature_rendered", types.text{default = ""})
 | 
				
			||||||
  end,
 | 
					  end,
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  [11] = function ()
 | 
				
			||||||
 | 
					    local render = require("lib.babycode").to_html
 | 
				
			||||||
 | 
					    local html_escape = require("lapis.html").escape
 | 
				
			||||||
 | 
					    local phs = db.query("SELECT * from post_history")
 | 
				
			||||||
 | 
					    local users = db.query("SELECT * from users")
 | 
				
			||||||
 | 
					    db.query("BEGIN")
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    for _, post_history in ipairs(phs) do
 | 
				
			||||||
 | 
					      db.query("UPDATE post_history SET content = ? WHERE id = ?", render(post_history.original_markup, html_escape), post_history.id)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    for _, user in ipairs(users) do
 | 
				
			||||||
 | 
					      db.query("UPDATE users SET signature_rendered = ? WHERE id = ?", render(user.signature_original_markup, html_escape), user.id)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    db.query("COMMIT")
 | 
				
			||||||
 | 
					  end,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -64,14 +64,11 @@
 | 
				
			|||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
    <div class="post-content">
 | 
					    <div class="post-content">
 | 
				
			||||||
      <% if not edit then %>
 | 
					      <% if not edit then %>
 | 
				
			||||||
        <div class="post-inner">
 | 
					        <div class="post-inner"><%- post.content %></div>
 | 
				
			||||||
        <%- post.content %>
 | 
					 | 
				
			||||||
        </div>
 | 
					 | 
				
			||||||
        <% if render_sig and #post.signature_rendered > 0 then %>
 | 
					        <% if render_sig and #post.signature_rendered > 0 then %>
 | 
				
			||||||
          <div class="signature-container">
 | 
					          <div class="signature-container">
 | 
				
			||||||
            <hr>
 | 
					            <hr>
 | 
				
			||||||
            <%- post.signature_rendered %>
 | 
					            <%- post.signature_rendered %></div>
 | 
				
			||||||
          </div>
 | 
					 | 
				
			||||||
        <% end %>
 | 
					        <% end %>
 | 
				
			||||||
      <% else %>
 | 
					      <% else %>
 | 
				
			||||||
        <% render("views.common.babycode-editor", {
 | 
					        <% render("views.common.babycode-editor", {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user