Sunday, July 23, 2017

#1 2012-12-07 04:24:22 am

Registered: 2012-11-21
Posts: 3046

lazy Atx headings to Setex headings in TextEdit


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.

[code]# This level 1 Atx Heading

gets transformed into

This level 1 Setex Heading


## This level 2 Atx Heading

gets transformed into

This level 2 Setex Heading

### This level 3 Atx heading

stays put.[/code]

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.


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


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.


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 smile


# © 2012 McUsr and Put in Public Domain GPL 1.0
# [url][/url]
set failed to false
tell application "TextEdit"
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
define(`x2', `$1

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

Last edited by McUsrII (2012-12-07 04:49:54 am)

Filed under: TextEdit, Atx, Setex, m4



#2 2012-12-07 04:52:37 am

Registered: 2012-11-21
Posts: 3046

Re: lazy Atx headings to Setex headings in TextEdit

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

It is remarkable what goes in Script Debugger. smile

Filed under: TextEdit, Atx, Setex, m4



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)