a python script for the table of contents

Previous topic - Next topic

a.l.e

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


utnik

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

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

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

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

#6
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

a.l.e

indeed, linked frame were not detected (correctly).

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

a.l.e

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

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

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

oh - sorry!

here is the attachment...

a.l.e

it worked here also with your document.

both with 1.6 and 1.5.8...

mmm....

utnik

#13
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

a.l.e

#14


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: