Wiki
Show
Ignore:
Timestamp:
07/04/09 08:46:25 (3 years ago)
Author:
Chuck.Esterbrook
Message:

Enhance le.cobra to support old Mac style text files.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Supplements/le.cobra

    r1727 r2124  
    33le.cobra 
    44 
    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 '?'. 
    66 
    77Skips binary files and reports files with unknown extensions. 
     
    4242    var _action as Action 
    4343 
    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 
    4647 
    4748    var _exts = Set<of String>() 
     
    167168        _exts.add(ext) 
    168169        if ext in .binExtensions 
    169             print 'skipping bin file      : [fi]' 
     170            print 'skipping bin file        : [fi]' 
    170171        else if ext in .textExtensions or ext == '' 
    171172            # In practice, I find that files without extensions are text files. 
    172173            # Usually captured text output from a command or a shell script. 
    173174            type = .lineEndingsType(fi) 
    174             pad = if(type=='dos', ' ', '') 
     175            pad = if(type=='dos', '   ', if(type=='unix', '  ', ''))  # 'oldmac' is longest 
    175176            print 'found text file ([type]) [pad]: [fi]' 
    176177            .act(type, fi) 
    177178        else 
    178             print 'unknown file type      : "[ext]" for [fi]' 
     179            print 'unknown file type        : "[ext]" for [fi]' 
    179180 
    180181    def lineEndingsType(fi as FileInfo) as String 
    181         ensure result in ['unix', 'dos', 'both', '?'] 
     182        ensure result in ['unix', 'dos', 'both', 'oldmac', '?'] 
    182183        text = File.readAllText(fi.fullName) 
    183184        match = _crLfRegEx.match(text) 
     
    202203        if isDos, return 'dos' 
    203204        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' 
    204211        return '?' 
    205212 
     
    212219                if type not in ['unix', '?'] 
    213220                    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', '') 
    215223            on Action.ToDos 
    216224                if type not in ['dos', '?'] 
    217225                    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') 
    219228        if text 
    220229            suffix = '-convert-line-endings-' + DateTime.now.toString('yyyy-MM-dd') 
     
    225234                File.delete(fi.fullName) 
    226235                File.move(newName, fi.fullName) 
    227                 print '                         Converted' 
     236                print '                           Converted' 
    228237 
    229238    def summarizeExtensions