Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick macro to do markdown formatting in word
Rem Incomplete, work in progresss, mucking around with markdown via Word
'For use on OS X with Word 2011
' Requires pandoc - installed in /usr/local/bin
' Need to have styles Indent1 Indent2 etc with hanging indent at some standard increment (say 1cm), with tab-stops 1 for Indent1, 2 for Indent2
' To install, paste into a module, maybe in your Normal template
' Bind SaveIt to, say Ctrl-s and MarkIt to Ctrl-q
Sub saveAsDoc(name)
ActiveDocument.SaveAs fileName:=name, FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, HTMLDisplayOnlyOutput:=False, MaintainCompat:= _
False
End Sub
Sub loseTabs()
Selection.WholeStory
With Selection.Find
.ClearFormatting
.Text = Chr(9)
.Replacement.ClearFormatting
.Replacement.Text = " "
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
End Sub
Sub saveit()
Call markit
Set doc = ActiveDocument
Dim name As String
'Remember this doc's name
name = ActiveDocument.FullName
fileName = ActiveDocument.name
'Call saveAsDoc(name)
Selection.WholeStory
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdPasteDefault)
Call loseTabs
ActiveDocument.SaveAs fileName:=name + ".md.html", FileFormat:=wdFormatHTML, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, HTMLDisplayOnlyOutput:=False, MaintainCompat:=False
ActiveDocument.SaveAs fileName:=name + ".md", FileFormat:=wdFormatText, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, Encoding:=65001, InsertLineBreaks:=False, AllowSubstitutions:= _
False, LineEnding:=wdCRLF
'saveAsDoc (name)
ActiveWindow.Close
Dim path As Variant
pathArray = Split(name, ":")
path = "/"
For dirnum = 1 To UBound(pathArray) - 1
path = path + pathArray(dirnum) + "/"
Next
'cmd = "do shell script ""/usr/local/bin/pandoc --self-contained -t html -o '" + path + fileName + ".html" + "' '" + path + fileName + ".md'"""
'TODO: for self contained to work need to cd to the same dir as the doc
cmd = "do shell script ""/usr/local/bin/pandoc --standalone -t html -o '" + path + fileName + ".html" + "' '" + path + fileName + ".md'"""
'MsgBox (cmd)
res = MacScript(cmd)
cmd = "do shell script ""open '" + path + fileName + ".html" + "'"""
res = MacScript(cmd)
End Sub
Sub markit()
With Options
.AutoFormatAsYouTypeApplyBulletedLists = False
End With
Call labelImages
Dim doc As Document
Dim para As Paragraph
Dim tabb As String
tabb = Chr(9)
Set doc = ActiveDocument
For Each para In doc.Paragraphs
Dim contents As String
contents = para.Range.Text
numtabs = UBound(Split(contents, tabb))
If numtabs > 4 Then numtabs = 4
If InStr(contents, "####") = 1 Then
para.Style = "Heading 4"
ElseIf InStr(contents, "###") = 1 Then
para.Style = "Heading 3"
ElseIf InStr(contents, "##") = 1 Then
para.Style = "Heading 2"
ElseIf InStr(contents, "#") = 1 Then
para.Style = "Heading 1"
ElseIf numtabs > 0 Then
para.Style = "Indent" + Trim(Str(numtabs))
Else
para.Style = "Normal"
End If
Next
End Sub
Sub labelImages()
Dim i As Integer
Dim name As String
name = ActiveDocument.name
Dim picrange As Range
Dim alt As String
'loop through inline shapes
For i = 1 To ActiveDocument.InlineShapes.Count
'Super hacky way of padding an int - there must be a better way!
j = i + 1000
Set picrange = ActiveDocument.InlineShapes.Item(i).Range
alt = ActiveDocument.InlineShapes.Item(i).AlternativeText
picrange.Start = picrange.End
picrange.End = picrange.Start + 2
If picrange.Text = "![" Then
picrange.MoveEndUntil ")"
picrange.End = picrange.End + 1
picrange.Delete
End If
picrange.InsertBefore "![" + alt + "](./" + name + ".md_files/image" + Right(Trim(Str(j * 2)), 3) + ".png)"
Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment