Unwrapping a Markdown table with a Pandoc Lua filter - lua

I have a DOCX file with this content:
# Heading
+---------------------+
| Paragraph |
| |
| ## Subheading |
| |
| +-----------------+ |
| | Nested table | |
| +-----------------+ |
+---------------------+
One last paragraph
Here is a sample file.
I want to run it through Pandoc and get this Markdown, with all tables unwrapped:
# Heading
Paragraph
## Subheading
Nested table
One last paragraph
I'm trying to write a Lua filter with walk_block but I have no experience with Lua and not making any progress. Can anyone point me in a helpful direction?
function Table(table)
pandoc.walk_block(table, {
Str = function(el)
-- TODO now what???
end
})
end

The Lua interface to tables is currently rather complex, so it's much simpler to convert the table into a so-called simple table. We can do so with pandoc.utils.to_simple_table. A simple table has a header row (header) and multiple body rows (rows), and we get access to cells by iterating over a row. Each cell is just a Blocks list, which we can collect in an accumulator.
Here's how this looks like:
function Table (tbl)
local simpleTable = pandoc.utils.to_simple_table(tbl)
local blocks = pandoc.Blocks{}
for _, headercell in ipairs(simpleTable.header) do
blocks:extend(headercell)
end
for _, row in ipairs(simpleTable.rows) do
for _, cell in ipairs(row) do
blocks:extend(cell)
end
end
return blocks, false
end
Running that filter should unwrap all tables, leaving just their contents.

Related

How to insert hard line break in Quarto document

I'm working with the Quarto document preparation system to programmatically generate large documents using Python. That is I am not using the RStudio or similar editor. All text must be generated as Python strings.
I have a simple table where one cell has several lines worth of content and I figure that I can add line-breaks within the cell. I understand that simple tables cannot include standard line breaks (\n) but I'm wondering if there is a way to insert so-called "hard-line-breaks" in cells. The user manual mentions them but states that only the editor can insert them. Is it possible to do this using string operations in the markdown source text?
Barring that is there a simple way to break lines within a cell in a table?
Quarto tables generated by RStudio visual Markdown editor
There's not much magic involved when it comes to hard line breaks, just lines that end with a \.
QMD file created in RStudio with visual mode enabled, a (Grid) table with both regular and hard line breaks looks like this:
---
title: "tbl"
format: html
editor: visual
---
+-------+-------------+
| Col1 | Col2 |
+=======+=============+
| press | press\ |
| | shift+enter |
| enter | |
+-------+-------------+
| | |
+-------+-------------+
With all the whitespace and linefeeds next to rendered output:
While RStudio visual editor starts with simple pipe tables, it switches automatically to grid tables when it encounters a line break or anything else in a cell that's not supported by simple pipe tables.
Python and grid-style Markdown tables
For generating Markdown grid tables in Python, tabulate with tablefmt="grid" is quite handy. Or pandas.DataFrame.to_markdown() in case of pandas, also built on top of tabulate.
from tabulate import tabulate
td = [["press\n\nenter","press\\\nshift+enter"],["no\nbackslash","",]]
hdr = ["Col1", "Col2"]
print(tabulate(td,headers=hdr, tablefmt="grid"))
Result:
+-----------+-------------+
| Col1 | Col2 |
+===========+=============+
| press | press\ |
| | shift+enter |
| enter | |
+-----------+-------------+
| no | |
| backslash | |
+-----------+-------------+
Rendered with Quarto:
Pandoc multiline tables
For whatever reason this is not mentioned in Quarto docs, but Pandoc also supports multiline_tables and rendering it with Quarto & Pandoc that are bundled with RStudio works fine, though for hard line breaks it still needs \ before a break.
Slighty modified sample from Pandoc documentation:
-------------------------------------------------------------
Centered Default Right Left
Header Aligned Aligned Aligned
----------- ------- --------------- -------------------------
First row 12.0 Example of\
a row that
spans multiple lines.
Second row 5.0 Here's another one. Note
the blank line between
rows.
-------------------------------------------------------------
Table: Here's the caption. It, too, may span\
multiple lines.
Renders as:

Select query in Ruby on Rails

I am new to ruby on rails but I am not finding the meaning of this line of code. I saw in the documentation of rails that select will build an array of objects from the database for the scope, converting them into an array and iterating through them using Array#select. Anyway I can’t understand the result of this line of code and on what it consists.
model.legal_storages.select{|storage| storage.send(document_type)==true}.last
model.legal_storages.select { |storage| storage.send(document_type) == true }.last - From the result of the last operation, select only the last element.
| | |
| | --------------------- For each element in model.legal_stores invoke
| | the method that is held by the variable document_type
| | and check if it's equal to true.
| |
| --------- Over the result of the last method,
| call select to filter those elements where
| condition in the block evaluates to true.
|
------------------- Invoke the method legal_stores in model.

How to shift table in markdown file?

I have a table in README.md file in TFS like this:
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
It's work. But I need to shift this table to the right by tab character. And if I place tab (or space character) before table rows it's not recognize as table and just print as plain text. HTML tag also doesn't work in TFS.
How I can move table?
Unfortunately shift table is not supported in Markdown.
Markdown does not provide any special syntax for tables.
Please see Syntax guidance for Markdown files, widgets, wikis, and pull request comments for details.
I can think of is that you can capture a screenshot for the table, render the captured image, check in the image, then Add image to your Markdown file.
But please note that just as Waylan mention in below comments : For screenshot of a table, the text will no longer be searchable and cannot be copy and pasted by the user. And any future edits will require recreating the entire table.

How to shorten SPSS code for comparing multiple ICD codes to multiple diagnosis column

This code selects cases which selects range of diagnosis (here range being 7567 to 75679) against multiple columns which stores the diagnosis codes (here DX1, DX2, DX3....etc.). Following works but it is pain to change the code every time when I am looking for new range of diagnosis. How do I shorten the code?
IF (RANGE(DX1,'7567','75679') | RANGE(DX2,'7567','75679') | RANGE(DX3,'7567','75679') | RANGE(DX4,'7567','75679') | RANGE(DX5,'7567','75679') | RANGE(DX6,'7567','75679') | RANGE(DX7,'7567','75679') |RANGE(DX8,'7567','75679')
| RANGE(DX9,'7567','75679') | RANGE(DX10,'7567','75679') | RANGE(DX11,'7567','75679') | RANGE(DX12,'7567','75679') | RANGE(DX13,'7567','75679') | RANGE(DX14,'7567','75679') | RANGE(DX15,'7567','75679')
| RANGE(DX16,'7567','75679') | RANGE(DX17,'7567','75679') | RANGE(DX18,'7567','75679') | RANGE(DX19,'7567','75679') | RANGE(DX20,'7567','75679') | RANGE(DX21,'7567','75679') | RANGE(DX22,'7567','75679') | RANGE(DX23,'7567','75679')
| RANGE(DX24,'7567','75679') | RANGE(DX25,'7567','75679'))ABDWALDEF=1.
EXECUTE.
count ABDWALDEF= DX1 to DX25 ('7567' thru'75679').
exe.
if ABDWALDEF>1 ABDWALDEF=1.
exe.
Personally, I would not reccomend string ranges in SPSS. You just need to make sure you know what you are doing, because string ranges are different from numerical ranges:
Take for example code 756780:
if DXs are numbers. it will not fit into the (7567, 75679) range, because 756780>75679.
if DXs are strings, it will fit into the same range of strings, because strings are sorted based on the first character, then the second, and so on. Fist 4 characters are identical, and in 5th position there is "9">"8". So in strings, "75679">"756780". Therefore, 756780 would be part of your range
As #eli-k mentioned in the comment, and you are really sure you want to work with numerica ranges, and not string ranges:
if all the codes are really numbers in text format, you might as well
change them to numbers and everything gets easier:
alter type DX1 to DXn (f10).

cannot find "for loop" keyword in robot framework

I am currently connecting SQL server to robot framework, so i can read my data table name in robot. and I want to use for loop to check table name, somehow, ":FOR" loop keyword cannot found, but I have installed libraries such as operating-system, collections, string, built-in, diff-library and so on. anyone can help me why i cannot use for loop? any help will be appreciated.
The robot framework users guide has a whole section on how to use the for loop. From that section:
The syntax starts with :FOR, where colon is required to separate the
syntax from normal keywords. The next cell contains the loop variable,
the subsequent cell must have IN, and the final cells contain values
over which to iterate. These values can contain variables, including
list variables.
Here's an example from the user's guide, reformatted to use pipes (for clarity):
*** Test Cases ***
| Example 1
| | :FOR | ${animal} | IN | cat | dog
| | | log | ${animal}
| | | log | 2nd keyword
| | Log | Outside loop
Maybe you are not escaping indented cells; as the Tip in the documentation says. Try writing loops like this:
:FOR ${index} IN RANGE ${start} ${stop}
\ log to console index: ${index}
\ Call a Keyword

Resources