-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
401 lines (342 loc) · 13.7 KB
/
Copy pathscript.js
File metadata and controls
401 lines (342 loc) · 13.7 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Python Functions Reference Guide - JavaScript
// PDF Download Functionality
async function downloadPDF() {
const downloadBtn = document.querySelector('.download-btn');
const originalText = downloadBtn.textContent;
try {
// Show loading state
downloadBtn.textContent = '⏳ Generating PDF...';
downloadBtn.disabled = true;
// Create PDF using jsPDF
const { jsPDF } = window.jspdf;
const pdf = new jsPDF('p', 'mm', 'a4');
// Add title page
pdf.setFontSize(24);
pdf.setTextColor(44, 62, 80);
pdf.text('Python Functions Reference Guide', 20, 30);
pdf.setFontSize(14);
pdf.setTextColor(100, 100, 100);
pdf.text('Complete reference for Python built-in functions and math module', 20, 45);
pdf.setFontSize(12);
pdf.setTextColor(0, 0, 0);
let yPosition = 70;
const pageHeight = 297; // A4 height in mm
const margin = 20;
// Add table of contents
pdf.setFontSize(16);
pdf.setTextColor(44, 62, 80);
pdf.text('Table of Contents', margin, yPosition);
yPosition += 15;
pdf.setFontSize(12);
pdf.setTextColor(0, 0, 0);
pdf.text('1. Python Built-in Functions ........................... 3', margin, yPosition);
yPosition += 8;
pdf.text('2. Python Math Module Functions .................... 15', margin, yPosition);
yPosition += 8;
pdf.text('3. Practical Usage Examples ......................... 25', margin, yPosition);
yPosition += 8;
pdf.text('4. Quick Reference Tips ............................. 28', margin, yPosition);
// Add new page for content
pdf.addPage();
yPosition = 30;
// Get all function cards
const sections = document.querySelectorAll('.section');
// Process each section
sections.forEach((sectionElement, sectionIndex) => {
const sectionHeader = sectionElement.querySelector('h2');
if (!sectionHeader) return;
// Add section header
pdf.setFontSize(18);
pdf.setTextColor(44, 62, 80);
const sectionTitle = sectionHeader.textContent;
pdf.text(sectionTitle, margin, yPosition);
yPosition += 15;
// Get function cards for this section
const sectionCards = sectionElement.querySelectorAll('.function-card');
sectionCards.forEach((card) => {
// Check if we need a new page
if (yPosition > pageHeight - 60) {
pdf.addPage();
yPosition = 30;
}
const functionName = card.querySelector('.function-name')?.textContent || '';
const description = card.querySelector('.function-description')?.textContent || '';
const syntax = card.querySelector('.function-syntax')?.textContent || '';
const example = card.querySelector('.function-example')?.textContent || '';
// Function name
pdf.setFontSize(14);
pdf.setTextColor(231, 76, 60);
pdf.text(functionName, margin, yPosition);
yPosition += 8;
// Description
pdf.setFontSize(10);
pdf.setTextColor(85, 85, 85);
const descLines = pdf.splitTextToSize(description, 170);
pdf.text(descLines, margin, yPosition);
yPosition += descLines.length * 5 + 3;
// Syntax
pdf.setFontSize(9);
pdf.setTextColor(44, 62, 80);
pdf.text('Syntax:', margin, yPosition);
yPosition += 5;
pdf.setTextColor(0, 0, 0);
const syntaxLines = pdf.splitTextToSize(syntax, 170);
pdf.text(syntaxLines, margin + 5, yPosition);
yPosition += syntaxLines.length * 4 + 3;
// Example
pdf.setTextColor(39, 174, 96);
pdf.text('Example:', margin, yPosition);
yPosition += 5;
pdf.setTextColor(0, 0, 0);
const exampleLines = pdf.splitTextToSize(example, 170);
pdf.text(exampleLines, margin + 5, yPosition);
yPosition += exampleLines.length * 4 + 8;
});
yPosition += 10; // Space between sections
});
// Add footer to all pages
const pageCount = pdf.internal.getNumberOfPages();
for (let i = 1; i <= pageCount; i++) {
pdf.setPage(i);
pdf.setFontSize(8);
pdf.setTextColor(150, 150, 150);
pdf.text(`Page ${i} of ${pageCount}`, 170, 285);
pdf.text('Python Functions Reference Guide', margin, 285);
}
// Save the PDF
pdf.save('Python_Functions_Reference_Guide.pdf');
// Success message
downloadBtn.textContent = '✅ Downloaded!';
setTimeout(() => {
downloadBtn.textContent = originalText;
downloadBtn.disabled = false;
}, 2000);
} catch (error) {
console.error('PDF generation failed:', error);
// Fallback to print dialog
downloadBtn.textContent = '🖨️ Opening Print Dialog...';
setTimeout(() => {
window.print();
downloadBtn.textContent = originalText;
downloadBtn.disabled = false;
}, 1000);
}
}
// Search Functionality
function addSearchFunctionality() {
const searchContainer = document.createElement('div');
searchContainer.className = 'search-container';
searchContainer.innerHTML = `
<input type="text" id="searchInput" placeholder="🔍 Search functions..."
autocomplete="off" aria-label="Search functions">
`;
const content = document.querySelector('.content');
const firstChild = content.querySelector('.alphabet-nav');
if (firstChild) {
content.insertBefore(searchContainer, firstChild);
} else {
content.insertBefore(searchContainer, content.firstChild);
}
const searchInput = document.getElementById('searchInput');
const functionCards = document.querySelectorAll('.function-card');
searchInput.addEventListener('input', function() {
const searchTerm = this.value.toLowerCase().trim();
let visibleCount = 0;
functionCards.forEach(card => {
const functionName = card.querySelector('.function-name')?.textContent.toLowerCase() || '';
const description = card.querySelector('.function-description')?.textContent.toLowerCase() || '';
const syntax = card.querySelector('.function-syntax')?.textContent.toLowerCase() || '';
if (searchTerm === '' ||
functionName.includes(searchTerm) ||
description.includes(searchTerm) ||
syntax.includes(searchTerm)) {
card.style.display = 'block';
visibleCount++;
} else {
card.style.display = 'none';
}
});
// Update sections visibility
updateSectionVisibility();
// Show/hide "no results" message
showNoResultsMessage(visibleCount === 0 && searchTerm !== '');
});
// Clear search on Escape key
searchInput.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
this.value = '';
this.dispatchEvent(new Event('input'));
this.blur();
}
});
}
// Update section visibility based on search results
function updateSectionVisibility() {
const sections = document.querySelectorAll('.section');
sections.forEach(section => {
const visibleCards = section.querySelectorAll('.function-card[style*="display: block"], .function-card:not([style*="display: none"])');
const sectionHeader = section.querySelector('h2');
if (visibleCards.length === 0) {
section.style.display = 'none';
} else {
section.style.display = 'block';
}
});
}
// Show "no results" message
function showNoResultsMessage(show) {
let noResultsMsg = document.getElementById('noResultsMessage');
if (show && !noResultsMsg) {
noResultsMsg = document.createElement('div');
noResultsMsg.id = 'noResultsMessage';
noResultsMsg.innerHTML = `
<div style="text-align: center; padding: 40px; color: #666;">
<h3>🔍 No functions found</h3>
<p>Try adjusting your search terms or browse all functions below.</p>
</div>
`;
document.querySelector('.content').appendChild(noResultsMsg);
} else if (!show && noResultsMsg) {
noResultsMsg.remove();
}
}
// Smooth scrolling for navigation links
function setupSmoothScrolling() {
document.querySelectorAll('.alphabet-nav a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
const headerOffset = 20;
const elementPosition = target.offsetTop;
const offsetPosition = elementPosition - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
}
});
});
}
// Keyboard shortcuts
function setupKeyboardShortcuts() {
document.addEventListener('keydown', function(e) {
// Ctrl/Cmd + K to focus search
if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
e.preventDefault();
const searchInput = document.getElementById('searchInput');
if (searchInput) {
searchInput.focus();
searchInput.select();
}
}
// Ctrl/Cmd + P to download PDF
if ((e.ctrlKey || e.metaKey) && e.key === 'p') {
e.preventDefault();
downloadPDF();
}
});
}
// Add copy functionality to code blocks
function setupCodeCopy() {
const codeBlocks = document.querySelectorAll('.function-syntax, .function-example');
codeBlocks.forEach(block => {
block.style.position = 'relative';
block.style.cursor = 'pointer';
block.title = 'Click to copy';
block.addEventListener('click', function() {
const text = this.textContent;
navigator.clipboard.writeText(text).then(() => {
// Show temporary feedback
const originalTitle = this.title;
this.title = 'Copied!';
this.style.opacity = '0.7';
setTimeout(() => {
this.title = originalTitle;
this.style.opacity = '1';
}, 1000);
}).catch(() => {
console.log('Copy failed');
});
});
});
}
// Theme toggle (if needed in future)
function setupThemeToggle() {
// Reserved for future dark mode toggle functionality
}
// Analytics (if needed)
function trackEvent(eventName, properties = {}) {
// Reserved for future analytics integration
console.log('Event:', eventName, properties);
}
// Error handling
window.addEventListener('error', function(e) {
console.error('JavaScript error:', e.error);
});
// Initialize everything when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
console.log('Python Functions Reference Guide loaded');
// Initialize all functionality
addSearchFunctionality();
setupSmoothScrolling();
setupKeyboardShortcuts();
setupCodeCopy();
// Track page load
trackEvent('page_loaded', {
timestamp: new Date().toISOString(),
userAgent: navigator.userAgent
});
});
// Service worker registration (for future PWA support)
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
// Reserved for future PWA functionality
});
}
// Utility functions
const utils = {
// Debounce function for search
debounce: function(func, wait, immediate) {
let timeout;
return function executedFunction(...args) {
const later = () => {
timeout = null;
if (!immediate) func(...args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func(...args);
};
},
// Format text for better display
formatText: function(text) {
return text.replace(/\n/g, '<br>').replace(/\t/g, ' ');
},
// Check if element is in viewport
isInViewport: function(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
};
// Export for potential module use
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
downloadPDF,
addSearchFunctionality,
updateSectionVisibility,
showNoResultsMessage,
setupSmoothScrolling,
setupKeyboardShortcuts,
setupCodeCopy,
setupThemeToggle,
trackEvent,
utils
};
}