├── LICENSE ├── README.markdown └── action.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Brian Jones 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # 1Writer Title Case 2 | 3 | This [1Writer][1writer] action implements of John Gruber’s approach to [Title Case][titlecase] in Javascript. 4 | 5 | To remove the 1Writer dependency, simple remove all code below the `// 1WRITER ACTION` line. 6 | 7 | ## Usage 8 | 9 | To use as a 1Writer action, simply copy and paste the code as-is into the editor when creating a new Javascript action. 10 | 11 | To use in your own code, pass the text you’d like to title case to `toTitleCase()` and it will return formatted text. 12 | 13 | ## License 14 | 15 | The MIT License (MIT) 16 | 17 | Copyright (c) 2015 Brian Jones 18 | 19 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 20 | 21 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 22 | 23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | [1writer]: http://1writerapp.com 26 | [titlecase]: http://daringfireball.net/2008/05/title_case 27 | -------------------------------------------------------------------------------- /action.js: -------------------------------------------------------------------------------- 1 | // 1Writer Title Case Action 2 | 3 | // Adheres to John Gruber’s rules for title case 4 | // http://daringfireball.net/2008/05/title_case 5 | 6 | // Based on Paul Mucur’s Ruby implementation 7 | // https://github.com/mudge/title_case 8 | 9 | var smallWordsRegex = 10 | /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\.?|via|vs\.?)$/; 11 | 12 | function colonCheck(word) { 13 | return (word[word.length - 1] === ':'); 14 | } 15 | 16 | function titleizeIfAppropriate(word) { 17 | var dotCheckRegex = /\w\.\w/; 18 | var intercapCheckRegex = /^.+[A-Z]/; 19 | 20 | if (!word.match(dotCheckRegex) && !word.match(intercapCheckRegex)) { 21 | // Check that word begins with a letter before upcasing 22 | if (word.match(/^\W*\w/)) { 23 | word = word[0].toUpperCase() + word.substr(1); 24 | } 25 | } 26 | 27 | return word; 28 | } 29 | 30 | function toTitleCase(text) { 31 | var result = ''; 32 | var colonFlag = false; // Was the current word preceded by a colon? 33 | 34 | var words = text.split(' '); 35 | 36 | for (var i = 0; i < words.length; i++) { 37 | var formattedWord = ''; 38 | 39 | if ((i === 0) || (i === words.length - 1) || 40 | (colonFlag) || (colonCheck(words[i]))) { 41 | // Titleize first words, last words, first words after colons, 42 | // and last words before colons regardless of whether 43 | // they are small words 44 | formattedWord = titleizeIfAppropriate(words[i]); 45 | } else if (words[i].match(smallWordsRegex)) { 46 | // If none of these boundaries apply, 47 | // but word is a small word, downcase 48 | formattedWord = words[i].toLowerCase(); 49 | } else { 50 | // All other words titleized if appropriate 51 | formattedWord = titleizeIfAppropriate(words[i]); 52 | } 53 | 54 | result += formattedWord + ' '; 55 | colonFlag = colonCheck(words[i]); 56 | } 57 | 58 | result = result.substr(0, result.length - 1); // chop trailing space 59 | return result; 60 | } 61 | 62 | // ACTION 63 | 64 | var selectedText = editor.getSelectedText(); 65 | 66 | var formattedText = toTitleCase(selectedText); 67 | 68 | editor.replaceSelection(formattedText); 69 | --------------------------------------------------------------------------------