Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Editing Tools</title>
- </head>
- <body bgcolor="#E6E6FA">
- <header>
- <button onclick="Fix_Indenting('')">Fix Indenting</button>
- <button onclick="Fix_Newlines('')">Fix Newlines</button>
- <button onclick="Fix_Capitalization('')">Fix Capitalization</button>
- <button onclick="Fix_Spacing('')">Fix Spacing</button>
- <button onclick="Fix_Coding('')">Fix Coding</button>
- <button onclick="Fix_All()">Fix All</button>
- </header>
- <textarea id="input" rows="40" cols="55" float:left>Copy story here</textarea>
- <textarea id="output" rows="40" cols="55" float:right>Edited story will appear here</textarea>
- </body>
- <script>
- function Fix_Indenting(story){
- if(story == ''){
- var story = document.getElementById("input").value; //Get input value
- }
- var sArray = story.split('\n'); //Split input by newline chars
- var newStory = ""; //This will be the string displayed at the end
- for(i=0; i<sArray.length; i++){
- //Build string to display
- newStory += sArray[i].trim() + '\n'; //trim() removes indenting and trailing spaces
- }
- Copy(newStory);
- RemoveTrailingSpaces();
- }
- function Fix_Newlines(story){
- if(story == ''){
- var story = document.getElementById("input").value; //Get input value
- }
- var length = story.length;
- var i=0;
- while(i<length-1){
- if(story.charAt(i) == '\n'){
- if(story.charAt(i+1) != '\n'){
- //One newline -> two newlines
- story = story.slice(0,i) + '\n' + story.slice(i);
- length++; //Added another char, increase length
- }else{
- //Found two newlines in a row
- //Need to remove any other newlines
- var j = 1;
- while(i+j<length && story.charAt(i+j) == '\n'){
- //Another newline
- j++;
- }
- story = story.slice(0,i+1) + story.slice(i+j-1); //Removing excess newlines
- length -= j; //Removed j chars, need to decrease length
- }
- i++; //In any case, we'll be left with exactly two newlines. This puts i after the second one.
- }
- i++;
- }
- Copy(story);
- RemoveTrailingSpaces();
- }
- function Fix_Capitalization(story){
- if(story == ''){
- var story = document.getElementById("input").value; //Get input value
- }
- var length = story.length;
- var i=1;
- //Everything in [] or {} should stay as is.
- //If flag1 (flag2) is true, it means we are inside [] ({})
- var flag1 = false;
- var flag2 = false;
- var punctuations = ['.','?','!'];
- if(story != ""){
- //If input isn't empty
- var c = story.charAt(0);
- if(c == c.toLowerCase()){
- //Make first letter of story uppercase
- story = c.toUpperCase() + story.slice(1);
- }
- if(c == '['){
- //Inside []
- flag1 = true;
- }else if(c == '{'){
- //Inside {}
- flag2 = true;
- }
- }
- while(i<length-1){
- var curr = story.charAt(i);
- if(curr == '['){
- //Started []
- flag1 = true;
- }else if(curr == ']'){
- //Exited []
- flag1 = false;
- }else if(curr == '{'){
- //Started {}
- flag2 = true;
- }else if(curr == '}'){
- //Exited {}
- flag2 = false;
- }
- if(flag1 || flag2){
- //Still inside [] or {}, continue to next char
- i++;
- continue;
- }
- //Capitalize first-person 'i'
- /*
- To find first-person 'i' we need to find the following:
- a) Find 'i'
- b) Found 'i' follows a space
- c) Found 'i' is followed by either a space or punctuation
- */
- if(curr == 'i'){
- //Found 'i'
- if(story.charAt(i-1) == ' '){
- //Found " i"
- var c = story.charAt(i+1);
- if(c == ' ' || c == '\'' || ArrayContains(punctuations,c)){
- //Found " i" and it's followed by either a space or punctuation
- story = story.slice(0,i) + 'I' + story.slice(i+1); //Make 'i' uppercase
- }
- }
- i++;
- }else if(ArrayContains(punctuations,curr)){
- //Punctuation
- if(i+2<length){
- if(curr == '.' && story.charAt(i+2) == '.'){
- //Don't capitalize after ellipses
- i += 2;
- }else if(curr == '.' && i-3>0 && story.charAt(i-3) == 'e' && story.charAt(i-2) == 't' && story.charAt(i-1) == 'c'){
- //Encountered etc. Don't capitalize.
- i++;
- }else{
- var c = story.charAt(i+2);
- if(c != ' '){
- if(c == c.toLowerCase()){
- //Capitalize char after punctuation followed by a single space
- story = story.slice(0,i+2) + story.charAt(i+2).toUpperCase() + story.slice(i+3);
- }
- }else if(i+3<length){
- c = story.charAt(i+3);
- if(c == c.toLowerCase()){
- //Capitalize char after punctuation followed by two spaces
- story = story.slice(0,i+3) + story.charAt(i+3).toUpperCase() + story.slice(i+4);
- }
- }
- }
- }
- }else if(curr == '\n'){
- var c = story.charAt(i+1);
- if(c == c.toLowerCase()){
- //Capitalize first letter of paragraph
- story = story.slice(0,i+1) + c.toUpperCase() + story.slice(i+2);
- }
- }
- i++;
- }
- Copy(story);
- RemoveTrailingSpaces();
- }
- function Fix_Spacing(story){
- if(story == ''){
- var story = document.getElementById("input").value; //Get input value
- }
- var length = story.length;
- var i=0;
- //Everything in [] or {} should stay as is.
- //If flag1 (flag2) is true, it means we are inside [] ({})
- var flag1 = false;
- var flag2 = false;
- if(story != ''){
- if(story.charAt(0) == '['){
- //First char is '['
- flag1 = true;
- }else if(story.charAt(0) == '{'){
- //First char is '{'
- flag2 = true;
- }
- }
- //We shouldn't separate punctuation from a closing quotation mark
- //When quoting is true, quotation marks have opened but haven't closed
- var quoting = false;
- var punctuations = ['.','?','!',','];
- while(i < length-1){
- curr = story.charAt(i);
- if(curr == '['){
- //Started []
- flag1 = true;
- }else if(curr == ']'){
- //Exited []
- flag1 = false;
- }else if(curr == '{'){
- //Started {}
- flag2 = true;
- }else if(curr == '}'){
- //Exited {}
- flag2 = false;
- }
- if(flag1 || flag2){
- //Still inside [] or {}, continue to next char
- i++;
- continue;
- }
- //Add space after punctuation
- if(ArrayContains(punctuations,curr)){
- //Found punctuation
- if(i+1<length){
- var c = story.charAt(i+1); //Char after punctuation
- if(c != '\n'){ //Check if there is a newline after the punctuation (if yes, do nothing)
- if(c != curr && !ArrayContains(punctuations,c)){
- //No punctuation after initial punctuation
- if(c != ' '){ //Check if there is a space after punctuation
- if(c != '"' || !quoting){ //We neither are quoting nor did we find quotation mark
- //Add space after punctuation
- story = story.slice(0,i+1) + ' ' + story.slice(i+1);
- length++; //Added char, length needs to be increased
- }else if(i+2<length){
- //We stopped quoting, we need to add space after '"' (if there isn't one)
- if(story.charAt(i+2) != ' '){
- story = story.slice(0,i+2) + ' ' + story.slice(i+2);
- length++;
- }
- }
- }
- }
- }
- }
- if(i-1 > 0 && story.charAt(i-1) == ' '){
- //There should never be space on the left of punctuation
- story = story.slice(0,i-1) + story.slice(i);
- length--;
- }
- }else if(curr == '"'){
- //Found a quotation mark
- //If we were quoting, we now stopped. If we weren't quoting, we now started.
- quoting = !quoting;
- }else if(curr == ' '){
- if(i+1<length && i+2<length){
- if(story.charAt(i+1) == ' ' && story.charAt(i+2) == ' '){
- //Only two spaces in a row are allowed
- var s = 3;
- while(s+i<length && story.charAt(s+i) == ' '){
- //Continue as long as a space is found
- s++;
- }
- story = story.slice(0,i+2) + story.slice(i+s); //Remove s-2 spaces
- length -= s-2; //Removed s-2 chars, length needs to be decreased
- i += 2; //Push i after the two spaces left
- }
- }
- }
- i++;
- }
- Copy(story);
- RemoveTrailingSpaces();
- }
- function Fix_Coding(story){
- if(story == ''){
- var story = document.getElementById("input").value; //Get input value
- }
- var length = story.length;
- var i=0;
- var triggers = ['s','p'];
- /*
- We need to be removing lone nowiki tags, but keeping opening and closing nowiki tags.
- Examples:
- <nowiki> <- removed, because it just opens the tag (without closing)
- </nowiki> <- removed, because it just closes a tag that was never opened
- <nowiki> Characters here... </nowiki> <- stays, tag opens and then closes
- */
- var nowikiIndexes = []; //Here we'll save the indexes of <nowiki> we find
- while(i<length){
- var curr = story.charAt(i);
- if(curr == '<' && i+2<length){
- //Found tag opening, c1 and c2 are the following two chars
- var c1 = story.charAt(i+1);
- var c2 = story.charAt(i+2);
- if(ArrayContains(triggers,c1) || (c1 == '/' && ArrayContains(triggers,c2))){
- //Found tag that either starts with 's' (or 'p') or is closing tag (with the following chars being either 's' or 'p')
- if((c1 == 's' && c2 != 'p') || (c2 == 's' && i+3<length && story.charAt(i+3) != 'p')){
- //Tag isn't span, even though it starts with 's', move on to next char
- i++;
- continue;
- }else if((c1 == 'p' && c2 == 'o') || (c2 == 'p' && i+3<length && story.charAt(i+3) == 'o')){
- //Found poem tag
- i++;
- continue;
- }
- //If we reached this point, it means that the tag we found should be deleted.
- //So, we will delete every character until we reach a '>' tag or the end of story.
- var j = 0;
- while(i+j < length-1 && story[i+j] != '>'){
- //Continue until we reach a '>' tag or the end of story
- j++;
- }
- story = story.slice(0,i) + story.slice(i+j+1); //Remove tag
- length -= j+1; //We removed chars, need to update length
- i--; //Set i to the index before the '<' of the tag we just removed
- }else if(c1 == 'b' && c2 == 'r'){
- //Found br tag
- var j = 0;
- while(i+j < length-1 && story[i+j] != '>'){
- //Continue until we reach a '>' tag or the end of story
- j++;
- }
- story = story.slice(0,i) + '\n' + story.slice(i+j+1); //Replace br tag with newline
- length -= j; //Removed br tag, but added newline. Need to adjust length.
- }else if(c1 == 'n' && c2 == 'o'){
- //Found <nowiki>. We need to add its index to our <nowiki> indexes.
- nowikiIndexes.push(i);
- }else if(c1 == '/' && c2 == 'n'){
- //Found </nowiki>. This closes an opened nowiki tag, if there is one.
- if(nowikiIndexes.length == 0){
- //We haven't got any nowiki tags opened. The closing tag we just found should be removed.
- story = story.slice(0,i) + story.slice(i+9); //Remove nowiki closing tag
- length -= 9; //Removed nowiki tag, decrease length by 9
- i--; //Set i to the index before the '<' of the tag we just removed
- }else{
- //We have at least one <nowiki>.
- //The last one (alternatively, the innermost one) should be removed, as the tag closed.
- nowikiIndexes.pop();
- }
- }
- }
- i++;
- }
- //Remove excess <nowiki>
- i = nowikiIndexes.length - 1;
- while(i >= 0){
- story = story.slice(0,nowikiIndexes[i]) + story.slice(nowikiIndexes[i]+8);
- i--;
- }
- Copy(story);
- RemoveTrailingSpaces();
- }
- function Fix_All(){
- var story = document.getElementById("input").value; //Get input value
- Fix_Coding(story);
- story = document.getElementById("output").value; //Update story
- Fix_Indenting(story);
- story = document.getElementById("output").value; //Update story
- Fix_Newlines(story);
- story = document.getElementById("output").value; //Update story
- Fix_Spacing(story);
- story = document.getElementById("output").value; //Update story
- Fix_Capitalization(story);
- }
- function RemoveTrailingSpaces(){
- var story = document.getElementById("output").value;
- var length = story.length;
- var i=0;
- while(i<length){
- var curr = story.charAt(i);
- if(curr == '\n'){
- var j = 1;
- while(i-j>0 && story.charAt(i-j) == ' '){
- j++;
- }
- story = story.slice(0,i-j+1) + story.slice(i);
- length -= j;
- i -= j-1;
- }
- i++;
- }
- length = story.length;
- if(story.charAt(length-1) == ' '){
- story = story.slice(0,length-1);
- }
- Copy(story);
- }
- function Copy(toCopy){
- //Copy to output
- document.getElementById("output").value = toCopy;
- }
- function ArrayContains(array,item){
- //If array contains item, returns true
- var l = array.length;
- var i = 0;
- for(i=0; i<l; i++){
- if(array[i] == item){
- return true;
- }
- }
- return false;
- }
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement