-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarklets
More file actions
executable file
·124 lines (101 loc) · 3.18 KB
/
Copy pathbookmarklets
File metadata and controls
executable file
·124 lines (101 loc) · 3.18 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
#!/usr/bin/env php
<?php
# =============================================================================
# Make bookmarklet from Javascript file
# By Justin Sternberg <me@jtsternberg.com>
# https://github.com/jtsternberg/Dot-Files/blob/master/symdotfiles
#
# Version 1.0.0
#
# Creates a bookmarklet file from an existing Javascript file.
# Handy for adding to your browser.
# Example bookmarklets: https://github.com/jtsternberg/Basecamp-UI-Improvements-Bookmarklets
#
# Uses daringfireball's bookmarklet.pl
# http://daringfireball.net/2007/03/javascript_bookmarklet_builder
#
# Usage:
# bookmarklets path/to/source.js path/to/bookmarklet.js
#
# OR to bookmarklet-icize a directory of JS files:
# bookmarklets path/source/files/ path/to/bookmarklets/destination/
# =============================================================================
if ( ! isset( $argv[1] ) ) {
echo 'Please pass the source and destination directories.';
return;
}
function is_relative_path( $path ) {
return 0 === strpos( $path, '~' );
}
function is_abs_path( $path ) {
return 0 === strpos( $path, '/' );
}
function is_js_file( $file ) {
return '.js' === substr( $file, -3 );
}
function make_bookmarklet( $js_file ) {
global $destination_dir, $working_dir, $file_ext, $destination_file, $working_file;
if ( $destination_file ) {
$new_file = $destination_dir . $destination_file;
} else {
$new_file = str_replace( $working_dir, $destination_dir, $js_file );
$new_file = str_replace(
array( '.js', '//' ),
array( '.' . $file_ext, '/' ),
$new_file
);
}
// print( __METHOD__ . ':' . __LINE__ .') : '. print_r( get_defined_vars(), true ) );
$home = $_SERVER['HOME'];
echo `perl $home/.dotfiles/bookmarklet.pl $js_file > $new_file`;
$filesize = filesize( $new_file );
if ( ! $filesize ) {
echo 'Failed to create: ' . $new_file . '.';
echo "\n";
} else {
$filesize = number_format( floatval( $filesize ) / 1024, 2 );
echo $new_file . ' created: ' . $filesize . ' kB';
echo "\n";
}
}
$file_ext = $argv[3] ?? 'bookmarklet.js';
$working_dir = $argv[1] ?? '';
$working_file = '';
if ( is_js_file( $working_dir ) ) {
$working_file = basename( $working_dir );
$working_dir = dirname( $working_dir ) . '/';
} else {
$working_dir = rtrim( $working_dir, '/' ) . '/';
}
if ( ! is_abs_path( $working_dir ) ) {
$working_dir = $_SERVER['PWD'] . ( $working_dir ? '/' . $working_dir : '' );
}
$destination_dir = $argv[2] ?? $working_dir;
$destination_file = '';
if ( is_js_file( $destination_dir ) ) {
$destination_file = basename( $destination_dir );
$destination_dir = dirname( $destination_dir ) . '/';
} else {
$destination_dir = rtrim( $destination_dir, '/' ) . '/';
}
if ( ! is_abs_path( $destination_dir ) ) {
$path = $destination_dir;
$destination_dir = $_SERVER['PWD'] . ( $path ? '/' . $path : '' );
if ( $path && ! is_js_file( $path ) ) {
$destination_dir .= '/';
}
}
if ( ! is_dir( $destination_dir ) ) {
mkdir( $destination_dir );
}
if ( $working_file ) {
return make_bookmarklet( $working_dir . $working_file );
}
$files = glob( $working_dir . '*.js' );
if ( empty( $files ) ) {
echo "Sorry, no .js files found in $working_dir\n\n";
return;
}
foreach ( $files as $file ) {
make_bookmarklet( $file );
}