Support for Markdown

Everything related to our flagship word processor.
Post Reply
Greiggy
Posts: 16
Joined: 2005-05-16 01:10:28
Location: Between Reading and Wokingham, Berks, UK

Support for Markdown

Post by Greiggy » 2010-12-09 06:10:13

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.

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

Re: Support for Markdown

Post by martin » 2010-12-09 11:17:12

Here's a macro will convert a good chunk of markdown in the front document to proper formatting:

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'
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.

iljajj
Posts: 15
Joined: 2010-01-22 01:52:54

Re: Support for Markdown

Post by iljajj » 2011-06-19 01:46:24

Hi, the macro doesn't seem to work flawlessly in NWP 2; the Markdown text"
Let’s see whether **Markdown** works on this thing, *shall we*?
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?

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

Re: Support for Markdown

Post by martin » 2011-06-24 13:14:30

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 489 times

iljajj
Posts: 15
Joined: 2010-01-22 01:52:54

Re: Support for Markdown

Post by iljajj » 2011-06-27 23:20:20

This seems to work perfectly. Thanks for uploading it!

skyblue
Posts: 5
Joined: 2015-03-18 07:28:09

Re: Support for Markdown

Post by skyblue » 2015-05-11 18:56:21

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

Thanks!

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

Re: Support for Markdown

Post by phspaelti » 2015-05-11 20:29:10

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
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.
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'
However this line will have to be placed in the macro before the line that does the horizontal lines, or you will never get to see its effect.
philip

Post Reply