Scribusoid

Author Topic: a python script for the table of contents  (Read 319 times)

a.l.e

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2498
  • Karma: 40
    • the graphics lab
a python script for the table of contents
« on: January 22, 2023, 06:09:20 pm »
for years i have been telling people not to use the table of contents tool provided by scribus.

i even worked on a new table of contents tool, which was based on the paragraph styles used in the document.
(the proof of concept did work, but among other factors, the scribus team was not willing to replace the current tool with the new one i was proposing... and i was not so eager to make the UI even more complex with two different table of contents tools!)

two weeks ago, pmj proposed a patch to support multiple levels of headings in the current TOC tool:

https://bugs.scribus.net/view.php?id=16887

i suddenly thought that it might be possible to create a script that would fill the attributes by looking at the paragraph styles used  in the document. and make the TOC tool a little less painful.
i wrote the script and it did work well, with a better patch for multilevel TOC it would have made the TOC tool usable... but why should i have stopped there?

here is a python script that runs inside of scribus and collects the paragraphs with specific styles (h1, h2, h3... but you can easily modify the global variable to support any naming schema you see fit) and creates a multilevel table of contents in the current frame:

https://github.com/aoloe/scribus-script-repository/tree/master/table-of-contents

it should work with any current version of scribus (except 1.4.x of course! that's not current!).

i still really think that the current TOC tool should be removed from scribus and replaced by one that is based on styles, but at least, now, there is a solution for creating a TOC in Scribus!

it does not cover all possible table of contents, but i hope that it can allow people to create (a bit more than) basic table of contents with scribus!

PatJr

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
  • Scribus User
Re: a python script for the table of contents
« Reply #1 on: January 22, 2023, 07:02:40 pm »
Thanks for your hard work.

utnik

  • Hero Member
  • *****
  • Posts: 1689
  • Karma: 19
Re: a python script for the table of contents
« Reply #2 on: January 22, 2023, 08:49:23 pm »
hi ale

i downloaded your script. but a test with scribus 1.5.8 and 1.7.0 reverted this:

Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "<string>", line 60
    match section['format']:
          ^
SyntaxError: invalid syntax


utnik

a.l.e

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2498
  • Karma: 40
    • the graphics lab
Re: a python script for the table of contents
« Reply #3 on: January 23, 2023, 11:38:50 am »
hi utnik

ok, yes, that is what i feared.
that lines needs a current python version and scribus seems to distribute a somehow older one.
(it works on debian testing with the system python...)

i will modify those lines tonight and use a more conservative syntax.

thanks for testing!

a.l.e

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2498
  • Karma: 40
    • the graphics lab
Re: a python script for the table of contents
« Reply #4 on: January 23, 2023, 08:49:18 pm »
i have now removed the match / case that is invalid in the older versions of python.

... and fixed the README files (during the duplication of the table-of-contents script some of the edits went to the wrong folder...)

let's hope that the script now also works with the python version distributed with scribus : - )

utnik

  • Hero Member
  • *****
  • Posts: 1689
  • Karma: 19
Re: a python script for the table of contents
« Reply #5 on: January 24, 2023, 01:39:31 pm »
hi ale

i checked your new version with scribus 1.5.8 and it works flawlessly as long as the title is the first paragraph in a frame and there's only one title per frame and you don't use linked frames. (…or unlink them before the title…)

as i often work with the following syntax:

    'h1'-title
        'h2'-title
            text
        'h2'-title
            text

i checked this as well. it needs some manual work (deleting unneeded stuff) – but in general it creates a clean toc.

utnik

a.l.e

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2498
  • Karma: 40
    • the graphics lab
Re: a python script for the table of contents
« Reply #6 on: January 24, 2023, 03:51:42 pm »
hi utnik

thanks for testing

can you please provide a sample .sla file?

here it also detects multiple headings per frame

h1
p
h2
p

and i thought that it would work with linked frames, but -- indeed -- i have not tested it.

ciao
a.l.e

p.s.:

h1
h2
p
h2
p

also works
« Last Edit: January 24, 2023, 06:08:49 pm by a.l.e »

a.l.e

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2498
  • Karma: 40
    • the graphics lab
Re: a python script for the table of contents
« Reply #7 on: January 24, 2023, 07:07:52 pm »
indeed, linked frame were not detected (correctly).

i've now pushed a new version with support for linked frame : - )

a.l.e

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2498
  • Karma: 40
    • the graphics lab
Re: a python script for the table of contents
« Reply #8 on: January 25, 2023, 11:18:14 pm »
some more work...

- scribus is returning the frames in their order of creation... now, i'm sorting them by position before looking for the heading styles.
- if the heading and paragraph styles do not exist, they are automatically created
- if you want to use specific styles or create multiple table of contents with different styles, now you can define the styles to be used for a specific TOC, by setting the attributes of the frame (see the README file for some short instructions)

also, craig has added an API function for getting the "real" page number (formatted according to the settings in the section). if it works correctly and it has also been added to 1.6, then i will use it also for the TOC script (and remove the code faking the sections as soon as most people can have a scribus version with that function)

the current version of the script is (still) here:

https://github.com/aoloe/scribus-script-repository/tree/master/table-of-contents

utnik

  • Hero Member
  • *****
  • Posts: 1689
  • Karma: 19
Re: a python script for the table of contents
« Reply #9 on: January 27, 2023, 01:49:03 pm »
hi ale

i tested the latest version of your script vith scribus 1.5.8 on macos 11.7.2. (see attached file)
there are two strange things:
  • the whole toc is formatted as 'toc1' (instead of 'toc1', 'toc2' and 'toc3' according to 'h1', 'h2' and 'h3')
  • there is an empty line in the toc.
it's no big deal to maintain manually but there might be an easy fix…

thanks for your work
utnik

a.l.e

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2498
  • Karma: 40
    • the graphics lab
Re: a python script for the table of contents
« Reply #10 on: January 27, 2023, 02:51:31 pm »
hi utnik

i've patched the script to avoid the empty line at the end.
thanks for pointing to this "small" glitch : - )

on the other side, in my document, i seem to get the toc# styles to be correctly applied in the toc.
i'm using a somehow actual version of 1.6svn but it should not make a difference...

so, now, i wonder what is in the file you planned to attach : - )

ciao
a.l.e

utnik

  • Hero Member
  • *****
  • Posts: 1689
  • Karma: 19
Re: a python script for the table of contents
« Reply #11 on: January 27, 2023, 05:54:53 pm »
oh - sorry!

here is the attachment…

a.l.e

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2498
  • Karma: 40
    • the graphics lab
Re: a python script for the table of contents
« Reply #12 on: January 27, 2023, 11:50:26 pm »
it worked here also with your document.

both with 1.6 and 1.5.8...

mmm....

utnik

  • Hero Member
  • *****
  • Posts: 1689
  • Karma: 19
Re: a python script for the table of contents
« Reply #13 on: January 28, 2023, 02:01:03 am »
hi ale

there is something really strange: with scribus 1.5.8 i have to apply 'toc2' and 'toc3' manually after the toc is created. with scribus 1.7.0.svn (r25199) it looks the same in the first place. (the whole toc in 'toc1') but when i press 'cmd' + 'z' the styles are there as expected…

utnik

edit: after pressing 'cmd' + 'z' three times most of the toc reformats in 1.5.8 as well. but the last two entries in my test file switched to 'default paragraph style'.
i will test on an other mac to see if it is something unique on my machine…

utnik
« Last Edit: January 28, 2023, 02:07:21 am by utnik »

a.l.e

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2498
  • Karma: 40
    • the graphics lab
Re: a python script for the table of contents
« Reply #14 on: January 28, 2023, 11:11:30 am »


i wonder if the script is 100% correct.

how do you get it?

here is a way that should give you an exact copy of what is on github:

« Last Edit: January 28, 2023, 11:21:32 am by a.l.e »

 

palmate