Skip to content

Suggestion: Adding nonbreakablespace lua filter #114

Open
@Delanii

Description

@Delanii

Hello mr. Tarleb,

with your help, I have finished writing and testing filter that introduces non-breakable space before or after specific strings. If I would prepare informative README.md and add makefile and data to perform tests, would you be interested in adding this filter to this repository?

I tryed to follow the lua-code style recommendations and also added comments that should clarify enough what I am doing (wanting to do).

Next goes final code of the filter:

--[[
Indexed table of one-letter prefixes, after which should be inserted '\160'.
Verbose, but can be changed per user requirements.
--]]

local prefixes = {
  'a',
  'i',
  'k',
  'o',
  's',
  'u',
  'v',
  'z',
  'A',
  'I',
  'K',
  'O',
  'S',
  'U',
  'V',
  'Z'
}

--[[
Some languages (czech among them) require nonbreakable space *before* long dash
--]]

local dashes = {
  '--',
  ''
}

--[[
Function responsible for searching for one-letter prefixes, after which is 
inserted non-breakable space. Function is short-circuited, that means:

* If it finds match with `prefix` in `prefixes` table, then it returns `true`.
* Otherwise, after the iteration is finished, returns `false` (prefix wasnt 
found).
--]]

function findOneLetterPrefix(myString)
  for index, prefix in ipairs(prefixes) do
    if myString == prefix then
      return true
	end
  end
  return false
end

--[[
Function responsible for searching for dashes, before whose is inserted 
non-breakable space. Function is short-circuited, that means:

* If it finds match with `dash` in `dashes` table, then it returns `true`.
* Otherwise, after the iteration is finished, returns `false` (dash wasnt 
found).
--]]

function findDashes(myDash)
  for index, dash in ipairs(dashes) do
    if myDash == dash then
      return true
	end
  end
  return false
end

--[[
Core filter function:

* It iterates over all inline elements in block
* If it finds Space element, uses previously defined functions to find
`prefixes` or `dashes`
* Replaces Space element with `Str '\u{a0}'`, which is non-breakable space 
representation
* Returns modified list of inlines
--]]

function Inlines (inlines)
  for i = 1, #inlines do
    if inlines[i].t == 'Space' then
	  
	  -- Check for one-letter prefixes in Str before Space
	  
      if inlines[i - 1].t == 'Str' then
	    local oneLetterPrefix = findOneLetterPrefix(inlines[i - 1].c)
		if oneLetterPrefix == true then
--		  inlines[i] = pandoc.Str '\xc2\xa0' -- Both work
          inlines[i] = pandoc.Str '\u{a0}'
		end
	  end
	  
	  -- Check for dashes in Str after Space
	  
	  if inlines[i + 1].t == 'Str' then
	    local dash = findDashes(inlines[i + 1].c)
		if dash == true then
		  inlines[i] = pandoc.Str '\u{a0}'
		end
	  end
	  
	  -- Check for not fully parsed Str elements - Those might be products of 
	  -- other filters, that were executed before this one
	  
	  if inlines[i + 1].t == 'Str' then
	    if string.match(inlines[i + 1].c, '%.*%s*[„]?%d+[“]?%s*%.*') then
		  inlines[i] = pandoc.Str '\u{a0}'
		end
	  end
	  
	end

	--[[
	Check for Str containing sequence " prefix ", which might occur in case of
	preceding filter creates it in one Str element. Also check, if quotation
	mark is present introduced by "quotation.lua" filter
	--]]
	
	if inlines[i].t == 'Str' then
	  for index, prefix in ipairs(prefixes) do
	    if string.match(inlines[i].c, '%.*%s+[„]?' .. prefix .. '[“]?%s+%.*') then
		  front,detection, replacement, back = string.match(inlines[i].c, '(%.*)(%s+[„]?' .. prefix .. '[“]?)(%s+)(%.*)')
		  inlines[i].c = front .. detection .. '\u{a0}' .. back
		end
	  end
	end
	
  end
  return inlines
end

Looking forward to you reply.

Regards, Tomas

Metadata

Metadata

Assignees

No one assigned

    Labels

    new filterRequest to develop/include a new filter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions