christopher-ramirez / secretary

Take the power of Jinja2 templates to OpenOffice and LibreOffice.
Other
190 stars 48 forks source link

Markdown tables to LibreOffice tables #32

Open RomainTT opened 7 years ago

RomainTT commented 7 years ago

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:

    '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"
        }
    }
christopher-ramirez commented 7 years ago

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 commented 7 years ago

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:

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 commented 7 years ago

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 commented 7 years ago

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

RomainTT commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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:

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

christopher-ramirez commented 7 years ago

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