| 1 |
%%% spelling.sty |
| 2 |
%%% Copyright 2012, 2013 Stephan Hennig |
| 3 |
%% |
| 4 |
%% This work may be distributed and/or modified under the conditions of |
| 5 |
%% the LaTeX Project Public License, either version 1.3 of this license |
| 6 |
%% or (at your option) any later version. The latest version of this |
| 7 |
%% license is in http://www.latex-project.org/lppl.txt |
| 8 |
%% and version 1.3 or later is part of all distributions of LaTeX |
| 9 |
%% version 2005/12/01 or later. |
| 10 |
%% |
| 11 |
%% See file README for more information. |
| 12 |
%% |
| 13 |
\ProvidesPackage{spelling} |
| 14 |
[2013/05/25 v0.41 Support for spell-checking of LuaTeX documents (SH)] |
| 15 |
\NeedsTeXFormat{LaTeX2e}[1999/12/01] |
| 16 |
% Test for the LuaTeX engine. |
| 17 |
\RequirePackage{ifluatex} |
| 18 |
\ifluatex |
| 19 |
\else |
| 20 |
\PackageError{spelling}{LuaTeX engine required}{You could try with the |
| 21 |
`lualatex' command.} |
| 22 |
\fi |
| 23 |
% Lua module version management. |
| 24 |
\RequirePackage{luatexbase-modutils} |
| 25 |
% LuaTeX call-back management. |
| 26 |
\RequirePackage{luatexbase-mcb} |
| 27 |
% User-defined whatsit node ID allocation. |
| 28 |
\RequirePackage{luatexbase-attr} |
| 29 |
% Load main Lua file. |
| 30 |
\directlua name {spelling}{ |
| 31 |
% The main Lua file is not a module, so we must pass a full path to |
| 32 |
% `dofile`. |
| 33 |
local f = kpse.find_file('spelling-main.lua', 'lua') |
| 34 |
dofile(f) |
| 35 |
} |
| 36 |
% Words are extracted after pages have been built. In lack of a proper |
| 37 |
% Lua call-back, we're hooking into shipout from the LaTeX side. |
| 38 |
\RequirePackage{atbegshi} |
| 39 |
\AtBeginShipout{% |
| 40 |
\directlua name {spelling-atbeginshipout}{ |
| 41 |
PKG_spelling.cb_AtBeginShipout(\the\AtBeginShipoutBox) |
| 42 |
}% |
| 43 |
} |
| 44 |
% Provide command for reading-in a list of bad spellings. |
| 45 |
\newcommand*{\spellingreadbad}[1]{% |
| 46 |
\directlua{ |
| 47 |
PKG_spelling.stage[1].parse_bad_plain_list_file('\luaescapestring{#1}') |
| 48 |
}% |
| 49 |
} |
| 50 |
% Provide command for reading-in a list of good spellings. |
| 51 |
\newcommand*{\spellingreadgood}[1]{% |
| 52 |
\directlua{ |
| 53 |
PKG_spelling.stage[1].parse_good_plain_list_file('\luaescapestring{#1}') |
| 54 |
}% |
| 55 |
} |
| 56 |
% Provide command for reading bad spellings from a LanguageTool XML |
| 57 |
% file. |
| 58 |
\newcommand*{\spellingreadLT}[1]{% |
| 59 |
\directlua{ |
| 60 |
PKG_spelling.stage[1].parse_XML_LanguageTool_file('\luaescapestring{#1}') |
| 61 |
}% |
| 62 |
} |
| 63 |
% Provide command for reading match rules from a file. Argument must be |
| 64 |
% a file name. |
| 65 |
\newcommand*{\spellingmatchrules}[1]{% |
| 66 |
\directlua{ |
| 67 |
PKG_spelling.stage[1].read_match_rules('\luaescapestring{#1}') |
| 68 |
}% |
| 69 |
} |
| 70 |
% Provide command for enabling/disabling visual feedback. |
| 71 |
\newcommand*{\spellinghighlight}[1]{% |
| 72 |
\directlua{ |
| 73 |
if '\luaescapestring{#1}' == 'on' then |
| 74 |
PKG_spelling.stage[2].enable_word_highlighting() |
| 75 |
else |
| 76 |
PKG_spelling.stage[2].disable_word_highlighting() |
| 77 |
end |
| 78 |
}% |
| 79 |
} |
| 80 |
% Provide command for setting visual feedback colour. |
| 81 |
\newcommand*{\spellinghighlightcolor}[1]{% |
| 82 |
\directlua{ |
| 83 |
PKG_spelling.stage[2].set_highlight_color('\luaescapestring{#1}') |
| 84 |
}% |
| 85 |
} |
| 86 |
% Provide command for enabling/disabling text ouput. |
| 87 |
\newcommand*{\spellingoutput}[1]{% |
| 88 |
\directlua{ |
| 89 |
if '\luaescapestring{#1}' == 'on' then |
| 90 |
PKG_spelling.stage[4].enable_text_output() |
| 91 |
else |
| 92 |
PKG_spelling.stage[4].disable_text_output() |
| 93 |
end |
| 94 |
}% |
| 95 |
} |
| 96 |
% Provide command for setting text output file name. |
| 97 |
\newcommand*{\spellingoutputname}[1]{% |
| 98 |
\directlua{ |
| 99 |
PKG_spelling.stage[4].set_output_file_name('\luaescapestring{#1}') |
| 100 |
}% |
| 101 |
} |
| 102 |
% Provide command for setting text output file line length. |
| 103 |
\newcommand*{\spellingoutputlinelength}[1]{% |
| 104 |
\directlua{ |
| 105 |
PKG_spelling.stage[4].set_output_line_length(\luaescapestring{#1}) |
| 106 |
}% |
| 107 |
} |
| 108 |
% Provide command for enabling/disabling text extraction. |
| 109 |
\newcommand*{\spellingextract}[1]{% |
| 110 |
\directlua{ |
| 111 |
if '\luaescapestring{#1}' == 'on' then |
| 112 |
PKG_spelling.stage[2].enable_text_tagging() |
| 113 |
else |
| 114 |
PKG_spelling.stage[2].disable_text_tagging() |
| 115 |
end |
| 116 |
}% |
| 117 |
} |
| 118 |
% Provide command to declare code point mappings. |
| 119 |
\newcommand*{\spellingmapping}[2]{% |
| 120 |
\directlua{ |
| 121 |
local r = PKG_spelling.stage[2].set_mapping(\luaescapestring{#1}, '\luaescapestring{#2}') |
| 122 |
if r == false then |
| 123 |
texio.write_nl('package spelling: invalid mapping: \luaescapestring{#1} => \luaescapestring{#2}') |
| 124 |
end |
| 125 |
}% |
| 126 |
} |
| 127 |
% Provide command to clear all existing code point mappings. |
| 128 |
\newcommand*{\spellingclearallmappings}{% |
| 129 |
\directlua{ |
| 130 |
PKG_spelling.stage[2].clear_all_mappings() |
| 131 |
}% |
| 132 |
} |
| 133 |
% Provide command to specify table paragraph behaviour. |
| 134 |
\newcommand*{\spellingtablepar}[1]{% |
| 135 |
\directlua{ |
| 136 |
PKG_spelling.stage[3].set_table_paragraphs(\luaescapestring{#1}) |
| 137 |
}% |
| 138 |
} |
| 139 |
% |
| 140 |
% Read bad and good spellings from default sources. |
| 141 |
\directlua{ |
| 142 |
PKG_spelling.stage[1].parse_default_bad_and_good() |
| 143 |
}% |
| 144 |
% |
| 145 |
% Enable visual feedback. |
| 146 |
\spellinghighlight{on} |
| 147 |
% Enable text ouput at the end of the LuaTeX run. |
| 148 |
\spellingoutput{on} |
| 149 |
% Enable text extraction. |
| 150 |
\spellingextract{on} |