Zen Coding

This forum allows you to share scripts with other Zeus users. Please do not post bug reports, feature requests or questions to this forum, but rather use it exclusively for posting scripts or for the discussion of scripts that have been posted.
jussij
Site Admin
Posts: 2228
Joined: Fri Aug 13, 2004 5:10 pm

Zen Coding

Postby jussij » Sun Nov 08, 2009 9:50 am

NOTE: The script below now comes as standard with the Zeus installer. To use this script just run the installer and then bind this macro to a keyboard key or to the menu.

The Zeus macro shown below wraps the Zen Coding plug-in allowing the package to be used from within the Zeus editor.

Installation Procedure

Step 1: Download the Zen Coding plug-in.

Step 2: Copy the following files found in this package:

Code: Select all

__init__.py
settings.py
zen_core.py

into the following folder location:

Code: Select all

C:\Program Files\Zeus\zScript\zencoding

Step 3: Copy the Zeus macro code shown below and save it to the following file:

Code: Select all

C:\Program Files\Zeus\zScript\zeus_zen.py

Testing the Installation

To test the installation do the following:

Step 1: Use the Zeus Macros, Load menu to load the zeus_zen.py macro script created above.

Step 2: Create a new file inside of Zeus and type in the following Zen command.

Code: Select all

html>table>tr*5

Step 3: Use the Zeus Macros, Playback menu to run the loaded macro script and this Zen command will be replaced with the following text expansion:

Code: Select all

<html>
   <table>
      <tr></tr>
      <tr></tr>
      <tr></tr>
      <tr></tr>
      <tr></tr>
   </table>
</html>

Here are some more Zen HTML and CSS examples.

Further Configuration - Bind Macro to the Keyboard

In the example above the macro was loaded and executed using the Macros menu. It is also possible to bind this macro to the keyboard by using the Zeus Options, Editor Options menu, making it much easier to execute.

Macro Code

Code: Select all

#
#         Name: Zen Coding Macro
#       Author: Jussi Jumppanen
#     Language: Python
#  Description:
#
#  This is Zeus Python macro that wraps around the Zen Coding
#  package found here:
#
#      http://code.google.com/p/zen-coding/source/checkout
#
#  For this macro to run the Zen Coding package needs to have been
#  correctly installed.
#
#  This package consists of the following files:
#
#      __init__.py
#      settings.py
#      zen_core.py
#
#  To install the Zen Coding package create the 'zencoding' folder in
#  the 'C:\Program Files\Zeus\zScript' folder and copy the three
#  files listed above to this new folder.
#
#  Zen HTML Examples:
#      http://code.google.com/p/zen-coding/wiki/ZenHTMLElementsEn
#
#  Zen CSS Examples:
#      http://code.google.com/p/zen-coding/wiki/ZenCSSPropertiesEn
#

import os
import re
import zeus

from zencoding import zen_core as zen
from zencoding.settings import zen_settings

def key_macro():
    locked = zeus.is_read_only()
    document = zeus.is_document()

    # macro only works for read/write documents.
    if (document == 1) and (locked == 0):
        zeus.screen_update_disable()

        zen.newline = '\n'
        zen.insertion_point = '##IP##'

        # get the current line details
        cur_line = zeus.get_line_text()
        cur_index = zeus.cursor_to_index(zeus.get_line_pos(), zeus.get_cursor_pos())

        # check for the end of line case
        cur_index = min(cur_index, len(cur_line))

        # make sure we are not past the EOL
        if zeus.is_cursor_online() == 0:
            zeus.MoveLineEnd()

            # no end of line text
            end_line = ""
        else:
            # get the end of line text
            end_line = cur_line[cur_index:len(cur_line)]

        # Help with bebugging
        #zeus.message_box(1, "line: " + cur_line + " index: " + str(cur_index))

        if cur_index > 0:
            # look for a Zen abbreviation
            abbr, start_index = zen.find_abbr_in_line(cur_line, cur_index)

            if abbr:
                # Help with bebugging
                #zeus.message_box(1, "abbr : " + abbr + " start: " + str(start_index))

                # get the current file extensions
                extension = zeus.macro_tag("$Ext")

                # set the document type based on the extension
                if (extension.lower() == ".css" ):
                    doc_type = 'xsl'
                else:
                    doc_type = 'html'

                # build up a new line by expanding out the Zen abbreviation
                result = zen.expand_abbr(abbr, doc_type)

                # Help with debugging
                #zeus.message_box(1, "result: " + result + "(" + abbr + ")")

                if result:
                    # the complete new line
                    result = cur_line[0:start_index] + result

                    # see if the current line had any padding
                    cur_line_pad = re.match(r'^(\s+)', cur_line)

                    if cur_line_pad:
                        # add the padding to the new line
                        result = zen.pad_string(result, cur_line_pad.group(1))

                    # see if spaces are being used for tabs
                    if zeus.macro_tag("$UseTabs") == 'false':
                        # get the tab size for the current document
                        tab_size = zeus.macro_tag("$TabSize")

                        # expand the tab characters into spaces
                        result = result.expandtabs(int(tab_size))

                    # remove the current line
                    zeus.MoveLineHome()
                    zeus.LineDeleteEnd()

                    # add in the end of line text
                    result = result + end_line

                    # write out the new line including the abbreviation
                    zeus.write(result, 0)

                    # try to look for an insertion point
                    if (result.find(zen.insertion_point) <> -1):
                        # save the current search options
                        zeus.search_options_save()
                        zeus.set_search_option("Scope"     , 0)
                        zeus.set_search_option("WholeWord" , 0)
                        zeus.set_search_option("UseCase"   , 0)
                        zeus.set_search_option("RegExpress", 0)
                        # find the insertion point
                        zeus.set_find_text(zen.insertion_point)
                        # put the cursor at the insertion point
                        zeus.set_cursor_pos(start_index + 1)
                        # locate insertion point
                        zeus.SearchPrevious()
                        # remove the insertion point text
                        zeus.MarkDelete()
                        # restore the saved search options
                        zeus.search_options_restore()
                else:
                    # give some feedback
                    zeus.message("The current line does not contain a valid Zen Code abbreviation.")
                    zeus.beep()
            else:
                # give some feedback
                zeus.message("The current line does not contain a valid Zen Code abbreviation.")
                zeus.beep()

        zeus.screen_update_enable()
    else:
        zeus.message("This macro only works for writable document files!")
        zeus.beep()

key_macro() # run the macro

Cheers Jussi

Return to “Scripts”

Who is online

Users browsing this forum: No registered users and 0 guests

cron