<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Structured Authoring &#187; Javascript</title>
	<atom:link href="http://structuredauthoring.org/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://structuredauthoring.org</link>
	<description>Learning Structured Authoring - Tools and Processes</description>
	<lastBuildDate>Fri, 14 Jan 2011 18:10:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Arbortext Comment and Uncomment Functions</title>
		<link>http://structuredauthoring.org/javascript/arbortext-comment-uncomment-functions/</link>
		<comments>http://structuredauthoring.org/javascript/arbortext-comment-uncomment-functions/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 16:00:00 +0000</pubDate>
		<dc:creator>douglaspaulwade</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://structuredauthoring.org/?p=224</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>The following two functions, (commentSelection and uncommentSelection) was written by <a href="mailto://chelberg@terraxml.com">Clay Helberg</a>. I added to my menu, &#8220;Tech Data&#8221;. I will have a future article on how to write menus. So below is just a snipnet of a menu.</p>
<div class="dean_ch" style="white-space: wrap;">menu_add <span class="st0">&quot;.TechData.&quot;</span> <span class="st0">&quot;Comment out selection&quot;</span><br />
&nbsp; -cmd <span class="br0">&#123;</span>js_eval<span class="br0">&#40;</span><span class="st0">&quot;commentSelection()&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span><br />
&nbsp; -active <span class="st0">&quot;cut_valid()&quot;</span><br />
&nbsp; -help <span class="st0">&quot;Wrap selected content in an SGML comment to hide it&quot;</span><br />
menu_add <span class="st0">&quot;.TechData.&quot;</span> <span class="st0">&quot;Uncomment out selection&quot;</span><br />
&nbsp; -cmd <span class="br0">&#123;</span>js_eval<span class="br0">&#40;</span><span class="st0">&quot;uncommentSelection()&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span><br />
&nbsp; -active <span class="st0">&quot;cut_valid()&quot;</span><br />
&nbsp; -help <span class="st0">&quot;Unwrap selected content in an SGML comment to show it&quot;</span></div>
<p><strong>Making a Comment</strong></p>
<p>To make a &#8220;comment function&#8221;, copy this code function below and place it in a file called <strong>comment.js</strong>. The file should be placed in your <strong>&lt;arbortext-installation&gt;/custom/scripts</strong> folder.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">// Cuts the selection, wraps it in comment</span><br />
<span class="co1">// delimiters, and then pastes the resulting</span><br />
<span class="co1">// comment (supplied by Clay Helberg)</span></p>
<p><span class="kw2">function</span> commentSelection<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// get current selection as string</span><br />
&nbsp; <span class="kw2">var</span> doc = Application.<span class="me1">activeDocument</span>;<br />
&nbsp; <span class="kw2">var</span> selection = doc.<span class="me1">textSelection</span>;<br />
&nbsp; <span class="kw2">var</span> selection_text = selection.<span class="me1">toMarkupString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; <span class="co1">// remember where we are</span><br />
&nbsp; <span class="kw2">var</span> start = selection.<span class="me1">startContainer</span>;<br />
&nbsp; <span class="kw2">var</span> offset = selection.<span class="me1">startOffset</span>;</p>
<p>&nbsp; <span class="co1">// generate the comment element</span><br />
&nbsp; <span class="kw2">var</span> comment = doc.<span class="me1">createComment</span><span class="br0">&#40;</span>selection_text<span class="br0">&#41;</span>;</p>
<p>&nbsp; <span class="co1">// paste the comment back in</span><br />
&nbsp; selection.<span class="me1">deleteContents</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; selection.<span class="me1">setStart</span><span class="br0">&#40;</span>start,offset<span class="br0">&#41;</span>;<br />
&nbsp; selection.<span class="me1">insertNode</span><span class="br0">&#40;</span>comment<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>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.</p>
<div id="attachment_256" class="wp-caption alignnone" style="width: 412px"><img class="size-full wp-image-256" title="makeseletion" src="http://structuredauthoring.org/wp-content/uploads/2009/05/makeseletion.png" alt="Make a valid selection" width="402" height="74" /><p class="wp-caption-text">Make a valid selection</p></div>
<p>If you are using the menu, select comment out selection.</p>
<div class="mceTemp">
<div id="attachment_255" class="wp-caption alignnone" style="width: 473px"><img class="size-full wp-image-255" title="commentout" src="http://structuredauthoring.org/wp-content/uploads/2009/05/commentout.png" alt="Menu Item Comment out selection" width="463" height="166" /><p class="wp-caption-text">Menu Item Comment out selection</p></div>
</div>
<p>If you want to use the command line, it is a bit verbose (good reason to make a menu item).</p>
<div id="attachment_258" class="wp-caption alignnone" style="width: 368px"><img class="size-full wp-image-258" title="js_eval-commentselection" src="http://structuredauthoring.org/wp-content/uploads/2009/05/js_eval-commentselection.png" alt="Command line comment selection" width="358" height="68" /><p class="wp-caption-text">Command line comment selection</p></div>
<div class="mceTemp">The results will change the entire chapter to a comment. This example shows one long comment. <strong>Tip:</strong> To minimize a comment from the command line, <strong>set showcomments=off</strong>.  To return the comments, as shown, type <strong>set showcomments=on</strong>.</div>
<div class="mceTemp">
<div id="attachment_260" class="wp-caption alignnone" style="width: 890px"><img class="size-full wp-image-260" title="commentedoutresults" src="http://structuredauthoring.org/wp-content/uploads/2009/05/commentedoutresults.png" alt="Results, one long comment" width="880" height="430" /><p class="wp-caption-text">Results - one long comment</p></div>
</div>
<p><strong>Returning a Commen</strong><strong>ted Section</strong></p>
<p>To return a comment back into valid content, place the code below into a file called <strong>uncomment.js</strong>. The file should be placed in your <strong>&lt;arbortext-installation&gt;/custom/scripts</strong> folder.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">// Cuts the selection, removes comment</span><br />
<span class="co1">// delimiters, and then pastes the resulting</span><br />
<span class="co1">// content (supplied by Clay Helberg)</span></p>
<p><span class="kw2">function</span> uncommentSelection<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; COMMENT_NODE = <span class="nu0">8</span>; <span class="co1">// W3C enumeration for node type</span></p>
<p>&nbsp; <span class="co1">// get current selection</span><br />
&nbsp; <span class="kw2">var</span> doc = Application.<span class="me1">activeDocument</span>;<br />
&nbsp; <span class="kw2">var</span> selection = doc.<span class="me1">textSelection</span>;<br />
&nbsp; <span class="kw2">var</span> selection_text = selection.<span class="me1">toMarkupString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; <span class="co1">// if it&#8217;s not a comment node, abort</span><br />
&nbsp; <span class="co1">// the operation</span><br />
&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>selection_text.<span class="me1">substring</span><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">4</span><span class="br0">&#41;</span> != <span class="st0">&quot;&lt;!&#8211;&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; Application.<span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Selection is not a comment: &#8216;&quot;</span> + selection_text + <span class="st0">&quot;&#8217;&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="co1">// remove the comment delimiters</span><br />
&nbsp; <span class="kw2">var</span> content = selection_text.<span class="me1">substring</span><span class="br0">&#40;</span><span class="nu0">4</span>,selection_text.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="nu0">-3</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; <span class="co1">// paste the comment content back in as markup</span><br />
&nbsp; selection.<span class="me1">deleteContents</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; selection.<span class="me1">insertParsedString</span><span class="br0">&#40;</span>content<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p lang="javascript">Returning a comment back to content is pretty much the reverse of making the comment. Select the comment, and from the command line, <strong>js_eval(&#8220;uncommentSelection()&#8221;)</strong> or if using the menu, select <strong>Uncomment out selection</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://structuredauthoring.org/javascript/arbortext-comment-uncomment-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

