Skip to content
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

Markdown tables to LibreOffice tables #32

Open
RomainTT opened this issue Nov 7, 2016 · 8 comments
Open

Markdown tables to LibreOffice tables #32

RomainTT opened this issue Nov 7, 2016 · 8 comments

Comments

@RomainTT
Copy link

RomainTT commented Nov 7, 2016

Hello (me again!)

Current markdown filter does not convert markdown tables. I tried to modify the code myself but without success...


There are 4 main steps to achieve this if I understand correctly:

  • markdown function must be called with an additional argument: mardown(markdown_text, extras=['tables'])
  • table, tr, and td must be added to markdown_map.py, I think it looks like this:
    'table': {
		'replace_with': 'table:table',
		'attributes': {
			'table:name': 'Table' + str(randint(100000000000000000,900000000000000000)),
			'table:style-name': str(randint(100000000000000000,900000000000000000))
		}
	},
	'tr': {
		'replace_with': 'table:table-row',
		'attributes': {
			'table:style-name': str(randint(100000000000000000,900000000000000000))
		}
	},
	'td': {
		'replace_with': 'table:table-cell',
		'attributes': {
			'table:style-name': str(randint(100000000000000000,900000000000000000)),
			'office:value-type': "string"
		}
	}
  • Automatically add text:p child to td elements : the exact same thing that is already done for li tag, line 728 in secretary.py
  • One thing I don't really know how to do it precisely: count the number of columns in the table to add the <table:table-column table:style-name="TestListTable.A"/> lines in the table header.
@christopher-ramirez
Copy link
Owner

Hello again!

A possible solution would be to count the td's inside a tr. That count would be the number of columns for the current row. As I know, column count is done by row, not by table as one may think. Correct me if I'm wrong.

@RomainTT
Copy link
Author

RomainTT commented Dec 8, 2016

Hey!
I've been working on this matter for a while, but I still can't get a correct result (but I'm very close!).

To answer your question, I think column count is done by table because column tags are children of a table tag, and common for every rows. But maybe I don't understand correctly.

I want to share with you a result that I have, because I'm currently stuck on a problem: only the first column of my tables appear in the final .odt document.

In the following files, you will find:

  • markdown_text.txt -> the markdown text which is given as an argument for the template
  • temporary.txt -> the output of self.files['content.xml'] in the render function of secretary
  • output.txt -> the generated document in xml format (I exported the .odt into .fodt format)

Opening temporary.txt and output.txt, we can see that only the second column is finally kept while the table seems to be complete in the variable self.files['content.xml']

Maybe you will see something I am missing in self.files['content.xml'], a missing tag or attribute... I don't know.

markdown_text.txt
temporary.txt
output.txt

edit: here is a link to the xml standard description of tables.

@RomainTT
Copy link
Author

The only difference I note is that my column tags are at the end of the table content, below definition of rows. I thought tags order was not important in XML, but it appears that order could be interpreted. I saw this in Frame description:

Each child element of a frame is a different representation of the same content. The order of content elements reflects the document author's preference for rendering, with the first child element being preferred.

I'll try to put column description at the beginning of the table content, before rows description.

@christopher-ramirez
Copy link
Owner

I would like to see the code you wrote for handling markdown tables.

@RomainTT
Copy link
Author

It finally works !
I was right, tags order is important for LibreOffice, and column tags must be written before rows tags.

As I'm a noob with Github, the best way I found for you to see my code is to fork your repo and push my commits on my own repo, you can find it there: https://github.com/RomainTT/secretary

Important note: as I did not know if style creation was important or not, I manually created a specific style for each table, column and cell. But actually it does not matter, I tried to remove any style creation and it still worked.

@christopher-ramirez
Copy link
Owner

Awesome! I was checking your code and noticed you also included image support in the markdown filter. That's great! I must confess I have my reserves because it relieves on PIL, which in some scenarios can be problematic.

I'm now thinking in how keeping the image support avoiding the issues with PIL.

Thank you for this job!

@RomainTT
Copy link
Author

I'm glad you like it.

I don't know what kind of issues we can have with PIL, I just discovered this module. If you can get something better that's great.

The image management is far from perfection right now:

  • it only works with local pictures, and if a relative path is given, this path must work from the script execution location (So if the markdown text is extracted from another location, the relative path will probably not work...).
  • Image size is very hard to handle. I searched for a long time but did not find the perfect solution. I wanted the image to fit the maximum width of its container, and respect width/height original proportion. But it seems to be impossible and I must give a width size manually. I set up a default width of 10cm because it is a good size to get a clear image ready for manual resizing in LibreOffice.

One good point however is that you can have files with or without extension (.jpg, etc.) it will always work.

@christopher-ramirez
Copy link
Owner

I held a previous discussion on image support on PR #24. Have a look into that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants