Convert html links into footnotes

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Trenien
Posts: 5
Joined: Tue May 13, 2008 12:39 pm

Convert html links into footnotes

Post by Trenien »

I downloaded an html file that includes (whithin the file) internal links that work as endnotes. To be clear, they are links of that kind:
file://Name_of_the_file#Endnote_number.
This is the file I downloaded: http://oll.libertyfund.org/index.php?op ... ayout=html

I wish to print the file, but that implies modifying it so these "Endnotes" become footnotes (anything else would make them completely useless). Of course, I could do it manually, but there are more than 250 of these, so I'd rather avoid it.
I already converted the file into an ODT document.

The notes themselve have the following structure:
[reference] body of the note \n - I think it's \n. It's the unprintable character that looks somewhat like Pi (π). There may be one or more carriage return in the body of an endnote.

The manual convertion process is such:
1. In the text body, CTRL+Left click on a ("endnote") link to go to the endnote itself (the link is either associated with an uppercase number or, most of the time, with one or more words in the text)
2. Select the body of the note, between the right bracket ] and the \n. Either copy or cut it.
3. Go back to the appropriate spot in the main body, using the link associated with [reference].
4. Use the Insert -> Footnotes command and paste in the appropriate spot after confirmation.
5. Repeat with the next link/note.

I did try to make a macro with the recording tool. Here is its code:

Code: Select all

REM  *****  BASIC  *****

Sub Main

End Sub


sub Convert_Footnote
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Bookmark"
args1(0).Value = "lf1292_footnote_nt_037"

dispatcher.executeDispatch(document, ".uno:JumpToMark", "", 0, args1())

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:Open", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Cut", "", 0, Array())

rem ----------------------------------------------------------------------
dim args20(0) as new com.sun.star.beans.PropertyValue
args20(0).Name = "Bookmark"
args20(0).Value = "c_lf1292_footnote_nt_037"

dispatcher.executeDispatch(document, ".uno:JumpToMark", "", 0, args20())

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:Open", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:InsertFootnote", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())


end sub
So this code should be modified so that it
- Dynamically takes the link for any endnote instead of only using the one I used to make the macro.
- Select by itself between the ] and the \n.
- Loops so the macra needs only to be used once for the entire file.
I have no idea how to do any of the above.

Help?
User avatar
Bhikkhu Pesala
Posts: 1253
Joined: Mon Oct 08, 2007 1:27 am

Re: Convert html links into footnotes

Post by Bhikkhu Pesala »

It is not so difficult to do manually.
Image
Select a footnote index anchor, right-click, and select Footnote to edit footnotes and change them to endnotes, or vice versa.

Use the scroll arrows to scroll right through the document, editing each footnote in turn. This is an easy way to change footnotes to endnotes or vice versa.

I believe there is another method by editing content.xml using Find and Replace in a text editor.
Idiot Compassion
LibreOffice 6.0.4 on Windows 10
Trenien
Posts: 5
Joined: Tue May 13, 2008 12:39 pm

Re: Convert html links into footnotes

Post by Trenien »

Thanks for the tip, but I did already know it.

My problem isn't the difficulty of converting one note into a footnote. The problem is that there are more than 250 of them, which would mean 4-5 hours of tedious, repeating work that the computer ought to be able to do by itself.

As for working on the xml file itself, that means dealing with two variables (the number of a specific note to navigate between the two relevant position in the file, and the text of said note that you have to move around). Unfortunately that's one variable too much for my meager regexp abilities.
User avatar
Bhikkhu Pesala
Posts: 1253
Joined: Mon Oct 08, 2007 1:27 am

Re: Convert html links into footnotes

Post by Bhikkhu Pesala »

Trenien wrote:The problem is that there are more than 250 of them, which would mean 4-5 hours of tedious, repeating work that the computer ought to be able to do by itself.
More like 4-5 minutes. I just tested with a document with 162 footnotes, converting them all to endnotes in less than three minutes. It would take you much longer than that to find a solution using macros.

Use the dialogue access key to switch to Footnotes or Endnotes, and the mouse to scroll to the next note.
Idiot Compassion
LibreOffice 6.0.4 on Windows 10
Trenien
Posts: 5
Joined: Tue May 13, 2008 12:39 pm

Re: Convert html links into footnotes

Post by Trenien »

Er... That would mean 1 note every 3 to 5 seconds. Either you're a god of the mouse, or your document is nothing like mine.

I already did such a conversion by hand on another similar file, with roughly the same number of notes. It took me the better part of 5 hours. Some notes (at least one out of five) are ambiguous in their positioning (depending on two subsequent characters) and may go to completely different position in the notes portion of the file. And let's not forget the many notes that span many paragraphs...

Just to understand what I'm talking about, go and take a quick look at the file I've linked, go past the first 10th of it and test a couple of notes. You'll see what I'm talking about.
Trenien
Posts: 5
Joined: Tue May 13, 2008 12:39 pm

Re: Convert html links into footnotes

Post by Trenien »

Oops, sorry about my last post, I just realized there's been a mistake: I only cursorily read your fist post and didn't realize you did the same with mine:
The file isn't, originaly, an openoffic file but an html file. That means the "notes" only link to another position within the file. Converting them doesn't only mean checking a box to say "this isn't and endnote, it's a footnote". If only...

To convert them, I have to click on the note reference (hyperlink) which brings me to the text of the note, copy or cut that text, click on the hyperlink that brings me back to the original position, insert a note and confirm, paste the text of the note. Rince, repeat.

Believe me, even for the cleanest, shortest ones, that takes significantly more than five seconds (more like between 30 seconds and one minutes, when there's no ambiguity in the note)
User avatar
Bhikkhu Pesala
Posts: 1253
Joined: Mon Oct 08, 2007 1:27 am

Re: Convert html links into footnotes

Post by Bhikkhu Pesala »

Trenien wrote: I only cursorily read your fist post and didn't realize you did the same with mine:
The file isn't, originaly, an openoffic file but an html file. That means the "notes" only link to another position within the file. Converting them doesn't only mean checking a box to say "this isn't and endnote, it's a footnote". If only...
:oops: It would certainly have helped if I had read your post more carefully. I was working under the assumption that you had already converted the HTML to an OpenOffice document with endnotes and needed to convert them to footnotes.
Idiot Compassion
LibreOffice 6.0.4 on Windows 10
carra
Posts: 8
Joined: Sun Jun 01, 2008 9:07 pm

Re: Convert html links into footnotes

Post by carra »

Same problem here with HTML footnote reference.

Help?

Thanks in advance.

carra
OOo 3.0.X on Ms Windows XP + Ubuntu
hagenp
Posts: 3
Joined: Fri Aug 01, 2008 4:49 pm

Re: Convert html links into footnotes

Post by hagenp »

Enjoy! -H

Code: Select all

' Insert hyperlinks from the current document as Endnotes.
' Derived from OpenOffice.org 2.0.3 / Gimmicks / GetHyperlinks()
' 2008-08-01 Hagen Patzke
Option Explicit

Sub Main
	RemoveAllEndnotes()
	Hyperlinks2Endnotes()
End Sub


Sub Hyperlinks2Endnotes()
	Dim oDocument         as Object
	Dim oSearchDesc       as Object
	Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue
	Dim oAllHyperLinks    as Object
	Dim oFound            as Object
	Dim oCrsr             as Object
	Dim oEndnote          as Object
	Dim sURL              as String
	Dim i                 as integer

	' get access to the document
	oDocument   = ThisComponent

	' create a Search Descriptor
	oSearchDesc = oDocument.CreateSearchDescriptor
	oSearchDesc.Valuesearch = False

	' define the Search-attributes
	SrchAttributes(0).Name  = "HyperLinkURL"
	SrchAttributes(0).Value = ""
	oSearchDesc.SetSearchAttributes(SrchAttributes())

	' Get all Hyperlinks
	oAllHyperLinks = oDocument.findAll(oSearchDesc())

	' Loop through them and save the link URL as Endnote text
	For i = 0 to oAllHyperLinks.Count - 1

		oFound = oAllHyperLinks(i)
		oCrsr  = oFound.Text.createTextCursorByRange(oFound)

		' save HyperLink URL text
		sURL   = oCrsr.HyperLinkURL

		' position at end of HyperLink text
		oCrsr.collapseToEnd()

		' prepare Endnote
		oEndnote = oDocument.createInstance("com.sun.star.text.Endnote")
		oEndnote.setLabel("[" & i & "]")

		' insert the Endnote 
		oDocument.Text.insertTextContent(oCrsr,oEndnote,FALSE)

		' only now we can insert Endnote text (the HyperLink URL)
		'oEndCrsr = oEndnote.Text.createTextCursor()
		'oEndNote.Text.insertString(oEndCrsr, oCrsr.HyperLinkURL, TRUE)
		oEndNote.setString(sURL)

	Next i

End Sub


Sub RemoveAllEndnotes()
	Dim oDocument         as Object
	Dim oAllEndnotes      as Object
	Dim oEndnote          as Object
	Dim i                 as integer

	oDocument    = ThisComponent
	oAllEndnotes = oDocument.getEndnotes()

	For i = oAllEndnotes.Count - 1 to 0 step -1
		oEndnote = oAllEndnotes(i)
		oEndnote.dispose()
	Next i

End Sub

OOo 3.X on WinXP and Ubuntu 9.10
DCC
Posts: 1
Joined: Wed Sep 17, 2008 1:51 pm

Re: Convert html links into footnotes

Post by DCC »

Thanks for the code - it was just the solution for which I was looking.

I have a couple of questions.

The file
endnote behavior.odt
small file showing basic and script generated endnotes
(16.54 KiB) Downloaded 399 times
shows two types of endnotes, "basic" and with the script you posted. Both work, but differently.

Text "xxxx A*" generates an actual endnote listing at the end of the doc "*A meaning of xxxx". Clicking on A* takes you to *A, and clicking on *A returns you to A*.

When generated by the hyperlinks2endnotes script, " link-yyyy [B*]" creates "[*B] url-link-yyyy". Clicking on the link "link-yyyy" takes you to the link target. Putting the cursor on or clicking on [B*] shows the text of the footnote in a tooltip: "url-link-yyyy". There is no "jump" to the associated endnote. However, a click on [*B] (the index of the endnote) takes you to [B*] (main text).

But, when
endnote behavior.pdf
pdf generated from .odt
(47.82 KiB) Downloaded 349 times
is created, the behaviors change.

The only jumps programmed are A* to *A and [B*] to [*B]. There are no jumps back from the endnotes to the main text.

Is this the best it gets, or is there something I can do to (1) get clicking on [B*] to jump to [*B] in the .odt or (2) get clicking on the endnote index to jump back to the main text in the .pdt file?

Thanks.
- D
Trenien
Posts: 5
Joined: Tue May 13, 2008 12:39 pm

Re: Convert html links into footnotes

Post by Trenien »

Close to a year later, check back, just in case...
Oh my... I can't believe it! Someone actually took the time to actually write the macro.

And then...

It doesn't work :cry:

Is there a bug, or was I unclear in what I was trying to achieve ? In any case, all the macro does is to put the url of each of the notes as endnote at the end of the file. Of course what I'd like as endnote ( or even better, footnotes) is the actual text of the notes.

Oh well, I'll just go throw myself off a cliff, now... :knock:
hagenp
Posts: 3
Joined: Fri Aug 01, 2008 4:49 pm

Re: Convert html links into footnotes

Post by hagenp »

Trenien wrote:Is there a bug, or was I unclear in what I was trying to achieve ?
Yes, your request was unclear to me.
Trenien wrote:In any case, all the macro does is to put the url of each of the notes as endnote at the end of the file.
Correct, it does exactly what I was looking for when I came across this forum entry:
  • The macro converts hyperlinks into endnotes (not footnotes),
  • so that hyperlink references are preserved when an article is printed.
Trenien wrote:Of course what I'd like as endnote (or even better, footnotes) is the actual text of the notes.
Well, then you have either to code this yourself, or find someone else who does it for you.
From the macro I posted, you can take
  1. the logic of looping through a document's hyperlinks, and
  2. the logic to add an endnote.
What you need additionally is to
  1. follow each hyperlink,
  2. open it as a new document,
  3. select and copy the complete contained text
  4. paste it as an endnote in the first document, and then
  5. close the document you opened for the hyperlink.
Happy programming! :-)
Last edited by hagenp on Wed Apr 21, 2010 3:22 pm, edited 1 time in total.
OOo 3.X on WinXP and Ubuntu 9.10
hagenp
Posts: 3
Joined: Fri Aug 01, 2008 4:49 pm

Re: Convert html links into footnotes

Post by hagenp »

DCC wrote:Thanks for the code - it was just the solution for which I was looking.
I have a couple of questions. [...]
Sorry if the macro does not exactly what you need:
My problem was that I needed a macro to convert a html link (hyperlink) into something printable at the end of a document.

This is what brought me here, and when I had a solution for my problem I posted it in the hope it might be useful for someone.

Well, it actually does "Convert html links into footnotes" (OK, endnotes...), after all. :D
OOo 3.X on WinXP and Ubuntu 9.10
Post Reply