• Javascript

    Posted on June 3rd, 2009

    Written by douglaspaulwade

    Tags

    This is a cool function that I use often. When you select content and invoke the function, it either creates a comment from the selection, or if your selection is a comment, the second function will uncomment the content. Why this is cool is becuase, you may have some content that you want removed. But you may be unsure. Instead of deleting or saving a backup copy, I like to make it a comment. Comments are for human readers only. Editors and composition engines will ignore comments.

    The following two functions, (commentSelection and uncommentSelection) was written by Clay Helberg. I added to my menu, “Tech Data”. I will have a future article on how to write menus. So below is just a snipnet of a menu.

    menu_add ".TechData." "Comment out selection"
      -cmd {js_eval("commentSelection()") }
      -active "cut_valid()"
      -help "Wrap selected content in an SGML comment to hide it"
    menu_add ".TechData." "Uncomment out selection"
      -cmd {js_eval("uncommentSelection()") }
      -active "cut_valid()"
      -help "Unwrap selected content in an SGML comment to show it"

    Making a Comment

    To make a “comment function”, copy this code function below and place it in a file called comment.js. The file should be placed in your <arbortext-installation>/custom/scripts folder.

    // Cuts the selection, wraps it in comment
    // delimiters, and then pastes the resulting
    // comment (supplied by Clay Helberg)

    function commentSelection() {
      // get current selection as string
      var doc = Application.activeDocument;
      var selection = doc.textSelection;
      var selection_text = selection.toMarkupString();

      // remember where we are
      var start = selection.startContainer;
      var offset = selection.startOffset;

      // generate the comment element
      var comment = doc.createComment(selection_text);

      // paste the comment back in
      selection.deleteContents();
      selection.setStart(start,offset);
      selection.insertNode(comment);
    }

    Make a selection (it must be a spot where your selection would not make the file invalid when the content is removed), in your document. For my example, I chose a chapter.

    Make a valid selection

    Make a valid selection

    If you are using the menu, select comment out selection.

    Menu Item Comment out selection

    Menu Item Comment out selection

    If you want to use the command line, it is a bit verbose (good reason to make a menu item).

    Command line comment selection

    Command line comment selection

    The results will change the entire chapter to a comment. This example shows one long comment. Tip: To minimize a comment from the command line, set showcomments=off.  To return the comments, as shown, type set showcomments=on.
    Results, one long comment

    Results - one long comment

    Returning a Commented Section

    To return a comment back into valid content, place the code below into a file called uncomment.js. The file should be placed in your <arbortext-installation>/custom/scripts folder.

    // Cuts the selection, removes comment
    // delimiters, and then pastes the resulting
    // content (supplied by Clay Helberg)

    function uncommentSelection() {

      COMMENT_NODE = 8; // W3C enumeration for node type

      // get current selection
      var doc = Application.activeDocument;
      var selection = doc.textSelection;
      var selection_text = selection.toMarkupString();

      // if it’s not a comment node, abort
      // the operation
      if (selection_text.substring(0,4) != "<!–") {
        Application.alert("Selection is not a comment: ‘" + selection_text + "’");
        return;
      }
      // remove the comment delimiters
      var content = selection_text.substring(4,selection_text.length()-3);

      // paste the comment content back in as markup
      selection.deleteContents();
      selection.insertParsedString(content);
    }

    Returning a comment back to content is pretty much the reverse of making the comment. Select the comment, and from the command line, js_eval(“uncommentSelection()”) or if using the menu, select Uncomment out selection.

    This entry was posted on Wednesday, June 3rd, 2009 at 9:00 am and is filed under Javascript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • 0 Comments

    Take a look at some of the responses we have had to this article.

  • Leave a Reply

    Let us know what you thought.

  • Name(required):

    Email(required):

    Website:

    Message: