New event plugin: Table of Contents for Articles

Creating and modifying plugins.
Slideshow Bob
Regular
Posts: 6
Joined: Sat Feb 07, 2009 11:01 pm

New event plugin: Table of Contents for Articles

Post by Slideshow Bob »

Hey there,

first post and I'd like to contribute a new event plugin I desperately needed for my blog. I couldn't find any solution on the net, so I created it on my own.

My plugin automagically creates a table of contents for blog entries based on header tags (h3 to h6). You can insert the table of contents anywhere in your article by typing the special tag [TOCx] where x is a number from 3 to 6 - indicating the desired header order number to index.

The event plugin is designed to play along with serendipity_event_entrysplit and is therefore able to reference the proper sub-page. However, this feature currently only works if the page break tag is manually put into the article text.

I hacked this thing together over night and it seems to be stable so far (at least in my blog).

The table of contents makes use of the CSS-class name "articletoc" for all generated HTML tags (div, ul, li), so changing its appearance to make it fit nicely into a template shouldn't be too difficult.

I hope you find it useful :-)

Thomas
Attachments
serendipity_event_articletoc.zip
Table of Contents for Articles
(3.33 KiB) Downloaded 319 times
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: New event plugin: Table of Contents for Articles

Post by garvinhicking »

Hi!

Great, many thanks for sharing your code!

Looking at the code, I suspect it should work just fine, however some ideas:

1. Since it already depends a lot on serendipity_event_Entrysplit, would you like to combine your code into the existing plugin and then I could submit your patch into the official plugin?

2. I see that it seems to work on the extended body, wouldn't it be nice to be able to also put the TOC immediately into the normal entry body?

3. You use preg_replace for a [TOCx] replacement; you could speed the loop up a bit, if you simply use str_replace?

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Slideshow Bob
Regular
Posts: 6
Joined: Sat Feb 07, 2009 11:01 pm

Re: New event plugin: Table of Contents for Articles

Post by Slideshow Bob »

Hi Garvin,

you are right about merging it with "entrysplit". I also have to admit that I felt a "little bit dirty" letting my new plugin redundantly do entrysplit's work, so merging both makes a lot of sense.

I'll give it a go in a few days, also considering your other suggestions, and I will post an updated entrysplit-plugin into this thread.

See you!

Thomas
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: New event plugin: Table of Contents for Articles

Post by garvinhicking »

Hi!

That's great news! Keep me posted. :-)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Slideshow Bob
Regular
Posts: 6
Joined: Sat Feb 07, 2009 11:01 pm

Re: New event plugin: Table of Contents for Articles

Post by Slideshow Bob »

Hi Garvin,

as promised, I've modified the entrysplit plugin considering your additional suggestions.
The TOC can now also be applied to the entry's body. However, indexing header tags is currently only applied to the extended text, since this is also the working scope of the original entrysplit plugin. I hope that this restriction is okay with you :-)

Thomas

EDIT: Woops... I noticed a bug when two entry bodies with a TOC are displayed on the entries list page. I'm going to fix it now and upload the updated version

EDIT 2: Fixed and uploaded - see attachment
Attachments
serendipity_event_entrysplit.zip
Modified entrysplit plugin (additional TOC functionality)
(10.22 KiB) Downloaded 335 times
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: New event plugin: Table of Contents for Articles

Post by garvinhicking »

Hi!

Phew. That's more changes I anticpated. But it seems to work fine in my tests, so I've committed it and will see what the feedback will be :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
bugrep
Regular
Posts: 74
Joined: Tue Feb 24, 2009 8:19 am
Contact:

Re: New event plugin: Table of Contents for Articles

Post by bugrep »

hello
im hijacking this thread, i dont know if this problem is related to the latest changes (toc).

i installed s9y today and before adding an entry i also installed a lot of plugins (those descriptions in spartacus could be more verbose or contain wiki links btw!).

when i view my empty blog is see this warnings:
Warning: Cannot use a scalar value as an array in /web/plugins/serendipity_event_entrysplit/serendipity_event_entrysplit.php on line 93

Warning: Cannot use a scalar value as an array in /web/plugins/serendipity_event_entrysplit/serendipity_event_entrysplit.php on line 94

i did not look at the code (php disgusts me actually ;) but i guess its an easy fix.
google finds two others blogs that contain this warnings... but they are only in googles cache and not on the current (no more empty) blogs

cheers!

edit1:
i noticed the same problem in the pdf plugin.
its not really a problem, but i think it should be corrected anyway :)
Warning: Cannot use a scalar value as an array in /web/plugins/serendipity_event_entrylastmodified/serendipity_event_entrylastmodified.php on line 100

Warning: Cannot use a scalar value as an array in /web/plugins/serendipity_event_entrylastmodified/serendipity_event_entrylastmodified.php on line 104
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: New event plugin: Table of Contents for Articles

Post by garvinhicking »

Hi!

could it be that you don't have any blog entry yet? At which place do you see the error, on the blog overview, or on a detailed blog entry page?

I'm sorry for the short descriptions...we really do need help to describe the plugins more in detail on the wiki.

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
bugrep
Regular
Posts: 74
Joined: Tue Feb 24, 2009 8:19 am
Contact:

Re: New event plugin: Table of Contents for Articles

Post by bugrep »

fast reply!

yes, there are were entries (should have made that clearer in the prev post).
after adding an entry those warnings are gone.
so its not really a big issue....

(at the moment im wrestling with the blogpdf plugin... doesnt output the corret content-type. neighter HTML2PDF nor fpdf. should i open another thread for that, if i cant solve it myself or abuse this thread further?)
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: New event plugin: Table of Contents for Articles

Post by garvinhicking »

Hi!
(at the moment im wrestling with the blogpdf plugin... doesnt output the corret content-type. neighter HTML2PDF nor fpdf. should i open another thread for that, if i cant solve it myself or abuse this thread further?)
You could tell us the URL to your blog, then I could check a few header-related things?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
bugrep
Regular
Posts: 74
Joined: Tue Feb 24, 2009 8:19 am
Contact:

Re: New event plugin: Table of Contents for Articles

Post by bugrep »

sure
http://blog.0wnz.at
the pdf generation seems to work with both options. but the header is wrong (tested in firefox and opera and confirmed by using fiddler).

im using a chrooted lighttpd with a very basic environment, so maybe php/the plugin is missing something, but doesnt tell (couldnt use spartacus before, because libnss_dns.so was not available).
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: New event plugin: Table of Contents for Articles

Post by garvinhicking »

Hi!

Hm, sadly I have no real-life experience with lightttpd. Is PHP running as CGI, FastCGI or what? When you write a test.php script with something like:

Code: Select all

<?php
header('Content-Type: application/pdf');
echo 'blabla'
Does that produce the right header? Because I believe it doesn't generally work on your isntall then?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
bugrep
Regular
Posts: 74
Joined: Tue Feb 24, 2009 8:19 am
Contact:

Re: New event plugin: Table of Contents for Articles

Post by bugrep »

php uses fast-cgi there.

i tried the proposed test script on the command line (inside the chroot) and served via lighty and both ways work ok.

"php5-cgi pdf.php
X-Powered-By: PHP/5.2.6-2+b1
Content-Type: application/pdf

blabla"
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: New event plugin: Table of Contents for Articles

Post by garvinhicking »

Hi!

I've checked the fpdf.php library. It only sents headers when $HTTP_SERVER_VARS['SERVER_NAME'] is set. Usually, for Apache this is a mandatory variable, so I can only guess it's not getting set in lighty/CGI?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
bugrep
Regular
Posts: 74
Joined: Tue Feb 24, 2009 8:19 am
Contact:

Re: New event plugin: Table of Contents for Articles

Post by bugrep »

thanks that pushed me in the right direction.

the problem was the php config, not lighttpd:
; Whether or not to register the old-style input arrays, HTTP_GET_VARS
; and friends. If you're not using them, it's recommended to turn them off,
; for performance reasons.
register_long_arrays = On

this was set to Off.

thanks for this excellent support!
Post Reply