I'm using Markdown increasingly on certain jobs which require speed and simple formatting of headings, lists, quotes etc. Markdown enables fast text entry without leaving the keyboard. Is there a way I could combine Markdown with NWP to get formatted output?
OK, I can do it using styles and still rely on this a lot -- it's just that Markdown is that much faster.
Support for Markdown
- martin
- Official Nisus Person
- Posts: 5230
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
Re: Support for Markdown
Here's a macro will convert a good chunk of markdown in the front document to proper formatting:
It relies upon your document style names, and assumes the standard set exists (ie: Heading 1, Heading 2 ... Block Quote, Emphatic, Strong). If your document lacks these styles the macro will give you an error.
If there's some aspect of markdown you use that the macro doesn't cover, please let me know. You might also see this thread, in which Kino has provided a variety of macros that help with markdown.
Code: Select all
# This macro converts various inline plain "markdown" to real formatting.
# It uses the standard Nisus Writer style set. If your styles have different
# names, edit the highlighted style names in the macro as needed.
$doc = Document.active
# Convert lines prefixed by octothorpes to heading
$level = 1
$tag = Cast to String '#'
$prefix = ''
While $level < 7
$prefix &= $tag
$style = $doc.styleWithName("Heading $level")
# find all headings for this level
Replace All "^$prefix +(.*)", '\1', 'E'
$style.apply
$level += 1
End
# Lines starting with angle brackets should be block quotes
Replace All '^>\s*(.+)', '\1', 'E'
Menu 'Paragraph Style:Block Quote'
# unordered and numbered lists
Replace All '^[*\\-+] (.*)', '\1', 'E'
Menu 'Lists:Bullet List'
Replace All '^\d+\. (.*)', '\1', 'E'
Menu 'Lists:Number List'
# italics and strong
Replace All '_(.+?)_', '\1', 'E'
Menu 'Character Style:Emphatic'
Replace All '\*(\S.*?)\*', '\1', 'E'
Menu 'Character Style:Strong'
If there's some aspect of markdown you use that the macro doesn't cover, please let me know. You might also see this thread, in which Kino has provided a variety of macros that help with markdown.
Re: Support for Markdown
Hi, the macro doesn't seem to work flawlessly in NWP 2; the Markdown text"
Is rendered into:Let’s see whether **Markdown** works on this thing, *shall we*?
It appears that everything inbetween asterisks is boldized. Using single asterisks for bold and underscores for italics appears to work, but this isn't playing by the rules entirely. Also, it would mean doing a massive search and replace on my markdown files. Would it be possible to adapt the macro?Let’s see whether *Markdown* works on this thing, shall we?
- martin
- Official Nisus Person
- Posts: 5230
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
Re: Support for Markdown
Here's an updated Markdown macro that handles a variety of things better, including the emphatic/bold markings. The new macro creates a copy of the current document before applying formatting.
- Attachments
-
Markdown Preview.nwm
- (23.55 KiB) Downloaded 1487 times
Re: Support for Markdown
This seems to work perfectly. Thanks for uploading it!
Re: Support for Markdown
This is wonderful, thank you.
I'm trying to extend this macro to add support for page breaks– if I want to use ***** in Markdown and convert it to a page break in Nisus, how could I go about that?
I don't think that this is standard Markdown usage, but I need something to easily create page breaks in print...
I know little about regular expressions, I got as far as renaming the comment block and changing the repetition of *s to 5...
Thanks!
I'm trying to extend this macro to add support for page breaks– if I want to use ***** in Markdown and convert it to a page break in Nisus, how could I go about that?
I don't think that this is standard Markdown usage, but I need something to easily create page breaks in print...
I know little about regular expressions, I got as far as renaming the comment block and changing the repetition of *s to 5...
Code: Select all
# page breaks
If Replace All '^([*\-]\s?){5,}$', ' ', 'EU'
$lineParaStyle = ' '
$style = GetTargetDocumentStyle( $lineParaStyle, 'paragraphStyle' )
$style.apply
End
Thanks!
Re: Support for Markdown
No, that would not be good. The bit of macro you started to modify actually replaces all types of patterns containing repeated sequences of stars and hyphens, even allowing spaces, into horizontal lines. At current a sequence of 5 stars would also be replaced by a horizontal line.skyblue wrote:This is wonderful, thank you.
I'm trying to extend this macro to add support for page breaks– if I want to use ***** in Markdown and convert it to a page break in Nisus, how could I go about that?
I don't think that this is standard Markdown usage, but I need something to easily create page breaks in print...
I know little about regular expressions, I got as far as renaming the comment block and changing the repetition of *s to 5...
Code: Select all
# page breaks If Replace All '^([*\-]\s?){5,}$', ' ', 'EU' $lineParaStyle = ' ' $style = GetTargetDocumentStyle( $lineParaStyle, 'paragraphStyle' ) $style.apply End
Also I assume you just want a page break and don't need any special formatting applied to the page break. In that case you could just write:
Code: Select all
# page breaks
Replace All '^\*{5}$', '\f', 'E'
philip
- martin
- Official Nisus Person
- Posts: 5230
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
Re: Support for Markdown
NOTE: A new version of this macro has been posted as Markdown Preview in the macro repository. That macro includes some tweaks to be more permissible with Markdown variants, e.g. MultiMarkdown syntax.
Re: Support for Markdown
This is awesome! Seriously. I did not know (or even considered) that using Markdown to write the draft and then use a macro to convert to styled-text is an option. Now that I do, my word-processing just gained some superpowers.
Just goes to prove why I love Nisus. This should be an A-list feature listed on the front-page.
Just goes to prove why I love Nisus. This should be an A-list feature listed on the front-page.