From a9131dedd6b40d5e62576f288b41b14b35866f7a Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Sun, 30 Jun 2024 22:06:04 +0100 Subject: [PATCH] Add the text version of the lua example notebook Plus, remove the final space in markdown cells that is not stable in round trips with MyST notebooks --- CHANGELOG.md | 7 ++ src/jupytext/version.py | 2 +- .../inputs/ipynb_lua/lua_example.ipynb | 12 ++-- .../outputs/ipynb_to_Rmd/lua_example.Rmd | 64 ++++++++++++++++++ .../outputs/ipynb_to_hydrogen/lua_example.lua | 65 ++++++++++++++++++ .../outputs/ipynb_to_md/lua_example.md | 64 ++++++++++++++++++ .../outputs/ipynb_to_myst/lua_example.md | 67 +++++++++++++++++++ .../outputs/ipynb_to_percent/lua_example.lua | 65 ++++++++++++++++++ .../outputs/ipynb_to_script/lua_example.lua | 53 +++++++++++++++ 9 files changed, 392 insertions(+), 7 deletions(-) create mode 100644 tests/data/notebooks/outputs/ipynb_to_Rmd/lua_example.Rmd create mode 100644 tests/data/notebooks/outputs/ipynb_to_hydrogen/lua_example.lua create mode 100644 tests/data/notebooks/outputs/ipynb_to_md/lua_example.md create mode 100644 tests/data/notebooks/outputs/ipynb_to_myst/lua_example.md create mode 100644 tests/data/notebooks/outputs/ipynb_to_percent/lua_example.lua create mode 100644 tests/data/notebooks/outputs/ipynb_to_script/lua_example.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index c147ce44..8bfb6826 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Jupytext ChangeLog ================== +1.16.3-dev (2024-07-??) +------------------- + +**Added** +- Added support for Lua notebooks ([#1252](https://github.com/mwouts/jupytext/pull/1252)) - thanks to [erentar](https://github.com/erentar) for this contribution + + 1.16.2 (2024-05-05) ------------------- diff --git a/src/jupytext/version.py b/src/jupytext/version.py index 8c85728e..1c37abd8 100644 --- a/src/jupytext/version.py +++ b/src/jupytext/version.py @@ -1,3 +1,3 @@ """Jupytext's version number""" -__version__ = "1.16.2" +__version__ = "1.16.3-dev" diff --git a/tests/data/notebooks/inputs/ipynb_lua/lua_example.ipynb b/tests/data/notebooks/inputs/ipynb_lua/lua_example.ipynb index 3ec0c6d5..02dc8be8 100644 --- a/tests/data/notebooks/inputs/ipynb_lua/lua_example.ipynb +++ b/tests/data/notebooks/inputs/ipynb_lua/lua_example.ipynb @@ -18,14 +18,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator). " + "Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this: " + "A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this:" ] }, { @@ -45,7 +45,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - " Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result: " + " Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result:" ] }, { @@ -74,14 +74,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table. " + "Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table: " + "As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table:" ] }, { @@ -109,7 +109,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - " With this function, it is easy to print those function names in alphabetical order. The loop " + " With this function, it is easy to print those function names in alphabetical order. The loop" ] }, { diff --git a/tests/data/notebooks/outputs/ipynb_to_Rmd/lua_example.Rmd b/tests/data/notebooks/outputs/ipynb_to_Rmd/lua_example.Rmd new file mode 100644 index 00000000..7f379822 --- /dev/null +++ b/tests/data/notebooks/outputs/ipynb_to_Rmd/lua_example.Rmd @@ -0,0 +1,64 @@ +--- +jupyter: + kernelspec: + display_name: Lua + language: lua + name: lua +--- + +Source: https://www.lua.org/pil/19.3.html + + +# Sort + + +Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator). + + +A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this: + +```{lua} +lines = { + luaH_set = 10, + luaH_get = 24, + luaH_present = 48, +} +``` + + Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result: + +```{lua} +a = {} +for n in pairs(lines) do table.insert(a, n) end +table.sort(a) +for i,n in ipairs(a) do print(n) end +``` + +Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table. + + +As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table: + +```{lua} +function pairsByKeys (t, f) + local a = {} + for n in pairs(t) do table.insert(a, n) end + table.sort(a, f) + local i = 0 -- iterator variable + local iter = function () -- iterator function + i = i + 1 + if a[i] == nil then return nil + else return a[i], t[a[i]] + end + end + return iter +end +``` + + With this function, it is easy to print those function names in alphabetical order. The loop + +```{lua} +for name, line in pairsByKeys(lines) do + print(name, line) +end +``` diff --git a/tests/data/notebooks/outputs/ipynb_to_hydrogen/lua_example.lua b/tests/data/notebooks/outputs/ipynb_to_hydrogen/lua_example.lua new file mode 100644 index 00000000..5d319fef --- /dev/null +++ b/tests/data/notebooks/outputs/ipynb_to_hydrogen/lua_example.lua @@ -0,0 +1,65 @@ +-- -*- coding: utf-8 -*- +-- --- +-- jupyter: +-- kernelspec: +-- display_name: Lua +-- language: lua +-- name: lua +-- --- + +-- %% [markdown] +-- Source: https://www.lua.org/pil/19.3.html + +-- %% [markdown] +-- # Sort + +-- %% [markdown] +-- Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator). + +-- %% [markdown] +-- A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this: + +-- %% +lines = { + luaH_set = 10, + luaH_get = 24, + luaH_present = 48, +} + +-- %% [markdown] +-- Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result: + +-- %% +a = {} +for n in pairs(lines) do table.insert(a, n) end +table.sort(a) +for i,n in ipairs(a) do print(n) end + +-- %% [markdown] +-- Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table. + +-- %% [markdown] +-- As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table: + +-- %% +function pairsByKeys (t, f) + local a = {} + for n in pairs(t) do table.insert(a, n) end + table.sort(a, f) + local i = 0 -- iterator variable + local iter = function () -- iterator function + i = i + 1 + if a[i] == nil then return nil + else return a[i], t[a[i]] + end + end + return iter +end + +-- %% [markdown] +-- With this function, it is easy to print those function names in alphabetical order. The loop + +-- %% +for name, line in pairsByKeys(lines) do + print(name, line) +end diff --git a/tests/data/notebooks/outputs/ipynb_to_md/lua_example.md b/tests/data/notebooks/outputs/ipynb_to_md/lua_example.md new file mode 100644 index 00000000..29a71d0a --- /dev/null +++ b/tests/data/notebooks/outputs/ipynb_to_md/lua_example.md @@ -0,0 +1,64 @@ +--- +jupyter: + kernelspec: + display_name: Lua + language: lua + name: lua +--- + +Source: https://www.lua.org/pil/19.3.html + + +# Sort + + +Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator). + + +A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this: + +```lua +lines = { + luaH_set = 10, + luaH_get = 24, + luaH_present = 48, +} +``` + + Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result: + +```lua +a = {} +for n in pairs(lines) do table.insert(a, n) end +table.sort(a) +for i,n in ipairs(a) do print(n) end +``` + +Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table. + + +As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table: + +```lua +function pairsByKeys (t, f) + local a = {} + for n in pairs(t) do table.insert(a, n) end + table.sort(a, f) + local i = 0 -- iterator variable + local iter = function () -- iterator function + i = i + 1 + if a[i] == nil then return nil + else return a[i], t[a[i]] + end + end + return iter +end +``` + + With this function, it is easy to print those function names in alphabetical order. The loop + +```lua +for name, line in pairsByKeys(lines) do + print(name, line) +end +``` diff --git a/tests/data/notebooks/outputs/ipynb_to_myst/lua_example.md b/tests/data/notebooks/outputs/ipynb_to_myst/lua_example.md new file mode 100644 index 00000000..ec6893c2 --- /dev/null +++ b/tests/data/notebooks/outputs/ipynb_to_myst/lua_example.md @@ -0,0 +1,67 @@ +--- +kernelspec: + display_name: Lua + language: lua + name: lua +--- + +Source: https://www.lua.org/pil/19.3.html + ++++ + +# Sort + ++++ + +Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator). + ++++ + +A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this: + +```{code-cell} +lines = { + luaH_set = 10, + luaH_get = 24, + luaH_present = 48, +} +``` + + Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result: + +```{code-cell} +a = {} +for n in pairs(lines) do table.insert(a, n) end +table.sort(a) +for i,n in ipairs(a) do print(n) end +``` + +Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table. + ++++ + +As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table: + +```{code-cell} +function pairsByKeys (t, f) + local a = {} + for n in pairs(t) do table.insert(a, n) end + table.sort(a, f) + local i = 0 -- iterator variable + local iter = function () -- iterator function + i = i + 1 + if a[i] == nil then return nil + else return a[i], t[a[i]] + end + end + return iter +end +``` + + With this function, it is easy to print those function names in alphabetical order. The loop + +```{code-cell} +for name, line in pairsByKeys(lines) do + print(name, line) +end +``` diff --git a/tests/data/notebooks/outputs/ipynb_to_percent/lua_example.lua b/tests/data/notebooks/outputs/ipynb_to_percent/lua_example.lua new file mode 100644 index 00000000..5d319fef --- /dev/null +++ b/tests/data/notebooks/outputs/ipynb_to_percent/lua_example.lua @@ -0,0 +1,65 @@ +-- -*- coding: utf-8 -*- +-- --- +-- jupyter: +-- kernelspec: +-- display_name: Lua +-- language: lua +-- name: lua +-- --- + +-- %% [markdown] +-- Source: https://www.lua.org/pil/19.3.html + +-- %% [markdown] +-- # Sort + +-- %% [markdown] +-- Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator). + +-- %% [markdown] +-- A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this: + +-- %% +lines = { + luaH_set = 10, + luaH_get = 24, + luaH_present = 48, +} + +-- %% [markdown] +-- Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result: + +-- %% +a = {} +for n in pairs(lines) do table.insert(a, n) end +table.sort(a) +for i,n in ipairs(a) do print(n) end + +-- %% [markdown] +-- Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table. + +-- %% [markdown] +-- As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table: + +-- %% +function pairsByKeys (t, f) + local a = {} + for n in pairs(t) do table.insert(a, n) end + table.sort(a, f) + local i = 0 -- iterator variable + local iter = function () -- iterator function + i = i + 1 + if a[i] == nil then return nil + else return a[i], t[a[i]] + end + end + return iter +end + +-- %% [markdown] +-- With this function, it is easy to print those function names in alphabetical order. The loop + +-- %% +for name, line in pairsByKeys(lines) do + print(name, line) +end diff --git a/tests/data/notebooks/outputs/ipynb_to_script/lua_example.lua b/tests/data/notebooks/outputs/ipynb_to_script/lua_example.lua new file mode 100644 index 00000000..b55e5a23 --- /dev/null +++ b/tests/data/notebooks/outputs/ipynb_to_script/lua_example.lua @@ -0,0 +1,53 @@ +-- -*- coding: utf-8 -*- +-- --- +-- jupyter: +-- kernelspec: +-- display_name: Lua +-- language: lua +-- name: lua +-- --- + +-- Source: https://www.lua.org/pil/19.3.html + +-- # Sort + +-- Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator). + +-- A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this: + +lines = { + luaH_set = 10, + luaH_get = 24, + luaH_present = 48, +} + +-- Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result: + +a = {} +for n in pairs(lines) do table.insert(a, n) end +table.sort(a) +for i,n in ipairs(a) do print(n) end + +-- Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table. + +-- As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table: + +function pairsByKeys (t, f) + local a = {} + for n in pairs(t) do table.insert(a, n) end + table.sort(a, f) + local i = 0 -- iterator variable + local iter = function () -- iterator function + i = i + 1 + if a[i] == nil then return nil + else return a[i], t[a[i]] + end + end + return iter +end + +-- With this function, it is easy to print those function names in alphabetical order. The loop + +for name, line in pairsByKeys(lines) do + print(name, line) +end