lazy Atx headings to Setex headings in TextEdit

Hello.

This is a script I made during my endeavours in learning the m4 incredibly useful macroprocessor, which is the big-brother of the cpp C-preprocessor, you know, the one that initially translated C++ into C.

This little script transforms Atx-style 1 and Atx-style 2 type headings into Setext-style 1, and Setext-style 2 type headngs. Those are the only levels with Setex but Atx goes from level 1’#’ to level 6 ‘######’, in parallel with html headings, Atx should be known for those that have ever edited anything in the media-wiki format.

Example

[code]# This level 1 Atx Heading

gets transformed into

This level 1 Setex Heading

And

This level 2 Atx Heading

gets transformed into

This level 2 Setex Heading

This level 3 Atx heading

stays put.[/code]
Why?

It is nice with Markdown formatted documents, that are to be transformed further into html. You diversify between a writing and a publishing format. But John Grubers Markdown, also make messages, and regular text files, like Readme files and the lot look good, by itself, without any conversion.

Reasoning

Someone might say, that this hasn’t much to do with AppleScript, I’ll say it does, because, should I do this with all the different text editors I use, then I’d have a days job implementing it in AppleScript, maybe I still have, but now, when this job is factored out to a do shell script, all I have to do for the different editors, is to restore the selection and such, which is a far less task.

Outsourcing to tools that do the job well.

So you might say that this is a study in out-factoring and modularization, to minimize the task at hand! :slight_smile:

You might also argue, why all this, when two lines of perl, will do. Well, I have had some problems with perl encodings I don’t want to repeat. Perl can do all this, but I don’t like it, that is me.

How?

The do shell script uses a combination of m4 and sed to accomplish the task. You can reverse it the first time, and expand headings as you go along.

Caveats

The dash seem to vary wildly in length versus character width among the different monospaced fonts. Menlo is a font that seem ideal, if you prefer to use your regular font, you’ll have to make a formula using nested evals inside the x2 macro.

Enjoy :slight_smile:


# © 2012 McUsr and Put in Public Domain GPL 1.0
# http://macscripter.net/viewtopic.php?pid=158442#p158442
set failed to false
tell application "TextEdit"
    try
        set theText to text of its document 1
    on error
        set failed to true
    end try
end tell
if not failed then
    set m4Macros to "divert(-1)
define(`x1', `$1
substr(`=============================================================================================',0,eval(len($1)-1))')
define(`x2', `$1 
substr(`----------------------------------------------------------------------------------------------',0,eval(len($1)*1))')
divert(0)dnl
"
    set transText to (do shell script "( cat <<<" & quoted form of m4Macros & " ; tr '\\r' '\\n' <<<" & quoted form of theText & " |sed -e '/^\\(#\\{2\\}\\) \\(.*\\)$/ { ; s/^\\(#\\{2\\}\\) \\(.*\\)$/x2(\\2)/ ; }' -e '/^\\(#\\{1\\}\\) \\(.*\\)$/ { ; s/^\\(#\\{1\\}\\) \\(.*\\)$/x1(\\2)/ ; }' ) |m4 - |sed 1d")
    tell application "TextEdit"
        tell its document 1
            set text of it to transText
            set font to "Menlo"
        end tell
    end tell
end if

Fixed an error in adressing TextEdit’s document’ 1’s text.

It is remarkable what goes in Script Debugger. :slight_smile: