Dart Code Sample

Use Chrome or Safari to cut and paste this code.


DartCodeToHtmlPage.dart

/*
 Convert your Dart codes to postable and prettified HTML text.
 1. Enter the file name such as MyApp.dart or MyApp.html.
 2. Copy your code on the Dart Editor and paste it into the 'Source Code' text area.
 3. Click 'convert and add' button.
 4. Repeat 1. through 3. if necessary.
 5. Click 'finish' button.
 6. Save the converted HTML text as a file such as MyAppCode.html.
 7. Place prettify.js and prettify.css in the same directory.
    (Get them from http://code.google.com/p/google-code-prettify/)
 8. Access the MyAppCode.html from your Chrome or Dartium browser.
 April, 2012, by Cresc. Corp.
*/
 
#import('dart:html');

class DartCodeToHtmlPage {

  final textA = @'''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta name="keywords" content="Dart, code, sample" />
<html>
  <head>
    <title>Dart Code Samples</title>
    <link href="prettify.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="prettify.js"></script>
  </head>
  <body onload="prettyPrint()">
    <h1 id="status">Dart Code Sample</h1>
    <h2 style="color:red">Use Chrome or Safari to cut and paste this code.</h2><br>''';
  final textB = ''' 
  </body>
</html>''';
  
  void run() {
    document.query('#status').innerHTML = 'Convert Dart source codes to single HTML page';
    var prompt1 = document.query('#prompt1');
    var prompt2 = document.query('#prompt2');
    var prompt3 = document.query('#prompt3');
    var result = document.query('#result');
    prompt1.innerHTML = 'Enter file name and fill the "Source code" text area with your code.';
    StringBuffer resultHtml = new StringBuffer(textA);
    bool firstFile = true;
    ButtonElement convertButton = document.query('#convertButton');
    ButtonElement finishButton = document.query('#finishButton');
    convertButton.on.click.add((e){
      TextAreaElement textArea1 = document.query('#fileName');
      TextAreaElement textArea2 = document.query('#sourceCode');
      if (textArea1.value == ''){
        prompt1.innerHTML = 'Enter file name!';
      } 
      else if (textArea2.value == ''){
        prompt1.innerHTML = 'Enter source code!';
      }
      else {
        String fileName = textArea1.value;
        String sourceCode = textArea2.value;
        StringBuffer sb = new StringBuffer('');
        for (int i = 0; i < sourceCode.length; i++) {
          if (sourceCode[i] == '&') {sb.add('&amp;');}
          else if (sourceCode[i] == '"') sb.add('&quot;');
          else if (sourceCode[i] == "'") sb.add('&#39;');
          else if (sourceCode[i] == '<') sb.add('&lt;');
          else if (sourceCode[i] == '>') sb.add('&gt;');
          else sb.add(sourceCode[i]);
        }
        resultHtml.add(''' 
    <p style="font-size:large">''').add(fileName).add(@'''</p>
    <pre class="prettyprint linenums:1">
''');
        resultHtml.add(sb.toString()).add('</pre><br>');
        textArea1.value = '';
        textArea2.value = '';
        prompt1.innerHTML = 'Enter the next file name and code.';
        if (firstFile) {
          prompt2.innerHTML = 'File $fileName added, enter next file or click the finish button.<br>';
          firstFile = false;
          finishButton.disabled = false;
          finishButton.style.backgroundColor = 'yellow';
        }
        else {
          prompt2.insertAdjacentHTML('beforeend', 'File $fileName added, enter next file or click the finish button.<br>');
        }
      }
    });
    finishButton.on.click.add((e) {
      finishButton.disabled = true;
      convertButton.disabled = true;
      resultHtml.add(textB);
      print('********** Cut the following text and paste it on your text editor window to save as .html file **********');
      print(resultHtml.toString());
      prompt3.insertAdjacentHTML('beforeend', "Finished, Cut the following text and paste it on your text editor window to save as .html file.");
      prompt3.insertAdjacentHTML('beforeend', "<br>Result HTML text is also available on your browser's console.");
      prompt3.insertAdjacentHTML('beforeend', '<br>(Select the wrench menu at the top-right of your browser window, then select Tools -> JavaScript console)');
      prompt3.insertAdjacentHTML('beforeend', '<br>Reload this page for new HTML page creation.');
      result.value = resultHtml.toString();
    });
    finishButton.disabled = true;
  }
}
 
void main() {
  new DartCodeToHtmlPage().run();
}

DartCodeToHtmlPage.html

<!DOCTYPE html> 
<html>
  <head>
    <title>DartCodeToHtmlPage</title>
  </head>
  <body style="color:green">
    <h1>DartCodeToHtmlPage</h1>
    <h2 id="status">dart is not running</h2><br>
    <div id="prompt1"></div><br>File name (such as MyApp.dart or MyApp.html) : <br>
    <div><textarea id="fileName" rows='1' cols='50' style='line-height:1;'></textarea></div>
    <div>Source code :<br>
      <textarea id="sourceCode" rows='20' cols='100' style='line-height:1; font-size:small'></textarea></div>
    <button id="convertButton"; style="background-color:yellow; width: 150px"> convert and add </button><br>
    <div id="prompt2"></div><br>
    <button id="finishButton"; style="background-color:white; width: 150px">  finish  </button><br>
    <div id="prompt3"></div><br>Result HTML text : <br>
    <textarea id="result" rows='20' cols='100' style='line-height:1; color:red; font-size:small'></textarea><br>
    <script type="application/dart" src="DartCodeToHtmlPage.dart"></script>
    <script src="http://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>
  </body>
</html>