I have a form consisting of a textarea, with line numbers thanks to Javascript. I use this form to validate JSON, just like jsonlint.org does it. The user enters some JSON, presses validate, and gets back the answer. I am not parsing the file myself, I use a python command :
python -m json.tool input.json
As you can see, this command takes a file. So what I am doing is the following :
- write form data to the file
- run this command and get output
- if no error, output to screen "Valid JSON" like jsonlint.org else show error message given by the program : "Error .... line x column y (char z)"
The line number given by the Python program corresponds to the one in the file and not to the one from textarea ... making my Javascript linenumbers useless. Finding a correspondence btw the 2 is giving me a hard time because it depends on the width of my textarea, and the font I'm using in it, and understanding how line numbers in files are set (which I do not really understand)
How could I find a correspondence btw the 2 ?
Thanks
JUST REALISING : my textarea looks exactly the same as the one you type in when you ask a question on SOF :D
----- EDIT as response to Ben ----- Very interesting. I implemented the feature I asked for using an option of the textarea called "wrap". I set to "soft" :
<form method='post' action='/exec' id="jsonform" >
<textarea wrap="hard" name="json" id="json_input" rows="20" cols="150" placeholder="Enter JSON to validate.">{{ resp | safe }}</textarea>
<div align="center">
<input type='submit' value='Validate' id="button" >
</div>
</form>
However, this makes valid JSON invalid. Because the Python module expects to have quotes before the break-line ... So I need to feed the validator with data from the form with textarea="soft", and show the data from the form with textarea="hard" . But I have one form. Also I tried the code given on the post you gave me, but it didn't work for some reason. And I don't know Javascript (need to change that.) so I couldn't debug or adapt the code ... HELLLP