From Markdown Macro?

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
xiamenese
Posts: 543
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

From Markdown Macro?

Post by xiamenese »

I've been reading the "To Markdown macro" thread:

https://nisus.com/forum/viewtopic.php?f=17&t=6800

But what I would really be interested in is a "From Markdown macro". Some of my work I'm now doing using MultiMarkdown and opening the files in Marked2 for proofing. However, although Marked2 claims to export to RTF, the resulting file when opened in NWP doesn't have any of the styles marked … all is just marked with the display fonts and sizes used in the Marked2 proofing display.

If I could import the .md files directly into NWP, with the hash-marked heading levels, blockquotes, Strong and Emphasis properly converted, that would be great. Tables would be the cherry on top of the cake.

Does such a macro exist?

:)

Mark
B.Otter
Posts: 27
Joined: 2021-02-06 15:24:00

Re: From Markdown Macro?

Post by B.Otter »

I guess my thoughts are, "Why build a new vehicle (macro) from scratch when there's pandoc?"

https://pandoc.org/

Brad
User avatar
xiamenese
Posts: 543
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: From Markdown Macro?

Post by xiamenese »

Thanks Brad,

I've thought about Pandoc, but the learning curve on that seems steeper than trying to work out a macro, leaving aside the fact that when I last looked at Pandoc, it wouldn't install on my M1 MBA … or I couldn't get it to. I would also have to install it on my Intel iMac, also running Monterey. So yes, if I could do that, I could then spend time learning how to do what I need.

On the other hand, I'm pretty much at home in NWP. Having opened the .md file in NWP and imported my standard style sheet, I've worked out how to replace the various heading levels and blockquote in turn — which I think is all I need — using PowerFind Pro search and replace. I can macroize each PowerFind Pro, but the resulting macros are plain text so don't keep the applied formatting.

My problem is I have virtually no macro-language-fu. However, I am about to explore how other macros I have — kindly created for me by Martin! — do apply formatting, and will then see if I can use that as a model to create a macro that will do it for "Heading 1"; if I succeed, I can then add the code for other heading levels and blockquote.

In that context, using Pandoc would be akin to acquiring a sledgehammer to crack a nut.

:lol:

Mark
Bob Stern
Posts: 170
Joined: 2006-03-12 12:32:47

Re: From Markdown Macro?

Post by Bob Stern »

Martin did write a macro that works great. You open a Markdown doc in NWP, run the macro, and it creates a Nisus document in a new window:
https://nisus.com/forum/viewtopic.php?f=18&t=4013
User avatar
xiamenese
Posts: 543
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: From Markdown Macro?

Post by xiamenese »

Further to my last, having looked at my macros, I've been able to copy into a new one the code that asks which stylesheet to import, and using the code

Code: Select all

Find and Replace @Text<(^#\s)([^\n\f]+)(#$$)>, @Text<\2> 'EaU'
	Menu "Format:Paragraph Style:Heading 1"
in a test file it finds the example and gives it the style "Heading 1" which is great, but doesn't get rid of the hash-marks. Parentheses in PowerFind Pro mark what they enclose as Capture( ), and <\2> is match the second captured. This doesn't work in the macro and I can't find the equivalents in the Macro Reference manual. Furthermore, duplicating it and using ## in the duplicated version doesn't find paragraphs marked with 2 hashes.

:(

Mark
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: From Markdown Macro?

Post by martin »

xiamenese wrote: 2022-01-12 11:18:03 Further to my last, having looked at my macros, I've been able to copy into a new one the code that asks which stylesheet to import, and using the code

Code: Select all

Find and Replace @Text<(^#\s)([^\n\f]+)(#$$)>, @Text<\2> 'EaU'
	Menu "Format:Paragraph Style:Heading 1"
in a test file it finds the example and gives it the style "Heading 1" which is great, but doesn't get rid of the hash-marks.
Is there a reason you're using that different/customized macro code instead of the code from the Markdown Preview macro? The Markdown Preview macro handles the replacement in a different way, and in my testing gets rid of the hashmarks.
User avatar
xiamenese
Posts: 543
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: From Markdown Macro?

Post by xiamenese »

Simple answer, Martin. :D

I wrote my last post without seeing that Bob Stern had replied telling me about the existing macro (Thank you to Bob for giving the answer to my original request!) and while waiting for anyone to help, I had been trying to write my own.

I'm on my iPad at the moment, but will download your macro when I’m back at my computer.

Mark
User avatar
xiamenese
Posts: 543
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: From Markdown Macro?

Post by xiamenese »

I've downloaded and installed the "Markdown Preview" macro — thank you Bob Stern and Martin — from:
https://nisus.com/forum/viewtopic.php?f=18&t=4013
opened the following pretty simple .md file — zipped, as the forum doesn't support .md attachments
Trial 2.md.zip
(7.69 KiB) Downloaded 324 times
and tried it. All that happens is it makes an untitled RTF copy, giving the whole text "Normal" style.

So I'm stumped.

:(

Mark

PS This is the situation on both my 27" iMac (i5) and M1 MBA, both running Monterey 12.1
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: From Markdown Macro?

Post by martin »

xiamenese wrote: 2022-01-12 18:51:13 Simple answer, Martin. :D

I wrote my last post without seeing that Bob Stern had replied telling me about the existing macro
All clear 8)
xiamenese wrote: 2022-01-13 04:04:31 I've downloaded and installed the "Markdown Preview" macro ...
and tried it. All that happens is it makes an untitled RTF copy, giving the whole text "Normal" style.
Thanks for trying it and reporting back Mark! The problem is that the macro does not handle some differences in your Markdown syntax. For example, your headings use this markdown:

Code: Select all

##Heading 2##
But the macro expects this markdown syntax:

Code: Select all

## Heading 2
There's a few other small differences like that.

I'm attaching an updated macro that's more flexible and should handle both types of Markdown correctly. I hope it does the trick for you! Please let me how it goes for you. If any of your other documents trip it up I'm sure we can tweak the macro further.
Attachments
Markdown Preview.nwm.zip
(5.22 KiB) Downloaded 319 times
User avatar
xiamenese
Posts: 543
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: From Markdown Macro?

Post by xiamenese »

martin wrote: 2022-01-13 09:25:40
Thanks for trying it and reporting back Mark! The problem is that the macro does not handle some differences in your Markdown syntax. For example, your headings use this markdown:

Code: Select all

##Heading 2##
But the macro expects this markdown syntax:

Code: Select all

## Heading 2
There's a few other small differences like that.

I'm attaching an updated macro that's more flexible and should handle both types of Markdown correctly. I hope it does the trick for you! Please let me how it goes for you. If any of your other documents trip it up I'm sure we can tweak the macro further.
Ah, the .md file was compiled from Scrivener, which uses MultiMarkdown, so clearly a slightly different dialect! Anyway, your modified macro works perfectly. For the moment I'll simply follow it by running my current stylesheet macro, but in due course, for my own purposes, I might duplicate it and then try changing the various styles to match my standard stylesheet.

Thanks very much for your help.

:)

Mark
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: From Markdown Macro?

Post by martin »

xiamenese wrote: 2022-01-13 10:06:36 Ah, the .md file was compiled from Scrivener, which uses MultiMarkdown, so clearly a slightly different dialect! Anyway, your modified macro works perfectly.
That's great to hear! Thanks for the info and letting me know Mark :)
Post Reply