-
-
Notifications
You must be signed in to change notification settings - Fork 168
Adding non-breakable spaces lua filter #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Delanii
wants to merge
37
commits into
pandoc:master
Choose a base branch
from
Delanii:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 30 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
1c951b5
Create sample.md
Delanii ed6a50a
Add files via upload
Delanii dd2f12e
Add files via upload
Delanii 36c91bf
Update README.md
Delanii 7df6f7c
Rename sample.md to sampleCZ.md
Delanii 64bf968
Add files via upload
Delanii e167899
Rename expected.html to expectedCZ.html
Delanii 5e829a0
Update nonbreakablespace.lua
Delanii 6a62c16
Rename nonbreakablespace.lua to pandocVlna.lua
Delanii 6a8bc4f
Delete expectedCZ.html
Delanii d0bc724
Add files via upload
Delanii feec602
Update sampleCZ.md
Delanii 0cca88c
Add files via upload
Delanii ea8e13a
Update makefile
Delanii f78a1fa
Delete expectedCZ.html
Delanii 4b5d58a
New test file with fixes error causes by SoftBreak element
Delanii d5fce8b
Now testing also for SoftBreak element
Delanii 80a6900
Delete expectedEN.html
Delanii 25a7564
New test file for english
Delanii a4c6e81
Delete expectedEN.html
Delanii 5f13001
Another try for test file for english
Delanii f060207
Delete README.md
Delanii c02aa69
Delete expectedCZ.html
Delanii 22c7c82
Delete expectedEN.html
Delanii fa6a2b8
Delete makefile
Delanii 7008e81
Delete pandocVlna.lua
Delanii a675d44
Delete sampleCZ.md
Delanii 5ebf029
Delete sampleEN.md
Delanii bc67eb1
Reupload
Delanii 7de7a14
Add files via upload
Delanii 76b00e7
Update pandocVlna.lua
Delanii 11794ff
Delete pandocVlna.lua
Delanii 7536ba9
Add files via upload
Delanii dad0797
Rewrite or README.md: Default setting to English
Delanii 8f7501f
Update per suggestions in PR
Delanii ce063f3
Bugfix
Delanii d407e90
Bugfix if last element in par block is "Space"
Delanii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Non-breakable space filter | ||
|
|
||
| This filter replaces regular spaces with non-breakable spaces according to | ||
| predefined conditions. Currently, this filter replaces regular spaces with | ||
| unbreakable ones after one-letter words (prefixes and conjunctions): | ||
| 'a', 'i', 'k', 'o', 's', 'u', 'v', 'z'; and theyre uppercase variant. Also | ||
| inserts non-breakable spaces in front of en-dashes and in front of numbers. | ||
| Some extra effort is taken in detecting these patterns in *not-fully* parsed | ||
| strings (for example, if this filter is used after some macro replacing | ||
| filter). | ||
|
|
||
| In this regard this filter functions similarly like TeX `vlna` preprocessor | ||
| or LuaTeX `luavlna` package. | ||
|
|
||
| The default settings are conformant to Czech typography rules, but these can | ||
| be changed easily by user customization in filter file `nonbreakablespace.lua` | ||
| by changing contents of `prefixes` or `dashes` tables. | ||
|
|
||
| Currently supported formats are: | ||
|
|
||
| * LaTeX a ConTeXt | ||
| * Open Office Document | ||
| * MS Word | ||
| * HTML | ||
|
|
||
| **NOTE**: Using this filter increases strain on line-breaking patterns. Whenever | ||
| possible, consider allowing hyphenation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <h1 id="tests">Tests</h1> | ||
| <h2 id="basic-test">Basic test</h2> | ||
| <p>a test i test k test o test s test u test v test z test A test I test K test O test S test U test V test Z test – test – test</p> | ||
| <h2 id="test-with-numbers">Test with numbers</h2> | ||
| <p>Test 19 test “19” test</p> | ||
| <h2 id="test-of-double-prefixes.">Test of double prefixes.</h2> | ||
| <p>A i test, i v test, a k test, a v test.</p> | ||
| <h2 id="test-of-block-code">Test of block code</h2> | ||
| <pre><code>a = 5 | ||
| k = "test"</code></pre> | ||
| <h2 id="test-of-inline-code">Test of inline code</h2> | ||
| <p>Test <code>a = 5</code> test</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <h1 id="tests">Tests</h1> | ||
| <h2 id="basic-test">Basic test</h2> | ||
| <p>a test i test A test I test the test The test – test – test</p> | ||
| <h2 id="test-with-numbers">Test with numbers</h2> | ||
| <p>Test 19 test “19” test</p> | ||
| <h2 id="test-of-double-prefixes.">Test of double prefixes.</h2> | ||
| <p>A i test, i v test, a k test, a v test.</p> | ||
| <h2 id="test-of-block-code">Test of block code</h2> | ||
| <pre><code>a = 5 | ||
| k = "test"</code></pre> | ||
| <h2 id="test-of-inline-code">Test of inline code</h2> | ||
| <p>Test <code>a = 5</code> test</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| DIFF ?= diff --strip-trailing-cr -u | ||
|
|
||
| test: | ||
| @pandoc --lua-filter=pandocVlna.lua sampleCZ.md | $(DIFF) expectedCZ.html - | ||
| @pandoc --lua-filter=pandocVlna.lua sampleEN.md | $(DIFF) expectedEN.html - | ||
| .PHONY: test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| local utils = require 'pandoc.utils' | ||
| local stringify = utils.stringify | ||
|
|
||
| --[[ | ||
| Indexed table of one-letter prefixes, after which should be inserted '\160'. | ||
| Verbose, but can be changed per user requirements. | ||
| --]] | ||
|
|
||
| local prefixes = {} | ||
|
|
||
| local prefixesEN = { | ||
| 'I', | ||
| 'a', | ||
| 'A', | ||
| 'the', | ||
| 'The' | ||
| } | ||
|
|
||
| local prefixesCZ = { | ||
| 'a', | ||
| 'i', | ||
| 'k', | ||
| 'o', | ||
| 's', | ||
| 'u', | ||
| 'v', | ||
| 'z', | ||
| 'A', | ||
| 'I', | ||
| 'K', | ||
| 'O', | ||
| 'S', | ||
| 'U', | ||
| 'V', | ||
| 'Z' | ||
| } | ||
|
|
||
| -- Set `prefixes` according to `lang` metadata value | ||
| function Meta(meta) | ||
| if meta.lang then | ||
| langSet = stringify(meta.lang) | ||
|
|
||
| if langSet == 'cs' then | ||
| prefixes = prefixesCZ | ||
| else | ||
| prefixes = prefixesEN --default to english prefixes | ||
| end | ||
|
|
||
| else | ||
| prefixes = prefixesEN --default to english prefixes | ||
| end | ||
|
|
||
| return prefixes | ||
| end | ||
|
|
||
| --[[ | ||
| Some languages (czech among them) require nonbreakable space *before* long dash | ||
| --]] | ||
|
|
||
| local dashes = { | ||
| '--', | ||
| '–' | ||
| } | ||
|
|
||
| --[[ | ||
| Table of replacement elements | ||
| --]] | ||
|
|
||
| local nonbreakablespaces = { | ||
| html = ' ', | ||
| latex = '~', | ||
| context = '~' | ||
| } | ||
|
|
||
| --[[ | ||
| 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 find_one_letter_prefix(my_string) | ||
| for index, prefix in ipairs(prefixes) do | ||
| if my_string == 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 find_dashes(my_dash) | ||
|
Delanii marked this conversation as resolved.
Outdated
|
||
| for index, dash in ipairs(dashes) do | ||
| if my_dash == dash then | ||
| return true | ||
| end | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| --[[ | ||
| Function to determine Space element replacement for non-breakable space | ||
| --according to output format | ||
| --]] | ||
|
|
||
| function insert_nonbreakable_space(format) | ||
| if format == 'html' then | ||
| return pandoc.RawInline('html', nonbreakablespaces.html) | ||
| elseif format:match 'latex' then | ||
| return pandoc.RawInline('tex',nonbreakablespaces.latex) | ||
| elseif format:match 'context' then | ||
| return pandoc.RawInline('tex',nonbreakablespaces.latex) | ||
| else | ||
| --fallback to inserting non-breakable space unicode symbol | ||
| return pandoc.Str '\u{a0}' | ||
| end | ||
| 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) | ||
|
|
||
| --variable holding replacement value for the non-breakable space | ||
| local insert = insert_nonbreakable_space(FORMAT) | ||
|
Delanii marked this conversation as resolved.
Outdated
|
||
|
|
||
| for i = 1, #inlines do | ||
|
|
||
| --assign elements to variables for simpler code writing | ||
| local currentEl = inlines[i] | ||
| local previousEl = inlines[i-1] | ||
| local nextEl = inlines[i+1] | ||
|
|
||
| if currentEl.t == 'Space' | ||
| or currentEl.t == 'SoftBreak' then | ||
|
|
||
| -- Check for one-letter prefixes in Str before Space | ||
|
|
||
| if previousEl.t == 'Str' then | ||
| local one_letter_prefix = find_one_letter_prefix(previousEl.text) | ||
| if one_letter_prefix == true then | ||
| -- inlines[i] = pandoc.Str '\xc2\xa0' -- Both work | ||
| inlines[i] = insert | ||
|
Delanii marked this conversation as resolved.
Outdated
|
||
| end | ||
| end | ||
|
|
||
| -- Check for dashes in Str after Space | ||
|
|
||
| if nextEl.t == 'Str' then | ||
| local dash = find_dashes(nextEl.text) | ||
|
|
||
| if dash == true then | ||
| inlines[i] = insert | ||
| end | ||
| end | ||
|
|
||
| -- Check for not fully parsed Str elements - Those might be products of | ||
| -- other filters, that were executed before this one | ||
|
|
||
| if nextEl.t == 'Str' then | ||
| if string.match(nextEl.text, '%.*%s*[„]?%d+[“]?%s*%.*') then | ||
| inlines[i] = insert | ||
| 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 currentEl.t == 'Str' then | ||
| for index, prefix in ipairs(prefixes) do | ||
| if string.match(currentEl.text, '%.*%s+[„]?' .. prefix .. '[“]?%s+%.*') then | ||
| front, detection, replacement, back = string.match(currentEl.text, | ||
| '(%.*)(%s+[„]?' .. prefix .. '[“]?)(%s+)(%.*)') | ||
|
|
||
| inlines[i].text = front .. detection .. insert .. back | ||
| end | ||
| end | ||
| end | ||
|
|
||
| end | ||
| return inlines | ||
| end | ||
|
|
||
| -- This should change the order of running functions: Meta - Inlines - rest ... | ||
| return { | ||
| {Meta = Meta}, | ||
| {Inlines = Inlines}, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --- | ||
| lang: cs | ||
| --- | ||
|
|
||
| # Tests | ||
|
|
||
| ## Basic test | ||
|
|
||
| a test i test k test o test s test u test v test z test A test I test K test O | ||
| test S test U test V test Z test -- test – test | ||
|
|
||
| ## Test with numbers | ||
|
|
||
| Test 19 test "19" test | ||
|
|
||
| ## Test of double prefixes. | ||
|
|
||
| A i test, i v test, a k test, a v test. | ||
|
|
||
| ## Test of block code | ||
|
|
||
| ``` | ||
| a = 5 | ||
| k = "test" | ||
| ``` | ||
|
|
||
| ## Test of inline code | ||
|
|
||
| Test `a = 5` test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Tests | ||
|
|
||
| ## Basic test | ||
|
|
||
| a test i test A test I test the test The test -- test – test | ||
|
|
||
| ## Test with numbers | ||
|
|
||
| Test 19 test "19" test | ||
|
|
||
| ## Test of double prefixes. | ||
|
|
||
| A i test, i v test, a k test, a v test. | ||
|
|
||
| ## Test of block code | ||
|
|
||
| ``` | ||
| a = 5 | ||
| k = "test" | ||
| ``` | ||
|
|
||
| ## Test of inline code | ||
|
|
||
| Test `a = 5` test |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.