File Coverage

blib/lib/App/Greple/type.pm
Criterion Covered Total %
statement 11 29 37.9
branch 0 16 0.0
condition n/a
subroutine 4 7 57.1
pod 0 3 0.0
total 15 55 27.2


line stmt bran cond sub pod time code
1             =encoding utf-8
2              
3             =head1 NAME
4              
5             App::Greple::type - file type filter module for greple
6              
7             =head1 SYNOPSIS
8              
9             greple -Mdig -Mtype --type-xxxx ... --dig .
10              
11             =head1 VERSION
12              
13             Version 1.0301
14              
15             =head1 DESCRIPTION
16              
17             This module filters search target files by given rule. It is
18             convenient to use with other B module which support recursive
19             or multi-file search such as C<-Mfind>, C<-Mdig> or C<-Mgit>.
20              
21             For example, option for Perl is defined as this:
22              
23             option --type-perl \
24             --suffix=pl,PL,pm,pod,t,psgi \
25             --shebang=perl
26              
27             Using this option, only files those name end with C<--suffix> option
28             or files which contains string C in the first C<#!> (shebang)
29             line will be searched.
30              
31             Option B<--suffix> and B<--shebang> are defined in
32             L module.
33              
34             =head2 NEGATIVE OPTIONS
35              
36             Negative options are automatically generated from positive options
37             with C<--no-> prefix. For example, C<--no-type-perl> option is
38             defined as this:
39              
40             option --no-type-perl \
41             --x-suffix=pl,PL,pm,pod,t,psgi \
42             --x-shebang=perl
43              
44             =head2 SHORT NAMES
45              
46             Short name mode is activated by default on this version. When
47             activated, all C<--type-xxxx> and C<--no-type-xxxx> options can be
48             used as C<--xxxx> and C<--no-xxxx> as well.
49              
50             As for Perl, C<--perl> and C<--no-perl> can be used.
51              
52             If you want to disable this mode, call module with config function
53             call, like C<-Mtype::config(short=0)> or C<-Mtype::config=short=0>.
54              
55             =head1 OPTIONS
56              
57             option --type-actionscript --suffix=as,mxml
58             option --type-ada --suffix=ada,adb,ads
59             option --type-asm --suffix=asm,s
60             option --type-asp --suffix=asp
61             option --type-aspx --suffix=master,ascx,asmx,aspx,svc
62             option --type-batch --suffix=bat,cmd
63             option --type-cc --suffix=c,h,xs
64             option --type-cfmx --suffix=cfc,cfm,cfml
65             option --type-clojure --suffix=clj
66             option --type-cmake --suffix=cmake --select-name=^CMakeLists.txt$
67             option --type-coffeescript --suffix=coffee
68             option --type-cpp --suffix=cpp,cc,cxx,m,hpp,hh,h,hxx,c++,h++
69             option --type-csharp --suffix=cs
70             option --type-css --suffix=css
71             option --type-dart --suffix=dart
72             option --type-delphi --suffix=pas,int,dfm,nfm,dof,dpk,dproj,groupproj,bdsgroup,bdsproj
73             option --type-elisp --suffix=el
74             option --type-elixir --suffix=ex,exs
75             option --type-erlang --suffix=erl,hrl
76             option --type-fortran --suffix=f,f77,f90,f95,f03,for,ftn,fpp
77             option --type-go --suffix=go
78             option --type-groovy --suffix=groovy,gtmpl,gpp,grunit,gradle
79             option --type-haskell --suffix=hs,lhs
80             option --type-hh --suffix=h
81             option --type-html --suffix=htm,html
82             option --type-java --suffix=java,properties
83             option --type-js --suffix=js
84             option --type-json --suffix=json
85             option --type-jsp --suffix=jsp,jspx,jhtm,jhtml
86             option --type-less --suffix=less
87             option --type-lisp --suffix=lisp,lsp
88             option --type-lua --suffix=lua --shebng=lua
89             option --type-markdown --suffix=md
90             option --type-md --type-markdown
91             option --type-make --suffix=mak,mk --select-name=^(GNUmakefile|Makefile|makefile)$
92             option --type-matlab --suffix=m
93             option --type-objc --suffix=m,h
94             option --type-objcpp --suffix=mm,h
95             option --type-ocaml --suffix=ml,mli
96             option --type-parrot --suffix=pir,pasm,pmc,ops,pod,pg,tg
97             option --type-perl --suffix=pl,PL,pm,pod,t,psgi --shebang=perl
98             option --type-perltest --suffix=t
99             option --type-php --suffix=php,phpt,php3,php4,php5,phtml --shebang=php
100             option --type-plone --suffix=pt,cpt,metadata,cpy,py
101             option --type-python --suffix=py --shebang=python
102             option --type-rake --select-name=^Rakefile$
103             option --type-rr --suffix=R
104             option --type-ruby --suffix=rb,rhtml,rjs,rxml,erb,rake,spec \
105             --select-name=^Rakefile$ --shebang=ruby
106             option --type-rust --suffix=rs
107             option --type-sass --suffix=sass,scss
108             option --type-scala --suffix=scala
109             option --type-scheme --suffix=scm,ss
110             option --type-shell --suffix=sh,bash,csh,tcsh,ksh,zsh,fish \
111             --shebang=sh,bash,csh,tcsh,ksh,zsh,fish
112             option --type-smalltalk --suffix=st
113             option --type-sql --suffix=sql,ctl
114             option --type-tcl --suffix=tcl,itcl,itk
115             option --type-tex --suffix=tex,cls,sty
116             option --type-tt --suffix=tt,tt2,ttml
117             option --type-vb --suffix=bas,cls,frm,ctl,vb,resx
118             option --type-verilog --suffix=v,vh,sv
119             option --type-vim --suffix=vim
120             option --type-xml --suffix=xml,dtd,xsl,xslt,ent --select-data='\A.*<[?]xml'
121             option --type-yaml --suffix=yaml,yml
122              
123             =head1 BACKGROUND
124              
125             This module is inspired by L command, and original matching
126             rule is taken from it.
127              
128             Filename matching can be done with B<-Mfind> module, but to know file
129             type from its content, different mechanism was required. So I made
130             the B<--begin> function can die to stop the file processing, and
131             introduced new B<-Mselect> module.
132              
133             =head1 SEE ALSO
134              
135             L, L
136              
137             L
138              
139             =head1 AUTHOR
140              
141             Kazumasa Utashiro
142              
143             =head1 LICENSE
144              
145             Copyright 2021-2023 Kazumasa Utashiro.
146              
147             This library is free software; you can redistribute it and/or modify
148             it under the same terms as Perl itself.
149              
150             =cut
151              
152             package App::Greple::type;
153             our $VERSION = "1.0301";
154              
155 1     1   631 use v5.14;
  1         3  
156 1     1   4 use warnings;
  1         2  
  1         21  
157 1     1   504 use Data::Dumper;
  1         5499  
  1         261  
158              
159             my($module, $argv);
160             my %opt = (short => 1, negative => 1);
161              
162             sub initialize {
163 0     0 0   ($module, $argv) = @_;
164             }
165              
166             sub config {
167 0     0 0   while (my($k, $v) = splice @_, 0, 2) {
168 0           $opt{$k} = $v;
169             }
170             }
171              
172             sub finalize {
173 0     0 0   my @options = $module->options;
174             #
175             # set negative --no-type-xxx options
176             #
177 0 0         if ($opt{negative}) {
178 0           my @options = $module->options;
179 0           for my $from (@options) {
180 0 0         my @to = $module->getopt($from) or next;
181 0 0         $from =~ s/^--type/--no-type/ or next;
182             grep {
183 0 0         s/^--(?=suffix|select|shebang)/--x-/g || s/^--(?=type-)/--no-/
184 0 0         } @to and do {
185 0           $module->setopt($from, \@to);
186             };
187             }
188             }
189             #
190             # set --xxx for --type-xxx option
191             # set --no-xxx for --no-type-xxx option
192             #
193 0 0         if ($opt{short}) {
194 0           my @options = $module->options;
195 0           for my $opt (@options) {
196 0 0         $opt =~ /^--(?(no-)?)type-(?.+)/ or next;
197 1     1   382 $module->setopt("--$+{no}$+{name}", $opt);
  1         315  
  1         86  
  0            
198             }
199             }
200 0 0         warn Dumper $module if $opt{dump};
201             }
202              
203             1;
204              
205             __DATA__