Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lua/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
--
-- (c) 2007, 2008 Yuri Takhteyev (yuri@freewisdom.org)
-- (c) 2007 Hisham Muhammad
-- (c) 2014 T s T (tst2005 gmail com) fixed for lua5.2
--
-- License: MIT/X, see http://sputnik.freewisdom.org/en/License
-----------------------------------------------------------------------------

module(..., package.seeall)
--define a module without call of module() function
local _M = {}

SKIP_SEPARATOR = true -- a constant
_M.SKIP_SEPARATOR = true -- a constant
-- SKIP_SEPARATOR not-used?

IN = "in"; OUT = "out"; SAME = "same" -- token statuses
_M.IN = "in"; _M.OUT = "out"; _M.SAME = "same" -- token statuses

-----------------------------------------------------------------------------
-- Split a string into tokens. (Adapted from Gavin Kistner's split on
Expand All @@ -23,6 +26,7 @@ IN = "in"; OUT = "out"; SAME = "same" -- token statuses
-- @param skip_separator [optional] don't include the sepator in the results.
-- @return A list of tokens.
-----------------------------------------------------------------------------
local
function split(text, separator, skip_separator)
separator = separator or "%s+"
local parts = {}
Expand Down Expand Up @@ -53,6 +57,7 @@ end
-- @param t2 the second string.
-- @return the least common subsequence as a matrix.
-----------------------------------------------------------------------------
local
function quick_LCS(t1, t2)
local m = #t1
local n = #t2
Expand Down Expand Up @@ -98,6 +103,7 @@ end
-- @param text The string to be escaped.
-- @return Escaped string.
-----------------------------------------------------------------------------
local
function escape_html(text)
text = text:gsub("&", "&amp;"):gsub(">","&gt;"):gsub("<","&lt;")
text = text:gsub("\"", "&quot;")
Expand All @@ -111,6 +117,7 @@ end
-- @param tokens a table of {token, status} pairs.
-- @return an HTML string.
-----------------------------------------------------------------------------
local
function format_as_html(tokens)
local diff_buffer = ""
local token, status
Expand Down Expand Up @@ -138,6 +145,7 @@ end
-- white space).
-- @return A list of annotated tokens.
-----------------------------------------------------------------------------
local
function diff(old, new, separator)
assert(old); assert(new)
new = split(new, separator); old = split(old, separator)
Expand All @@ -159,6 +167,8 @@ function diff(old, new, separator)
suffix = token..suffix
end

local IN, OUT, SAME = _M.IN, _M.OUT, _M.SAME

-- Setup a table that will store the diff (an upvalue for get_diff). We'll
-- store it in the reverse order to allow for tail calls. We'll also keep
-- in this table functions to handle different events.
Expand Down Expand Up @@ -211,4 +221,10 @@ function diff(old, new, separator)
end


_M.split = split
_M.quick_LCS = quick_LCS
_M.escape_html = escape_html
_M.format_as_html = format_as_html
_M.diff = diff

return _M
2 changes: 1 addition & 1 deletion tests/basic.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


require"diff"
local diff = require"diff"



Expand Down