Script to generate Remark formatted HTML file form FoldingText?

kerim's Avatar

kerim

12 Feb, 2013 09:06 AM

I've written several earlier posts about trying to use FoldingText to write HTML slide presentations. My new favorite app for this is Remark. But because the format of a Remark file is HTML it would be nice if I could work in pure FoldingText Markdown and then convert/export to Remark. Here is what would be needed to make this work.

(1) Add "---" between all top-level headers. So

# Header 1
## Header 2
- item1
# Header 3
- item 2

would become:

# Header 1
## Header 2
- item1
---
# Header 3
- item 2

(2) Insert the resulting text into the following HTML file where I've written [INSERT]

        <!DOCTYPE html>
        <html>
          <head>
            <title>Title</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <script src="https://github.com/downloads/gnab/remark/remark-0.4.4.min.js" type="text/javascript"></script>
            <style type="text/css" media="screen">
              /* Slideshow styles */
            </style>
          </head>
          <body>
            <textarea id="source">
[INSERT]
            </textarea>
            <div id="slideshow"></div>
          </body>
        </html>

(3) Finally, "Save as" in the appropriate folder.

I don't think this should be too hard, but I haven't used AppleScript in a while and could use some help.

Thanks!

  1. Support Staff 2 Posted by Jesse Grosjean on 18 Feb, 2013 03:35 PM

    Jesse Grosjean's Avatar

    A bit late... and not very pretty, but this does the first part of what you want I think:

    to searchReplace(thisText, searchTerm, replacement)
        set AppleScript's text item delimiters to searchTerm
        set thisText to thisText's text items
        set AppleScript's text item delimiters to replacement
        set thisText to "" & thisText
        set AppleScript's text item delimiters to {""}
        return thisText
    end searchReplace
    
    tell front document of application "FoldingText"
        set thisText to my searchReplace(read text, "
    # ", "
    ---
    # ")
    
        set finalText to "<!DOCTYPE html>
    <html>
        <head>
        <title>Title</title>
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>
        <script src=\"https://github.com/downloads/gnab/remark/remark-0.4.4.min.js\" type=\"text/javascript\"></script>
        <style type=\"text/css\" media=\"screen\">
            /* Slideshow styles */
        </style>
        </head>
        <body>
            <textarea id=\"source\">" & thisText & "</textarea>
            <div id=\"slideshow\"></div>
        </body>
    </html>
    "
    
        finalText
    
    end tell
    

    It doesn't do the save as part, but instead just prints the text out into the AppleScript Editor console where you can copy it.

  2. 3 Posted by kerim on 18 Feb, 2013 11:09 PM

    kerim's Avatar

    Thanks!

  3. 4 Posted by kerim on 18 Feb, 2013 11:53 PM

    kerim's Avatar

    This almost works. It pops up a save-as dialog and writes the results to a file, but somehow TextWrangler tells me that the resulting "UTF-8 file is damaged or incorrectly formed." Not sure why?

    to searchReplace(thisText, searchTerm, replacement)
        set AppleScript's text item delimiters to searchTerm
        set thisText to thisText's text items
        set AppleScript's text item delimiters to replacement
        set thisText to "" & thisText
        set AppleScript's text item delimiters to {""}
        return thisText
    end searchReplace
    
    on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
        try
            set the target_file to the target_file as text
            set the open_target_file to ¬
                open for access file target_file with write permission
            if append_data is false then ¬
                set eof of the open_target_file to 0
            write this_data to the open_target_file starting at eof
            close access the open_target_file
            return true
        on error
            try
                close access file target_file
            end try
            return false
        end try
    end write_to_file
    
    
    tell front document of application "FoldingText"
        set thisText to my searchReplace(read text, "
    # ", "
    ---
    # ")
        
        set finalText to "<!DOCTYPE html>
    <html>
        <head>
        <title>Title</title>
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>
        <script src=\"https://github.com/downloads/gnab/remark/remark-0.4.4.min.js\" type=\"text/javascript\"></script>
        <style type=\"text/css\" media=\"screen\">
            /* Slideshow styles */
        </style>
        </head>
        <body>
            <textarea id=\"source\">" & thisText & "</textarea>
            <div id=\"slideshow\"></div>
        </body>
    </html>
    "
        
        finalText
        
        set theFile to choose file name with prompt "Set file name and location:"
        my write_to_file(finalText, theFile, true)
        
    end tell
    
  4. 5 Posted by kerim on 19 Feb, 2013 02:01 AM

    kerim's Avatar

    OK. I think this solves the problem:

    to searchReplace(thisText, searchTerm, replacement)
        set AppleScript's text item delimiters to searchTerm
        set thisText to thisText's text items
        set AppleScript's text item delimiters to replacement
        set thisText to "" & thisText
        set AppleScript's text item delimiters to {""}
        return thisText
    end searchReplace
    
    on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
        try
            set the target_file to the target_file as text
            set the open_target_file to ¬
                open for access file target_file with write permission
            if append_data is false then ¬
                set eof of the open_target_file to 0
            write this_data to the open_target_file starting at eof as «class utf8»
            close access the open_target_file
            return true
        on error
            try
                close access file target_file
            end try
            return false
        end try
    end write_to_file
    
    
    tell front document of application "FoldingText"
        set thisText to my searchReplace(read text, "
    # ", "
    ---
    # ")
        
        set finalText to "<!DOCTYPE html>
    <html>
        <head>
        <title>Title</title>
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>
        <script src=\"https://github.com/downloads/gnab/remark/remark-0.4.4.min.js\" type=\"text/javascript\"></script>
        <style type=\"text/css\" media=\"screen\">
            /* Slideshow styles */
        </style>
        </head>
        <body>
            <textarea id=\"source\">" & thisText & "</textarea>
            <div id=\"slideshow\"></div>
        </body>
    </html>
    "
        
        finalText
        
        set theFile to choose file name with prompt "Set file name and location:"
        my write_to_file(finalText, theFile, true)
        
    end tell
    
  5. 6 Posted by kerim on 19 Feb, 2013 02:22 AM

    kerim's Avatar

    A couple of notes:

    The HTML can specify an external stylesheet if you replace

        <style type=\"text/css\" media=\"screen\">
            /* Slideshow styles */
        </style>
    

    with this:

    <link rel=\"stylesheet\" type=\"text/css\" href=\"remark.css\" />
    

    Also, it would be nice if the script could automatically save the file as [CURRENT FILENAME].HTML (pulling the name and path of the current FT file, and simply changing the extension to HTML and saving it in the same folder. It would need to fail gracefully if a file with that name already exists.

  6. 7 Posted by kerim on 02 Mar, 2013 06:29 AM

    kerim's Avatar

    Here is a revised script

    to searchReplace(thisText, searchTerm, replacement)
        set AppleScript's text item delimiters to searchTerm
        set thisText to thisText's text items
        set AppleScript's text item delimiters to replacement
        set thisText to "" & thisText
        set AppleScript's text item delimiters to {""}
        return thisText
    end searchReplace
    
    on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
        try
            set the target_file to the target_file as text
            set the open_target_file to ¬
                open for access file target_file with write permission
            if append_data is false then ¬
                set eof of the open_target_file to 0
            write this_data to the open_target_file starting at eof as «class utf8»
            close access the open_target_file
            return true
        on error
            try
                close access file target_file
            end try
            return false
        end try
    end write_to_file
    
    
    tell front document of application "FoldingText"
        set thisText to my searchReplace(read text, "
    # ", "
    ---
    # ")
        
        set finalText to "<!DOCTYPE html>
    <html>
        <head>
        <title>Title</title>
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>
        <script src=\"https://github.com/downloads/gnab/remark/remark-0.4.4.min.js\" type=\"text/javascript\"></script>
    <link rel=\"stylesheet\" type=\"text/css\" href=\"remark.css\" />
        </head>
        <body>
            <textarea id=\"source\">" & thisText & "</textarea>
            <div id=\"slideshow\"></div>
        </body>
    </html>
    "
        
        tell front document of application "FoldingText"
            set thedocument to file of front document of application "FoldingText"
            -- alternative to give file pre-determined name in same folder
            -- tell application "Finder"
            -- set theContainer to container of (thedocument as alias)
            -- end tell
            -- set newPath to ((theContainer as string) & "export.html")
            set newPath to ((thedocument as string) & ".html")
        end tell
        
        -- Alternative to select name of file (must replace "newpath" with "theFile" in write_to
        --  set theFile to choose file name with prompt "Set file name and location:"
        my write_to_file(finalText, newPath, true)
        
        
        
    end tell
    
  7. 8 Posted by kerim on 02 Mar, 2013 06:49 AM

    kerim's Avatar

    Oops, that version would append to an existing file instead of overwriting it when making changes. This version works better:

    to searchReplace(thisText, searchTerm, replacement)
        set AppleScript's text item delimiters to searchTerm
        set thisText to thisText's text items
        set AppleScript's text item delimiters to replacement
        set thisText to "" & thisText
        set AppleScript's text item delimiters to {""}
        return thisText
    end searchReplace
    
    on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
        try
            set the target_file to the target_file as text
            set the open_target_file to ¬
                open for access file target_file with write permission
            if append_data is false then ¬
                set eof of the open_target_file to 0
            write this_data to the open_target_file starting at eof as «class utf8»
            close access the open_target_file
            return true
        on error
            try
                close access file target_file
            end try
            return false
        end try
    end write_to_file
    
    
    tell front document of application "FoldingText"
        set thisText to my searchReplace(read text, "
    # ", "
    ---
    # ")
        
        set finalText to "<!DOCTYPE html>
    <html>
        <head>
        <title>Title</title>
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>
        <script src=\"https://github.com/downloads/gnab/remark/remark-0.4.4.min.js\" type=\"text/javascript\"></script>
    <link rel=\"stylesheet\" type=\"text/css\" href=\"remark.css\" />
        </head>
        <body>
            <textarea id=\"source\">" & thisText & "</textarea>
            <div id=\"slideshow\"></div>
        </body>
    </html>
    "
        
        tell front document of application "FoldingText"
            set thedocument to file of front document of application "FoldingText"
            set newPath to ((thedocument as string) & ".html")
        end tell
        
        my write_to_file(finalText, newPath, false)
        
        
        
    end tell
    
  8. 9 Posted by kerim on 03 Mar, 2013 07:48 AM

    kerim's Avatar

    This script works very well now. But the search-replace function doesn't work with one of the key features of Remark, which involves certain Markdown Extensions placed right after the slide divider. For instance:

    ---
    name: title
    class: center, middle, inverse
    # Silde Text
    > More slide text
    ---

    There is also a "template" function which can refer to a previous "name," inheriting all of it's class values. The search-replace currently divides the slide at the `#` and so these these additions end up being before the slide divider and not after it. I don't know if it is possible to re-write the script to fix this? It would need to make exceptions for lines starting with any of these three words, including them in the following slide, not the previous one.

    Anyone able to script this?

  9. 10 Posted by kerim on 04 Mar, 2013 02:08 AM

    kerim's Avatar

    I've fully documented this script's use and posted the latest version of the code over at Remark GitHub Wiki.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.