Modify

Opened 11 years ago

Closed 7 years ago

Last modified 7 years ago

#11024 closed enhancement (fixed)

Internalise links to document

Reported by: trent.jones@… Owned by: christiano@…
Priority: normal Component: TracWikiPrintPlugin
Severity: normal Keywords:
Cc: Trac Release: 0.12

Description

When I have links in a a word file and convert it to a pdf, those links still work for the file. This however, maintains the links to the server, which is not ideal as I was hoping to use the exported PDF as an offline version of the wiki. Is it possible to make an option so that links can be internalised?

Attachments (0)

Change History (12)

comment:1 Changed 11 years ago by tekknokrat

I would support this request. It would be great to have links pointed to internal sections anchors of the pdf document. I see a huge benefit when resolving internal links, external links could be kept as-is. For this to work the plugin needs be able to convert the linked (wiki) content to html and attach this to output as well.

comment:2 Changed 7 years ago by Christian <christiano@…>

I don't know if anyone is interested, but I thought I would throw this up here. I hacked the ability to have local links of type wiki turned into anchors in the pdf. Must set the new option, local_anchor=true, to true in trac.ini.

This only works for links to local wiki pages and anchors on local wiki pages. I doubt this is the best way of going about getting this functionality, but here it is if you need it.

  • wikiprint/wikiprint.py

     
    164164    httpauth_user = Option('wikiprint', 'httpauth_user')
    165165    httpauth_password = Option('wikiprint', 'httpauth_password')
    166166    omit_links = BoolOption('wikiprint', 'omit_links')
     167    local_anchor = BoolOption('wikiprint', 'local_anchor')
    167168    omit_macros = ListOption('wikiprint', 'omit_macros')
    168169    rebase_links = Option('wikiprint', 'rebase_links')
    169170    default_charset = Option('trac', 'default_charset', 'utf-8')
     
    240241        #Now convert in that context
    241242        page = format_to_html(self.env, context, text)
    242243        self.env.log.debug('WikiPrint => Wiki to HTML output: %r', page)
     244
     245        if self.local_anchor:
     246            page = Markup("<a name=" + page_name + "></a>") + page
     247            regex = re.escape(req.abs_href()) + r'\/wiki\/'
     248            r = re.compile((regex)+r'(\w*)(#?)')
     249            page = r.sub('#\g<1>' ,page)
     250            r1 = re.compile(r'(span class="wikianchor" id=")(.*)(\/span)')
     251            page = r1.sub('a name="' + page_name + '\g<2>/a',page)
     252        self.env.log.debug('WikiPrint => HTML input to html_to_pdf is: %r',
     253                           page)
    243254
    244         self.env.log.debug('WikiPrint => HTML output for WikiPrint is: %r',
    245                            page)
    246255        self.env.log.debug('WikiPrint => Finish function wikipage_to_html')
    247256
    248257        return page

comment:3 Changed 7 years ago by Ryan J Ollos

Owner: changed from Álvaro Iradier to Ryan J Ollos
Status: newaccepted

comment:4 Changed 7 years ago by Ryan J Ollos

In 16431:

TracWikiPrintPlugin 2.0.0dev: Link to sections in book format

Refs #11024.

comment:5 in reply to:  2 ; Changed 7 years ago by Ryan J Ollos

Replying to Christian <christiano@…>:

I don't know if anyone is interested, but I thought I would throw this up here. I hacked the ability to have local links of type wiki turned into anchors in the pdf. Must set the new option, local_anchor=true, to true in trac.ini.

Looks like a good start. More work is needed for linking to section headings. Example markup:

= Heading1

Link to [#heading2].

= Heading 2 #heading2

Some more text.

comment:6 Changed 7 years ago by Ryan J Ollos

Owner: Ryan J Ollos deleted
Status: acceptednew

comment:7 in reply to:  5 Changed 7 years ago by Christian <christiano@…>

Replying to Ryan J Ollos:

Replying to Christian <christiano@…>:

I don't know if anyone is interested, but I thought I would throw this up here. I hacked the ability to have local links of type wiki turned into anchors in the pdf. Must set the new option, local_anchor=true, to true in trac.ini.

Looks like a good start. More work is needed for linking to section headings. Example markup:

= Heading1

Link to [#heading2].

= Heading 2 #heading2

Some more text.

I addressed the above example. I also add '/' seperators for consistency with links to subpages.

This hack will break if the trac format_to_html functionality changes at all. I'm really out of my element here. I employed as a hardware engineer and probably know just enough to get myself into trouble when it comes to this type of coding.

  • wikiprint/wikiprint.py

     
    164164    httpauth_user = Option('wikiprint', 'httpauth_user')
    165165    httpauth_password = Option('wikiprint', 'httpauth_password')
    166166    omit_links = BoolOption('wikiprint', 'omit_links')
     167    local_anchor = BoolOption('wikiprint', 'local_anchor')
    167168    omit_macros = ListOption('wikiprint', 'omit_macros')
    168169    rebase_links = Option('wikiprint', 'rebase_links')
    169170    default_charset = Option('trac', 'default_charset', 'utf-8')
     
    240241        #Now convert in that context
    241242        page = format_to_html(self.env, context, text)
    242243        self.env.log.debug('WikiPrint => Wiki to HTML output: %r', page)
    243 
    244         self.env.log.debug('WikiPrint => HTML output for WikiPrint is: %r',
     244
     245        if self.local_anchor:
     246            page = Markup("<a name=" + page_name + "/></a>") + page
     247            regex = re.escape(req.abs_href()) + r'\/wiki\/'
     248            r = re.compile((regex)+r'([a-zA-Z0-9_/]*)(#?)')
     249            page = r.sub('#\g<1>/' ,page)
     250            r1 = re.compile(r'(span class="wikianchor" id=")(.*)(\/span)')
     251            page = r1.sub('a name="' + page_name + '/\g<2>/a',page)
     252            r1 = re.compile(r'(h[0-9] id=")(.*)(">)')
     253            page = r1.sub('\g<1>' + page_name + '/\g<2>\g<3><a name="' + page_name + '/\g<2>"></a>' ,page)
     254
     255        self.env.log.debug('WikiPrint => HTML input to html_to_pdf is: %r',
    245256                           page)
    246257        self.env.log.debug('WikiPrint => Finish function wikipage_to_html')

comment:8 Changed 7 years ago by Ryan J Ollos

Thanks for the additional change. It will need to be rebased on r16431.

comment:9 in reply to:  8 ; Changed 7 years ago by Christian <christiano@…>

Replying to Ryan J Ollos:

Thanks for the additional change. It will need to be rebased on r16431.

Sorry about that.

Pages with sub-pages had '/' between the page and the sub-page in the link, but then the anchor would get concatenated with the sub-page name. I don't know if it really matters as we never really look at the links, but to make the links more consistent I added '/' for separators between anchors and page names. Page/<Sub-page><Anchor> ==> Page/Sub-page/Anchor Before I was just concatenating the page name and the anchor. An unwanted result is that page names without Anchors needed a '/' added as well. I'll defer to your opinion.

I had some trouble with tracwikiprintplugin/1.0/wikiprint/wikiprint.py@16431:249#L243. I was getting double #'s and I was having a problem with '/' placement. I reverted it back to the regex pattern I was using before and added a '/' to the group.

  • wikiprint/wikiprint.py

     
    243243        self.env.log.debug('WikiPrint => Wiki to HTML output: %r', page)
    244244
    245245        # Link to internal sections of document.
    246         # This needs further development for section headings (th:#11024)
    247246        if self.local_anchor:
    248             page = Markup('<a name=' + page_name + '></a>') + page
    249             r = re.compile(re.escape(req.abs_href.wiki()) + r'/(.*?)(#\w+)')
    250             page = r.sub('#\g<1>', page)
     247            page = Markup('<a name=' + page_name + '/></a>') + page
     248            r = re.compile(re.escape(req.abs_href.wiki()) + r'/([a-zA-Z0-9_/]*)(#?)')
     249            page = r.sub('#\g<1>/', page)
    251250            r1 = re.compile(r'(span class="wikianchor" id=")(.*)(/span)')
    252             page = r1.sub('a name="' + page_name + '\g<2>/a', page)
     251            page = r1.sub('a name="' + page_name + '/\g<2>/a', page)
     252            r2 = re.compile(r'(h[0-9] id=")(.*)(">)')
     253            page = r2.sub('\g<1>' + page_name + '/\g<2>\g<3><a name="' + page_name + '/\g<2>"></a>' ,page)
    253254        self.env.log.debug("WikiPrint => HTML input to html_to_pdf is: %r",
    254255                           page)

I do have an off topic question. What is the plugin used in trac-hacks that does the auto-completion for revision numbers and macros?

comment:10 in reply to:  9 Changed 7 years ago by Christian <christiano@…>

Replying to Christian <christiano@…>:

I do have an off topic question. What is the plugin used in trac-hacks that does the auto-completion for revision numbers and macros?

Never mind. I should have spent two seconds to look at the front page before asking.

comment:11 Changed 7 years ago by Ryan J Ollos

Owner: set to Ryan J Ollos
Resolution: fixed
Status: newclosed

In 16816:

TracWikiPrintPlugin 2.0.0dev: Link to internal sections of document

Fixes #11024.

comment:12 Changed 7 years ago by Ryan J Ollos

Owner: changed from Ryan J Ollos to christiano@…

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain christiano@….
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.