summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/spelling/spelling-stage-4.lua
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2013-05-24 00:18:09 +0000
committerKarl Berry <karl@freefriends.org>2013-05-24 00:18:09 +0000
commitfed22753adeb474e7470dbe7c96326fd6bcfbc27 (patch)
tree51a481e1b11ee6aad48949a36db9df820a459bac /Master/texmf-dist/scripts/spelling/spelling-stage-4.lua
parent9c422e89d41cc8126f2379412df4a7ab8cb30f0f (diff)
spelling (23may13)
git-svn-id: svn://tug.org/texlive/trunk@30665 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/spelling/spelling-stage-4.lua')
-rwxr-xr-xMaster/texmf-dist/scripts/spelling/spelling-stage-4.lua95
1 files changed, 37 insertions, 58 deletions
diff --git a/Master/texmf-dist/scripts/spelling/spelling-stage-4.lua b/Master/texmf-dist/scripts/spelling/spelling-stage-4.lua
index 734d3006098..ab4fabb22f7 100755
--- a/Master/texmf-dist/scripts/spelling/spelling-stage-4.lua
+++ b/Master/texmf-dist/scripts/spelling/spelling-stage-4.lua
@@ -19,7 +19,7 @@
--
-- @author Stephan Hennig
-- @copyright 2012, 2013 Stephan Hennig
--- @release version 0.3
+-- @release version 0.4
--
-- @trick Prevent LuaDoc from looking past here for module description.
--[[ Trick LuaDoc into entering 'module' mode without using that command.
@@ -31,9 +31,15 @@ module(...)
local M = {}
+-- Import external modules.
+local unicode = require('unicode')
+
+
-- Function short-cuts.
local tabconcat = table.concat
-local utf8len = unicode.utf8.len
+local tabinsert = table.insert
+
+local Ulen = unicode.utf8.len
-- Declare local variables to store references to resources that are
@@ -43,43 +49,20 @@ local utf8len = unicode.utf8.len
local __text_document
---- Set module resources.
--- Make various resources, that are provided by external code, available
--- to this module.
---
--- @param res Ressource table.
-local function set_resources(res)
- __text_document = res.text_document
-end
-M.set_resources = set_resources
-
-
--- Module options.
-- This table contains all module options. User functions to set
-- options are provided.
--
-- @class table
-- @name __opts
--- @field output_eol End-of-line character in output.
-- @field output_file_name Output file name.
-- @field output_line_length Line length in output.
local __opts = {
- output_eol,
output_file_name,
output_line_lenght,
}
---- Set output EOL character.
--- Text output will be written with the given end-of-line character.
---
--- @param eol New output EOL character.
-local function set_output_eol(eol)
- __opts.output_eol = eol
-end
-M.set_output_eol = set_output_eol
-
-
--- Set output file name.
-- Text output will be written to a file with the given name.
--
@@ -106,37 +89,39 @@ end
M.set_output_line_length = set_output_line_length
---- Break a paragraph into lines of a fixed length and write the lines
---- to a file.
+--- Break a text paragraph into lines.
+-- Lines are broken at spaces only. Lines containing only one word may
+-- exceed maximum line length.
--
--- @param f A file handle.
-- @param par A text paragraph (an array of words).
-local function __write_text_paragraph(f, par)
- local maxlinelength = __opts.output_line_length
- local eol = __opts.output_eol
+-- @param max_line_len Maximum length of lines in wrapped paragraph. If
+-- the argument is less then 1, paragraph isn't wrapped at all.
+-- @return Table containing the lines of the paragraph.
+local function __wrap_text_paragraph(par, max_line_len)
+ local wrapped_par = {}
-- Index of first word on current line. Initialize current line with
-- first word of paragraph.
- local lstart = 1
+ local line_start = 1
-- Track current line length.
- local llen = utf8len(par[1])
+ local line_len = Ulen(par[line_start])
-- Iterate over remaining words in paragraph.
for i = 2,#par do
- local wlen = utf8len(par[i])
- -- Does word fit onto current line?
- if (llen + 1 + wlen <= maxlinelength) or (maxlinelength < 1) then
- -- Append word to current line.
- llen = llen + 1 + wlen
- else
- -- Write the current line up to the preceeding word to file (words
- -- separated by spaces and with a trailing newline).
- f:write(tabconcat(par, ' ', lstart, i-1), eol)
+ local word_len = Ulen(par[i])
+ local new_line_len = line_len + 1 + word_len
+ -- Maximum line length exceeded?
+ if new_line_len > max_line_len and max_line_len >= 1 then
+ -- Insert current line into wrapped paragraph.
+ tabinsert(wrapped_par, tabconcat(par, ' ', line_start, i-1))
-- Initialize new current line.
- lstart = i
- llen = wlen
+ line_start = i
+ new_line_len = word_len
end
+ -- Append word to current line.
+ line_len = new_line_len
end
- -- Write last line of paragraph.
- f:write(tabconcat(par, ' ', lstart), eol)
+ -- Insert last line of paragraph.
+ tabinsert(wrapped_par, tabconcat(par, ' ', line_start))
+ return wrapped_par
end
@@ -145,14 +130,12 @@ end
local function __write_text_document()
-- Open output file.
local fname = __opts.output_file_name or (tex.jobname .. '.spell.txt')
- local f = assert(io.open(fname, 'wb'))
- local eol = __opts.output_eol
+ local f = assert(io.open(fname, 'w'))
+ local max_line_len = __opts.output_line_length
-- Iterate through document paragraphs.
for _,par in ipairs(__text_document) do
- -- Separate paragraphs by a blank line.
- f:write(eol)
- -- Write paragraph to file.
- __write_text_paragraph(f, par)
+ -- Write wrapped paragraph to file with a leading empty line.
+ f:write('\n', tabconcat(__wrap_text_paragraph(par, max_line_len), '\n'), '\n')
-- Delete paragraph from memory.
__text_document[_] = nil
end
@@ -200,16 +183,12 @@ M.disable_text_output = disable_text_output
--- Module initialisation.
--
local function __init()
+ -- Get local references to package ressources.
+ __text_document = PKG_spelling.res.text_document
-- Set default output file name.
set_output_file_name(nil)
-- Set default output line length.
set_output_line_length(72)
- -- Set default output EOL character.
- if (os.type == 'windows') or (os.type == 'msdos') then
- set_output_eol('\r\n')
- else
- set_output_eol('\n')
- end
-- Remember call-back status.
__is_active_output = false
end