Advertisement
Guest User

Editing

a guest
Aug 11th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.39 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4. <head>
  5. <title>Editing Tools</title>
  6. </head>
  7.  
  8. <body bgcolor="#E6E6FA">
  9. <header>
  10. <button onclick="Fix_Indenting('')">Fix Indenting</button>
  11. <button onclick="Fix_Newlines('')">Fix Newlines</button>
  12. <button onclick="Fix_Capitalization('')">Fix Capitalization</button>
  13. <button onclick="Fix_Spacing('')">Fix Spacing</button>
  14. <button onclick="Fix_Coding('')">Fix Coding</button>
  15. <button onclick="Fix_All()">Fix All</button>
  16. </header>
  17.  
  18. <textarea id="input" rows="40" cols="55" float:left>Copy story here</textarea>
  19. <textarea id="output" rows="40" cols="55" float:right>Edited story will appear here</textarea>
  20. </body>
  21.  
  22.  
  23. <script>
  24. function Fix_Indenting(story){
  25. if(story == ''){
  26. var story = document.getElementById("input").value; //Get input value
  27. }
  28.  
  29. var sArray = story.split('\n'); //Split input by newline chars
  30. var newStory = ""; //This will be the string displayed at the end
  31.  
  32. for(i=0; i<sArray.length; i++){
  33. //Build string to display
  34. newStory += sArray[i].trim() + '\n'; //trim() removes indenting and trailing spaces
  35. }
  36.  
  37. Copy(newStory);
  38.  
  39. RemoveTrailingSpaces();
  40. }
  41.  
  42. function Fix_Newlines(story){
  43. if(story == ''){
  44. var story = document.getElementById("input").value; //Get input value
  45. }
  46.  
  47. var length = story.length;
  48.  
  49. var i=0;
  50.  
  51. while(i<length-1){
  52. if(story.charAt(i) == '\n'){
  53. if(story.charAt(i+1) != '\n'){
  54. //One newline -> two newlines
  55. story = story.slice(0,i) + '\n' + story.slice(i);
  56. length++; //Added another char, increase length
  57. }else{
  58. //Found two newlines in a row
  59. //Need to remove any other newlines
  60. var j = 1;
  61.  
  62. while(i+j<length && story.charAt(i+j) == '\n'){
  63. //Another newline
  64. j++;
  65. }
  66.  
  67. story = story.slice(0,i+1) + story.slice(i+j-1); //Removing excess newlines
  68. length -= j; //Removed j chars, need to decrease length
  69. }
  70.  
  71. i++; //In any case, we'll be left with exactly two newlines. This puts i after the second one.
  72. }
  73.  
  74. i++;
  75. }
  76.  
  77. Copy(story);
  78.  
  79. RemoveTrailingSpaces();
  80. }
  81.  
  82. function Fix_Capitalization(story){
  83. if(story == ''){
  84. var story = document.getElementById("input").value; //Get input value
  85. }
  86.  
  87. var length = story.length;
  88.  
  89. var i=1;
  90.  
  91. //Everything in [] or {} should stay as is.
  92. //If flag1 (flag2) is true, it means we are inside [] ({})
  93. var flag1 = false;
  94. var flag2 = false;
  95.  
  96. var punctuations = ['.','?','!'];
  97.  
  98. if(story != ""){
  99. //If input isn't empty
  100.  
  101. var c = story.charAt(0);
  102.  
  103. if(c == c.toLowerCase()){
  104. //Make first letter of story uppercase
  105. story = c.toUpperCase() + story.slice(1);
  106. }
  107.  
  108. if(c == '['){
  109. //Inside []
  110. flag1 = true;
  111. }else if(c == '{'){
  112. //Inside {}
  113. flag2 = true;
  114. }
  115. }
  116.  
  117. while(i<length-1){
  118. var curr = story.charAt(i);
  119.  
  120. if(curr == '['){
  121. //Started []
  122. flag1 = true;
  123. }else if(curr == ']'){
  124. //Exited []
  125. flag1 = false;
  126. }else if(curr == '{'){
  127. //Started {}
  128. flag2 = true;
  129. }else if(curr == '}'){
  130. //Exited {}
  131. flag2 = false;
  132. }
  133.  
  134. if(flag1 || flag2){
  135. //Still inside [] or {}, continue to next char
  136. i++;
  137. continue;
  138. }
  139.  
  140. //Capitalize first-person 'i'
  141. /*
  142. To find first-person 'i' we need to find the following:
  143.  
  144. a) Find 'i'
  145. b) Found 'i' follows a space
  146. c) Found 'i' is followed by either a space or punctuation
  147. */
  148. if(curr == 'i'){
  149. //Found 'i'
  150. if(story.charAt(i-1) == ' '){
  151. //Found " i"
  152. var c = story.charAt(i+1);
  153.  
  154. if(c == ' ' || c == '\'' || ArrayContains(punctuations,c)){
  155. //Found " i" and it's followed by either a space or punctuation
  156. story = story.slice(0,i) + 'I' + story.slice(i+1); //Make 'i' uppercase
  157. }
  158. }
  159.  
  160. i++;
  161. }else if(ArrayContains(punctuations,curr)){
  162. //Punctuation
  163. if(i+2<length){
  164. if(curr == '.' && story.charAt(i+2) == '.'){
  165. //Don't capitalize after ellipses
  166. i += 2;
  167. }else if(curr == '.' && i-3>0 && story.charAt(i-3) == 'e' && story.charAt(i-2) == 't' && story.charAt(i-1) == 'c'){
  168. //Encountered etc. Don't capitalize.
  169. i++;
  170. }else{
  171. var c = story.charAt(i+2);
  172.  
  173. if(c != ' '){
  174. if(c == c.toLowerCase()){
  175. //Capitalize char after punctuation followed by a single space
  176. story = story.slice(0,i+2) + story.charAt(i+2).toUpperCase() + story.slice(i+3);
  177. }
  178. }else if(i+3<length){
  179. c = story.charAt(i+3);
  180.  
  181. if(c == c.toLowerCase()){
  182. //Capitalize char after punctuation followed by two spaces
  183. story = story.slice(0,i+3) + story.charAt(i+3).toUpperCase() + story.slice(i+4);
  184. }
  185. }
  186. }
  187. }
  188.  
  189. }else if(curr == '\n'){
  190. var c = story.charAt(i+1);
  191.  
  192. if(c == c.toLowerCase()){
  193. //Capitalize first letter of paragraph
  194. story = story.slice(0,i+1) + c.toUpperCase() + story.slice(i+2);
  195. }
  196. }
  197.  
  198. i++;
  199. }
  200.  
  201. Copy(story);
  202.  
  203. RemoveTrailingSpaces();
  204. }
  205.  
  206. function Fix_Spacing(story){
  207. if(story == ''){
  208. var story = document.getElementById("input").value; //Get input value
  209. }
  210.  
  211. var length = story.length;
  212.  
  213. var i=0;
  214.  
  215. //Everything in [] or {} should stay as is.
  216. //If flag1 (flag2) is true, it means we are inside [] ({})
  217. var flag1 = false;
  218. var flag2 = false;
  219.  
  220. if(story != ''){
  221. if(story.charAt(0) == '['){
  222. //First char is '['
  223. flag1 = true;
  224. }else if(story.charAt(0) == '{'){
  225. //First char is '{'
  226. flag2 = true;
  227. }
  228. }
  229.  
  230. //We shouldn't separate punctuation from a closing quotation mark
  231. //When quoting is true, quotation marks have opened but haven't closed
  232. var quoting = false;
  233.  
  234. var punctuations = ['.','?','!',','];
  235.  
  236. while(i < length-1){
  237. curr = story.charAt(i);
  238.  
  239. if(curr == '['){
  240. //Started []
  241. flag1 = true;
  242. }else if(curr == ']'){
  243. //Exited []
  244. flag1 = false;
  245. }else if(curr == '{'){
  246. //Started {}
  247. flag2 = true;
  248. }else if(curr == '}'){
  249. //Exited {}
  250. flag2 = false;
  251. }
  252.  
  253. if(flag1 || flag2){
  254. //Still inside [] or {}, continue to next char
  255. i++;
  256. continue;
  257. }
  258.  
  259. //Add space after punctuation
  260. if(ArrayContains(punctuations,curr)){
  261. //Found punctuation
  262. if(i+1<length){
  263. var c = story.charAt(i+1); //Char after punctuation
  264.  
  265. if(c != '\n'){ //Check if there is a newline after the punctuation (if yes, do nothing)
  266. if(c != curr && !ArrayContains(punctuations,c)){
  267. //No punctuation after initial punctuation
  268. if(c != ' '){ //Check if there is a space after punctuation
  269. if(c != '"' || !quoting){ //We neither are quoting nor did we find quotation mark
  270. //Add space after punctuation
  271. story = story.slice(0,i+1) + ' ' + story.slice(i+1);
  272. length++; //Added char, length needs to be increased
  273. }else if(i+2<length){
  274. //We stopped quoting, we need to add space after '"' (if there isn't one)
  275. if(story.charAt(i+2) != ' '){
  276. story = story.slice(0,i+2) + ' ' + story.slice(i+2);
  277. length++;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284.  
  285. if(i-1 > 0 && story.charAt(i-1) == ' '){
  286. //There should never be space on the left of punctuation
  287. story = story.slice(0,i-1) + story.slice(i);
  288. length--;
  289. }
  290. }else if(curr == '"'){
  291. //Found a quotation mark
  292. //If we were quoting, we now stopped. If we weren't quoting, we now started.
  293. quoting = !quoting;
  294. }else if(curr == ' '){
  295. if(i+1<length && i+2<length){
  296. if(story.charAt(i+1) == ' ' && story.charAt(i+2) == ' '){
  297. //Only two spaces in a row are allowed
  298. var s = 3;
  299.  
  300. while(s+i<length && story.charAt(s+i) == ' '){
  301. //Continue as long as a space is found
  302. s++;
  303. }
  304.  
  305. story = story.slice(0,i+2) + story.slice(i+s); //Remove s-2 spaces
  306. length -= s-2; //Removed s-2 chars, length needs to be decreased
  307. i += 2; //Push i after the two spaces left
  308. }
  309. }
  310. }
  311.  
  312. i++;
  313. }
  314.  
  315. Copy(story);
  316.  
  317. RemoveTrailingSpaces();
  318. }
  319.  
  320. function Fix_Coding(story){
  321. if(story == ''){
  322. var story = document.getElementById("input").value; //Get input value
  323. }
  324.  
  325. var length = story.length;
  326.  
  327. var i=0;
  328.  
  329. var triggers = ['s','p'];
  330.  
  331. /*
  332. We need to be removing lone nowiki tags, but keeping opening and closing nowiki tags.
  333.  
  334. Examples:
  335.  
  336. <nowiki> <- removed, because it just opens the tag (without closing)
  337. </nowiki> <- removed, because it just closes a tag that was never opened
  338. <nowiki> Characters here... </nowiki> <- stays, tag opens and then closes
  339. */
  340. var nowikiIndexes = []; //Here we'll save the indexes of <nowiki> we find
  341.  
  342. while(i<length){
  343. var curr = story.charAt(i);
  344.  
  345. if(curr == '<' && i+2<length){
  346. //Found tag opening, c1 and c2 are the following two chars
  347. var c1 = story.charAt(i+1);
  348. var c2 = story.charAt(i+2);
  349.  
  350. if(ArrayContains(triggers,c1) || (c1 == '/' && ArrayContains(triggers,c2))){
  351. //Found tag that either starts with 's' (or 'p') or is closing tag (with the following chars being either 's' or 'p')
  352.  
  353. if((c1 == 's' && c2 != 'p') || (c2 == 's' && i+3<length && story.charAt(i+3) != 'p')){
  354. //Tag isn't span, even though it starts with 's', move on to next char
  355. i++;
  356. continue;
  357. }else if((c1 == 'p' && c2 == 'o') || (c2 == 'p' && i+3<length && story.charAt(i+3) == 'o')){
  358. //Found poem tag
  359. i++;
  360. continue;
  361. }
  362.  
  363. //If we reached this point, it means that the tag we found should be deleted.
  364. //So, we will delete every character until we reach a '>' tag or the end of story.
  365. var j = 0;
  366.  
  367. while(i+j < length-1 && story[i+j] != '>'){
  368. //Continue until we reach a '>' tag or the end of story
  369. j++;
  370. }
  371.  
  372. story = story.slice(0,i) + story.slice(i+j+1); //Remove tag
  373. length -= j+1; //We removed chars, need to update length
  374. i--; //Set i to the index before the '<' of the tag we just removed
  375. }else if(c1 == 'b' && c2 == 'r'){
  376. //Found br tag
  377. var j = 0;
  378.  
  379. while(i+j < length-1 && story[i+j] != '>'){
  380. //Continue until we reach a '>' tag or the end of story
  381. j++;
  382. }
  383.  
  384. story = story.slice(0,i) + '\n' + story.slice(i+j+1); //Replace br tag with newline
  385. length -= j; //Removed br tag, but added newline. Need to adjust length.
  386. }else if(c1 == 'n' && c2 == 'o'){
  387. //Found <nowiki>. We need to add its index to our <nowiki> indexes.
  388. nowikiIndexes.push(i);
  389. }else if(c1 == '/' && c2 == 'n'){
  390. //Found </nowiki>. This closes an opened nowiki tag, if there is one.
  391. if(nowikiIndexes.length == 0){
  392. //We haven't got any nowiki tags opened. The closing tag we just found should be removed.
  393. story = story.slice(0,i) + story.slice(i+9); //Remove nowiki closing tag
  394. length -= 9; //Removed nowiki tag, decrease length by 9
  395. i--; //Set i to the index before the '<' of the tag we just removed
  396. }else{
  397. //We have at least one <nowiki>.
  398. //The last one (alternatively, the innermost one) should be removed, as the tag closed.
  399. nowikiIndexes.pop();
  400. }
  401. }
  402. }
  403.  
  404. i++;
  405. }
  406.  
  407. //Remove excess <nowiki>
  408. i = nowikiIndexes.length - 1;
  409.  
  410. while(i >= 0){
  411. story = story.slice(0,nowikiIndexes[i]) + story.slice(nowikiIndexes[i]+8);
  412. i--;
  413. }
  414.  
  415. Copy(story);
  416.  
  417. RemoveTrailingSpaces();
  418. }
  419.  
  420. function Fix_All(){
  421. var story = document.getElementById("input").value; //Get input value
  422.  
  423. Fix_Coding(story);
  424. story = document.getElementById("output").value; //Update story
  425. Fix_Indenting(story);
  426. story = document.getElementById("output").value; //Update story
  427. Fix_Newlines(story);
  428. story = document.getElementById("output").value; //Update story
  429. Fix_Spacing(story);
  430. story = document.getElementById("output").value; //Update story
  431. Fix_Capitalization(story);
  432. }
  433.  
  434. function RemoveTrailingSpaces(){
  435. var story = document.getElementById("output").value;
  436. var length = story.length;
  437.  
  438. var i=0;
  439.  
  440. while(i<length){
  441. var curr = story.charAt(i);
  442.  
  443. if(curr == '\n'){
  444. var j = 1;
  445.  
  446. while(i-j>0 && story.charAt(i-j) == ' '){
  447. j++;
  448. }
  449.  
  450. story = story.slice(0,i-j+1) + story.slice(i);
  451. length -= j;
  452. i -= j-1;
  453. }
  454.  
  455. i++;
  456. }
  457.  
  458. length = story.length;
  459.  
  460. if(story.charAt(length-1) == ' '){
  461. story = story.slice(0,length-1);
  462. }
  463.  
  464. Copy(story);
  465. }
  466.  
  467. function Copy(toCopy){
  468. //Copy to output
  469. document.getElementById("output").value = toCopy;
  470. }
  471.  
  472. function ArrayContains(array,item){
  473. //If array contains item, returns true
  474. var l = array.length;
  475. var i = 0;
  476.  
  477. for(i=0; i<l; i++){
  478. if(array[i] == item){
  479. return true;
  480. }
  481. }
  482.  
  483. return false;
  484. }
  485. </script>
  486. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement