Changeset 2124 for cobra/trunk/Supplements/le.cobra
- Timestamp:
- 07/04/09 08:46:25 (3 years ago)
- Files:
-
- 1 modified
-
cobra/trunk/Supplements/le.cobra (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cobra/trunk/Supplements/le.cobra
r1727 r2124 3 3 le.cobra 4 4 5 "line endings" - Report the line endings of text files as 'dos', 'unix', ' both' or '?'.5 "line endings" - Report the line endings of text files as 'dos', 'unix', 'oldmac', 'both' or '?'. 6 6 7 7 Skips binary files and reports files with unknown extensions. … … 42 42 var _action as Action 43 43 44 var _crLfRegEx = Regex(r'\r\n') 45 var _notCrLfRegEx = Regex(r'[^\r]\n') 44 var _crLfRegEx = Regex(r'\r\n') # dos / windows 45 var _notCrLfRegEx = Regex(r'[^\r]\n') # unix / mac / linux / etc 46 var _crRegEx = Regex(r'\r') # old mac 46 47 47 48 var _exts = Set<of String>() … … 167 168 _exts.add(ext) 168 169 if ext in .binExtensions 169 print 'skipping bin file : [fi]'170 print 'skipping bin file : [fi]' 170 171 else if ext in .textExtensions or ext == '' 171 172 # In practice, I find that files without extensions are text files. 172 173 # Usually captured text output from a command or a shell script. 173 174 type = .lineEndingsType(fi) 174 pad = if(type=='dos', ' ', '')175 pad = if(type=='dos', ' ', if(type=='unix', ' ', '')) # 'oldmac' is longest 175 176 print 'found text file ([type]) [pad]: [fi]' 176 177 .act(type, fi) 177 178 else 178 print 'unknown file type : "[ext]" for [fi]'179 print 'unknown file type : "[ext]" for [fi]' 179 180 180 181 def lineEndingsType(fi as FileInfo) as String 181 ensure result in ['unix', 'dos', 'both', ' ?']182 ensure result in ['unix', 'dos', 'both', 'oldmac', '?'] 182 183 text = File.readAllText(fi.fullName) 183 184 match = _crLfRegEx.match(text) … … 202 203 if isDos, return 'dos' 203 204 if isUnix, return 'unix' 205 match = _crRegEx.match(text) 206 if match.success 207 matchText = match.groups[0].value 208 assert matchText == '\r' 209 # print 'OldMac ([CobraCore.toTechString(matchText)])' 210 return 'oldmac' 204 211 return '?' 205 212 … … 212 219 if type not in ['unix', '?'] 213 220 text = File.readAllText(fi.fullName) 214 text = text.replace('\r', '') 221 if type == 'oldmac', text = text.replace('\r', '\n') 222 else, text = text.replace('\r', '') 215 223 on Action.ToDos 216 224 if type not in ['dos', '?'] 217 225 text = File.readAllText(fi.fullName) 218 text = text.replace('\r', '').replace('\n', '\r\n') 226 if type == 'oldmac', text = text.replace('\r', '\r\n') 227 else, text = text.replace('\r', '').replace('\n', '\r\n') 219 228 if text 220 229 suffix = '-convert-line-endings-' + DateTime.now.toString('yyyy-MM-dd') … … 225 234 File.delete(fi.fullName) 226 235 File.move(newName, fi.fullName) 227 print ' Converted'236 print ' Converted' 228 237 229 238 def summarizeExtensions



