-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.m
More file actions
322 lines (234 loc) · 7.24 KB
/
Copy pathUtil.m
File metadata and controls
322 lines (234 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
BeginPackage["Lui`Util`"]
Needs["WUtils`WUtils`"];
ToStringHeld::usage = "ToStringHeld "
LuiDir::usage = "LuiDir "
FileNameToDefaultLinguistic::usage = "FileNameToDefaultLinguistic "
SplitLinguisticAndAction::usage = "SplitLinguisticAndAction "
SplitLinguisticAndExpression::usage = "SplitLinguisticAndExpression "
ConvertOldLinguistic::usage = "ConvertOldLinguistic "
FilterOldLinguistics::usage = "FilterOldLinguistics "
FilterOldLinguistic::usage = "FilterOldLinguistic "
ConvertOldLinguisticDefinition::usage = "ConvertOldLinguisticDefinition "
ConvertOldLinguisticDefinitions::usage = "ConvertOldLinguisticDefinitions "
Begin["`Private`"]
(*!
\function ToStringHeld
\calltable
ToStringHeld[HoldComplete[expr_]] '' renders the inner expression as InputForm.
\maintainer danielb
*)
Attributes[ToStringHeld] = {HoldFirst};
ToStringHeld[heldExpr_HoldComplete] := StringTake[ToString[heldExpr, InputForm], {14, -2}]
ToStringHeld[heldExpr_Hold] := StringTake[ToString[heldExpr, InputForm], {6, -2}]
(*!
\function LuiDir
\calltable
LuiDir[] '' returns the base Lui directory.
\maintainer danielb
*)
LuiDir[] :=
Block[{},
FileNameDrop[FindFile["Lui`"], -2]
]
(*!
\function FileNameToDefaultLinguistic
\calltable
FileNameToDefaultLinguistic[file] '' given a filename, which for example may be camel case, returns a default linguistic to use for the file.
Examples:
FileNameToDefaultLinguistic["JustTestingABC.nb"] === "just testing abc"
Unit tests:
RunUnitTests[Lui`Util`FileNameToDefaultLinguistic]
\maintainer danielb
*)
FileNameToDefaultLinguistic[file_] :=
Block[{},
ToLowerCase[DeCamelCase[FileBaseName[file]]]
];
(*!
\function SplitLinguisticAndExpression
\calltable
SplitLinguisticAndExpression[str] '' splits a linguistic and action into it's two parts.
Examples:
SplitLinguisticAndExpression["my linguistic -> \"My Expression\""] === {"my linguistic", "\"My Expression\""}
Unit tests:
RunUnitTests[Lui`Util`SplitLinguisticAndExpression]
\maintainer danielb
*)
SplitLinguisticAndExpression[str_] :=
Block[{pattern, expression = Null},
pattern =
StringReplace[
str,
Longest[pre__] ~~ "->" ~~ expr__ :>
(
expression = StringTrim[expr];
StringTrim[pre]
)
];
{pattern, expression}
];
(*!
\function ConvertOldLinguistic
\calltable
ConvertOldLinguistic[ling] '' converts a linguistic from the old format to the new format.
Examples:
ConvertOldLinguistic["testing"] === "testing"
Unit tests:
RunUnitTests[Lui`Util`ConvertOldLinguistic]
\maintainer danielb
*)
Clear[ConvertOldLinguistic];
Clear[convertOldLinguisticInner];
ConvertOldLinguistic[ling_] := convertOldLinguisticInner[ling]
ConvertOldLinguistic[(RuleDelayed|Rule)[lhs_, rhs_]] :=
With[{action = Indent2[HoldComplete[rhs], 0, 4, "RemoveHold" -> True, "UseTabs" -> True],
pattern = ConvertOldLinguistic[lhs]},
pattern <>
If [StringLength[action] < 20 &&
StringFreeQ[action, "\n"] &&
StringLength[pattern] < 40,
" -> " <> action
,
"\n" <> Indent2[HoldComplete[rhs], 1, 4, "RemoveHold" -> True, "UseTabs" -> True]
]
]
ConvertOldLinguistic[ling_Alternatives] :=
StringJoin[Riffle[convertOldLinguisticInner /@ (List@@ling), " | "]]
ConvertOldLinguistic[ling_Global`FO] :=
StringJoin[Riffle[convertOldLinguisticInner /@ (List@@ling), " "]]
convertOldLinguisticInner[ling_Alternatives] :=
"(" <> StringJoin[Riffle[convertOldLinguisticInner /@ (List@@ling), " | "]] <> ")"
convertOldLinguisticInner[ling_String] :=
Block[{},
Which[
StringMatchQ[ling, LetterCharacter.. | DigitCharacter..],
ling
,
StringMatchQ[ling, ("$" ~~ __)],
"~" <> StringTake[ling, {2, -1}]
,
True,
ToString[ling, InputForm]
]
];
convertOldLinguisticInner[ling_Global`FO] :=
"(" <> StringJoin[Riffle[convertOldLinguisticInner /@ (List@@ling), " "]] <> ")"
convertOldLinguisticInner[ling_Global`sym] :=
With[{str = First[ling]},
"~" <> StringTake[str, {2, -1}]
]
convertOldLinguisticInner[(Global`op|Global`opt)[ling_]] :=
convertOldLinguisticInner[ling] <> "?"
convertOldLinguisticInner[ling_Global`AO] :=
With[
{
res =
ConvertOldLinguistic[
Alternatives @@
(
Global`FO @@@
Permutations[List @@ ling]
)
]
},
If [StringLength[res] > 200,
"\"PATTERN_TOO_LARGE\""
,
res
]
]
(*!
\function FilterOldLinguistics
\calltable
FilterOldLinguistics[list] '' removes items from the list that correspond to notebooks that no longer exist, etc.
Examples:
FilterOldLinguistics[
{
"just testing" -> NotebookFileName[],
"just testing" :> "C:\\Temp\\Lsdlkfjd.nb",
"just testing" :> "Todo.nb"
}
]
===
{
"just testing" ->
"E:\\Users\\Daniel\\Dropbox\\Notebooks\\FilterOldLinguistics\\FilterOldLinguistics.nb",
"just testing" :> "Todo.nb"
}
Unit tests:
RunUnitTests[Lui`Util`FilterOldLinguistics]
\maintainer danielb
*)
FilterOldLinguistics[list_] := FilterOldLinguistic /@ list
(*!
\function FilterOldLinguistic
\calltable
FilterOldLinguistic[ling] '' returns the linguistic if we should keep it, or Sequence[] if not.
Examples:
FilterOldLinguistic["just testing" :> 123] === "just testing" :> 123
Unit tests:
RunUnitTests[Lui`Util`FilterOldLinguistic]
\maintainer danielb
*)
Clear[FilterOldLinguistic];
FilterOldLinguistic[ling:(Rule|RuleDelayed)[_, action_String]] :=
Block[{keep = True},
If [!StringFreeQ[action, "\\"],
keep = FileExistsQ[action];
,
keep = ResolveIssueNotebook[action] =!= $Failed;
];
If [keep,
ling
,
Sequence @@ {}
]
]
FilterOldLinguistic[ling_] := ling
(*!
\function ConvertOldLinguisticDefinition
\calltable
ConvertOldLinguisticDefinition[ling] '' converts an old linguistic definition to the new format.
Examples:
ConvertOldLinguisticDefinition[
"$bb" -> {"bb" | "blackberry" | "black berry" :> "BlackBerry"}
]
===
"~bb:\n\tbb | blackberry | \"black berry\" -> \"BlackBerry\""
Unit tests:
RunUnitTests[Lui`Util`ConvertOldLinguisticDefinition]
\maintainer danielb
*)
ConvertOldLinguisticDefinition[ling_] :=
Block[{symbol = ling[[1]], patterns = ling[[2]]},
"~" <> StringTake[symbol, {2, -1}] <> ":\n\t" <>
StringJoin[
Riffle[
StringReplace[ConvertOldLinguistic /@ patterns, "\n\t" :> "\n\t\t"],
"\n\t"
]
]
];
(*!
\function ConvertOldLinguisticDefinitions
\calltable
ConvertOldLinguisticDefinitions[list] '' converts old linguistic definitions to the new format.
Examples:
ConvertOldLinguisticDefinitions[
{
"$curt" -> {("curt" | "curtis") | FO["curt" | "curtis", "beattie"] :> "CurtisBeattie"},
"$login" -> {"login" | "username" | "user name" | "password" | "account"},
"$bb" -> {"bb" | "blackberry" | "black berry" :> "BlackBerry"},
"$bb10" -> {FO[sym["$bb"], "10"] :> "BlackBerry10"}
}
]
===
"~curt:\n\t(curt | curtis) | ((curt | curtis) beattie)\n\t\t\"CurtisBeattie\"\n~login:\n\tlogin | username | \"user name\" | password | account\n~bb:\n\tbb | blackberry | \"black berry\" -> \"BlackBerry\"\n~bb10:\n\t~bb 10 -> \"BlackBerry10\""
Unit tests:
RunUnitTests[Lui`Util`ConvertOldLinguisticDefinitions]
\maintainer danielb
*)
ConvertOldLinguisticDefinitions[list_] :=
StringJoin[Riffle[ConvertOldLinguisticDefinition /@ list, "\n"]]
End[]
EndPackage[]