Inline Markdown footnotes

Get help using and writing Nisus Writer Pro macros.
Post Reply
capvideo
Posts: 20
Joined: 2008-03-16 16:41:16
Contact:

Inline Markdown footnotes

Post by capvideo » 2018-01-31 15:14:29

I use Martin’s simple Markdown script to convert Markdown to a Nisus document; I recently ran into the need to also convert inline footnotes, and added the following code:

Code: Select all

Macro.run "Markdown Preview"
$document = Document.active

#Convert Inline Footnotes
While Find Next '\[\^[^\]]+\]', "E"
	$noteText = TextSelection.activeSubtext
	$noteRange = TextSelection.activeRange
	$noteLocation = $noteRange.location
	Edit:Delete
	$noteText.deleteInRange Range.new 0, 2
	$noteText.deleteInRange Range.new $noteText.length-1, 1
	$newNote = Note.insertFootnoteInTextAtIndex $document.text, $noteLocation, $noteText
End
$document.setSelection $newNote.fullText
Format:Paragraph Style:Remove Paragraph Styles
It works, but I’m wondering if it couldn’t be better, especially the insertFootnoteInTextAtIndex line and the removing of the Normal style from the footnotes after adding.

That is,
  1. should I be using $document.text as the destination for the footnote?
  2. it seems like it would be better to be able to remove the paragraph styles (while retaining any other attributes) from the potential footnote text directly instead of removing it from all footnotes regardless of whether they were created using this macro. Something like “$noteText.removeParagraphStyles”, but I couldn’t see anything that does that in the Macro Reference. Am I missing something else that would perform the same task?

User avatar
phspaelti
Posts: 954
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Inline Markdown footnotes

Post by phspaelti » 2018-01-31 18:15:28

capvideo wrote:should I be using $document.text as the destination for the footnote?
Well what else would you use? You want to put the notes into your document. But one thing that might be an issue is that this will fail if the notes that the macro finds reside in some other type of text, like a table cell a comment, (another footnote!), etc. The alternative would be to use the .text (as different from the .subtext) of the found selection. In light of the point I'm trying to make here, the best would be to check if the text object of the active selection is the same as the document text, before trying to create a footnote out of the found bit.
it seems like it would be better to be able to remove the paragraph styles (while retaining any other attributes) from the potential footnote text directly instead of removing it from all footnotes regardless of whether they were created using this macro. Something like “$noteText.removeParagraphStyles”, but I couldn’t see anything that does that in the Macro Reference. Am I missing something else that would perform the same task?[/list]
No. There are no such commands. And there are probably pretty good reasons for this. You can manipulate the formatting of a bit of text that you are holding in memory using the Push Target command. (See the Macro Reference). This would allow you to have the "Remove Paragraph Styles" command apply to the text in memory rather than the current selection in the document.
philip

capvideo
Posts: 20
Joined: 2008-03-16 16:41:16
Contact:

Re: Inline Markdown footnotes

Post by capvideo » 2018-02-01 09:56:06

Push Target sounds interesting. However, it doesn’t look like it allows the use of menu commands on Pushed objects.
NOTE: normal menu based commands like “Bold” still affect the active document as normal.
I did try it anyway:

Code: Select all

Macro.run "Markdown Preview"
$document = Document.active

#Convert Footnotes
While Find Next '\[\^[^\]]+\]', "E"
	$noteText = TextSelection.activeSubtext
	$noteRange = TextSelection.activeRange
	$noteLocation = $noteRange.location
	Edit:Delete
	$noteText.deleteInRange Range.new 0, 2
	$noteText.deleteInRange Range.new $noteText.length-1, 1
	Push Target Text $noteText
	Format:Paragraph Style:Remove Paragraph Styles
	Pop Target Text
	$newNote = Note.insertFootnoteInTextAtIndex $document.text, $noteLocation, $noteText
End
And as the quoted text above suggests, it does not remove paragraph styles from $noteText, but rather from the paragraph the cursor happens to be in, in the active document.

Push Target appears from the examples to be mainly for the “Set…” formatting commands; I couldn’t find a “Set…” command for Setting/Unsetting Paragraph Styles (or any Styles).

User avatar
phspaelti
Posts: 954
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Inline Markdown footnotes

Post by phspaelti » 2018-02-01 22:04:29

You're right. The set of commands for manipulating formatting, styles and attributes is a bit 'hit and miss'.
One command that will work for pushed targets is Style.apply. So if you have a desired style as an object you can apply it to your target text. But removing won't be possible with that method.
When all else fails, you can put the text bit in a new document, or on a clipboard and format it there, then collect the result and dismiss the document/clipboard. This is a method that Nisus Classic users will fondly recall as the "light show" :P (NWP is fast enough that you might not even see anything.)

But for these and others reasons, I would generally avoid formatting bit by bit. A better approach is to put the bits into the document, then select just the desired bits, and apply the formatting in the document. This is what you were trying to do at first, but rather than selecting the .fullText you'll have to accumulate the individual note selections in an array. For this to work correctly you will have to carefully consider how you are inserting things into the document. Your current macro works front to back, so something like this should work (no guarantees, I just wrote this here.):

Code: Select all

#Convert Inline Footnotes
$newNotes = Array.new
While Find Next '\[\^[^\]]+\]', "E"
   $noteText = TextSelection.activeSubtext
   $noteRange = TextSelection.activeRange
   $noteLocation = $noteRange.location
   Edit:Delete
   $noteText.deleteInRange Range.new 0, 2
   $noteText.deleteInRange Range.new $noteText.length-1, 1
   $newNote = Note.insertFootnoteInTextAtIndex $document.text, $noteLocation, $noteText
   $newNotes.push TextSelection.new($newNote.fullText, $newNote.fullTextRange)
End
$document.setSelection $newNotes
Format:Paragraph Style:Remove Paragraph Styles
If you are generally working with small amounts of text (< a dozen notes), then your method will probably be ok. But usually I would opt for a method that works "in batch". First find all the notes, then process the find results in a loop, then apply the formatting. In the case of notes this can be tricky however, since removing and inserting will change your document, so if you want to keep track of them to select them again later, you have to operate carefully. This problem has been dealt with a number of times. I'd look for macros by Kino, either in the macro repository, on this forum.
philip

Post Reply