[texworks] Lua Extension - put into Sentance Case, Trim, Date Time, lowercase, uppercase

GMAIL paul.a.norman at gmail.com
Wed Sep 2 11:44:43 CEST 2009


message from TeXworks user

I am very new to Lua as no doubt the following will confirm, but I have been able to hobble this together from the Tutorials and chm help and other Lua Wiki resources.

Share it in case it inspires any one else...

(Also wondering, does TW object expose any more properties or methods please?

Paul

--[[TeXworksScript
Title: Sentance Case
Description: Puts Selection into Sentance Case beware of Proper Names being lowercased
Author: Paul A Norman From gleanings in Lua chm help Lua 5.1 Reference Manual, wiki, and tutorials
Version: 0.4
Date: 2009-09-02
Script-Type: standalone
]]
-- Sentance Case

 txt = TW.getSelectedText()

 txt = string.lower(txt) -- normlaise case beware of Proper Names being lowercased

 txt = txt:gsub("^%s*(.-)%s*$", "%1") -- trims spaces from front and, back - is impnt for checking for period dot (uk fullstop)

 txt = " " .. txt:gsub("^%l", string.upper)  -- puts first letter into capital adds space to front

     i = string.len(txt)

    rr = txt:sub(i)

 if rr ~= "." then txt = txt .. "." end

  txt = txt ..  " " -- adds space to end to avoid collissions, LaTeX doesn't worry about extra spaces if we over do it

 TW.replaceSelectedText(txt)

----------------

--[[TeXworksScript
Title: Trim
Description: Trims Selection
Author: Paul A Norman From gleanings in Lua chm help Lua 5.1 Reference Manual, wiki, and tutorials
Version: 0.1
Date: 2009-09-02
Script-Type: standalone
]]
-- Trim blanks from front and back


 txt = TW.getSelectedText()

 txt = txt:gsub("^%s*(.-)%s*$", "%1")

 TW.replaceSelectedText(txt)

------------------

--[[TeXworksScript
Title: Title Case
Description: Puts Selection into Title Case
Author: Paul A Norman From gleanings in Lua chm help Lua 5.1 Reference Manual, wiki, and tutorials
Version: 0.1
Date: 2009-09-02
Script-Type: standalone
]]
--Change an entire string to Title Case (i.e. capitalise the first letter of each word)

local function tchelper(first, rest)
  return first:upper()..rest:lower()
end
-- Add extra characters to the pattern if you need to. _ and ' are
--  found in the middle of identifiers and English words.
-- We must also put %w_' into [%w_'] to make it handle normal stuff
-- and extra stuff the same.
-- This also turns hex numbers into, eg. 0Xa7d4

 txt = TW.getSelectedText()

 txt = string.lower(txt)

 txt = txt:gsub("(%a)([%w_']*)", tchelper)

 TW.replaceSelectedText(txt)

-----------------------

--[[TeXworksScript
Title: Date Time
Description: Puts Date Time in
Author: Paul A Norman From gleanings in Lua chm help Lua 5.1 Reference Manual, wiki, and tutorials
Version: 0.1
Date: 2009-09-02
Script-Type: standalone
]]

-- txt = TW.getSelectedText()

 TW.replaceSelectedText(os.date())

--------------------------

--[[TeXworksScript
Title: Lowercase
Description: Puts Selection into Lowercase
Author: Paul A Norman From gleanings in Lua chm help Lua 5.1 Reference Manual, wiki, and tutorials
Version: 0.1
Date: 2009-09-02
Script-Type: standalone
]]

 txt = TW.getSelectedText()

 txt = string.lower(txt)

 TW.replaceSelectedText(txt)

-----------------------

--[[TeXworksScript
Title: Uppercase
Description: Puts Selection into Uppercase
Author: Paul A Norman From gleanings in Lua chm help Lua 5.1 Reference Manual, wiki, and tutorials
Version: 0.1
Date: 2009-09-02
Script-Type: standalone
]]

 txt = TW.getSelectedText()

 txt = string.upper(txt)

 TW.replaceSelectedText(txt)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://tug.org/pipermail/texworks/attachments/20090902/1757ff04/attachment.html>


More information about the texworks mailing list