views:

24

answers:

1

I am currently using edit_area_full.js on a few web pages to format python code examples. I have a few cases where I want to pretty format json data, but edit_area does not support json. What is the best drop in replacement for edit_area that can format json data in a text box in a webpage?

Here is how I am formatting python code.

<p><textarea id="code-area" name="user_code" rows="12" cols="80">if True: pass</textarea></p>

<script language="javascript" type="text/javascript" src="/static/edit_area_full.js"></script>
  <script language="javascript" type="text/javascript">
    editAreaLoader.init({
      id: "code-area",
      syntax: "python",
      start_highlight: true,
      allow_resize: "no",
      font_size: 12,
      toolbar: "new_document, |, help",
      replace_tab_by_spaces: 4
    });
  </script>
+1  A: 

You can do it using:

You can test the later library in work here: http://jsbeautifier.org/

For example, I fed it this:

var testvar = {asd:[abc,cde,fg,{hello: world},1,2,3],ccc:"aaa",ddd:33,cas:[qwerty,[1,[2],3],4]};

And got this in result:

var testvar = {
    asd: [abc, cde, fg, {
        hello: world
    }, 1, 2, 3],
    ccc: "aaa",
    ddd: 33,
    cas: [qwerty, [1, [2], 3], 4]
};
Max
So to reformat the contents of a text box in a web page I would need to fetch the text box contents, run js_beautify(js_source_text, options), and then replace the contents of the text box with the results from this function. Just checking to make sure that I'm not making this more difficult than it is.
Chris