-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
772 lines (673 loc) · 32.9 KB
/
app.py
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
from imports import *
log_dir = os.getenv("LOG_DIRECTORY", "logs/")
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_file_path = os.path.join(log_dir, "application.log")
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(log_file_path),
logging.StreamHandler()
])
logger = logging.getLogger(__name__)
logger.info("Logging system initialized")
load_dotenv()
app = Flask(__name__)
# limiter = Limiter(key_func=get_remote_address,
# storage_uri="redis://redis:6379",
# default_limits=["15 per minute"])
# limiter.init_app(app)
app.secret_key = os.environ.get('SECRET_KEY', 'dev-secret-key')
# Route for the home page
@app.route('/')
@app.route('/home')
def home():
# return redirect(url_for('diff_viewer'))
logger.info(f"Home page loaded!!!")
return render_template('index.html')
# Diff Viewer Page
@app.route('/string-tools/diff-viewer', methods=['GET', 'POST'])
def diff_viewer():
try:
diff_result = None
text1 = ""
text2 = ""
if request.method == 'POST':
# Retrieve text inputs
text1 = request.form.get('text1', '')
text2 = request.form.get('text2', '')
# Generate diff result
diff_result = difflib.HtmlDiff().make_file(text1.splitlines(), text2.splitlines(),
fromdesc="Text 1", todesc="Text 2")
# Pass the inputs back to the template, along with the diff result
return render_template('diff_viewer.html', diff_result=diff_result, text1=text1, text2=text2)
except Exception as e:
logger.error(f"An error occurred. {(str(e))}")
# JSON schema generator
@app.route('/json-tools/json-schema-generator', methods=['GET', 'POST'])
def json_schema_generator():
schema_result = None
schema_for_copying = None
json_input = ""
conditionals = ""
error_message = None
if request.method == 'POST':
json_input = request.form.get('json_input', '').strip()
conditionals_input = request.form.get('conditionals', '').strip()
if not json_input:
error_message = "JSON input is empty. Please provide a valid JSON."
else:
try:
conditionals = json.loads(conditionals_input) if conditionals_input else None
generated_schema = generate_json_schema(json_input, conditionals=conditionals)
schema_for_copying = json.dumps(generated_schema, indent=2)
schema_with_lines = "\n".join(f"{i+1}: {line}" for i, line in enumerate(schema_for_copying.splitlines()))
schema_result = schema_with_lines
except ValueError as e:
error_message = f"Error processing input: {e}"
return render_template('json_schema_generator.html', json_input=json_input, conditionals=conditionals, schema_result=schema_result, schema_for_copying=schema_for_copying, error_message=error_message)
# JSON Validator
@app.route('/json-tools/json-validator', methods=['GET', 'POST'])
def json_validator():
try:
validation_result = None
error_details = None
formatted_json = None
formatted_json_with_lines = None
json_input = ""
schema_input = "" # Initialize schema_input
if request.method == 'POST':
json_input = request.form.get('json_input', '')
schema_input = request.form.get('schema_input', '').strip() # Get the schema input from the form
try:
parsed_json = json.loads(json_input)
formatted_json = json.dumps(parsed_json, indent=2)
validation_result = "JSON is valid."
# If schema is provided, validate JSON against the schema
if schema_input:
parsed_schema = json.loads(schema_input)
validate(instance=parsed_json, schema=parsed_schema) # This will raise ValidationError if validation fails
validation_result += " JSON is valid against the provided schema."
formatted_json_with_lines = "\n".join(f"{i+1:3}: {line}" for i, line
in enumerate(formatted_json.splitlines()))
except json.JSONDecodeError as e:
validation_result = "Invalid JSON: The input is not a valid JSON format."
error_details = f"Error at line {e.lineno}, column {e.colno}: {e.msg}"
except ValidationError as e:
validation_result = "Invalid JSON: Schema validation error."
error_details = str(e)
except Exception as e:
validation_result = "An unexpected error occurred. Please check the JSON format and schema."
return render_template('json_validator.html',
validation_result=validation_result,
formatted_json=formatted_json,
formatted_json_with_lines=formatted_json_with_lines,
json_input=json_input, error_details=error_details,
schema_input=schema_input) # Return schema_input to the template
except Exception as e:
logger.error(f"An error occurred. {str(e)}")
# RegEx Checker
@app.route('/regex-tools/regex-checker', methods=['GET', 'POST'])
# @limiter.limit("15 per minute") # Rate limiting
def regex_checker():
match_result = None
regex_pattern = ''
test_string = ''
if request.method == 'POST':
regex_pattern = request.form.get('regex', '')
test_string = request.form.get('string', '')
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(re.fullmatch, regex_pattern, test_string)
try:
if future.result(timeout=1): # 1 second timeout for the regex operation
match_result = "Pattern matches the string."
else:
match_result = "Pattern does not match the string."
except concurrent.futures.TimeoutError:
match_result = "Execution timed out due to complex/malicious pattern."
except re.error as e:
match_result = f"Regex Error: {str(e)}"
return render_template('regex_checker.html',
match_result=match_result,
regex_pattern=regex_pattern,
test_string=test_string)
#Regex generator
@app.route('/regex-tools/regex-generator', methods=['GET', 'POST'])
def regex_generator():
try:
regex_pattern = ""
text_input = "" # Ensure text_input is defined outside the condition
if request.method == 'POST':
text_input = request.form.get('text_input', '')
# Email address pattern
if re.match(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$", text_input):
regex_pattern = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}"
# Phone number pattern (simple example, adjust as needed)
elif re.match(r"^\+?1?\d{10,15}$", text_input):
regex_pattern = "\+?1?\d{10,15}"
# Birthday pattern (in the format YYYY-MM-DD)
elif re.match(r"^\d{4}-\d{2}-\d{2}$", text_input):
regex_pattern = "\d{4}-\d{2}-\d{2}"
# SSN pattern (simple example, adjust as needed)
elif re.match(r"^\d{3}-\d{2}-\d{4}$", text_input):
regex_pattern = "\d{3}-\d{2}-\d{4}"
else:
regex_pattern = generate_basic_pattern(text_input)
return render_template('regex_generator.html',
regex_pattern=regex_pattern,
text_input=text_input)
except Exception as e:
logger.error(f"An error occurred. {str(e)}")
# JSON COnverter
@app.route('/json-tools/json-converter', methods=['GET', 'POST'])
def json_converter():
try:
input_data = ""
output_data = ""
conversion_type = "" # Determines conversion direction: 'to_json' or 'to_string'
error = None
if request.method == 'POST':
input_data = request.form.get('input_data', '')
conversion_type = request.form.get('conversion_type', '')
try:
if conversion_type == 'to_json':
# Process for converting string to JSON
# Decode the string as a raw string literal
processed_input = input_data.encode().decode('unicode_escape')
# Strip leading and trailing double quotes if they are extra
if processed_input.startswith('"') and processed_input.endswith('"'):
processed_input = processed_input[1:-1]
# Replace escaped double quotes with actual double quotes
processed_input = processed_input.replace('\\"', '"')
# Convert string to JSON
json_object = json.loads(processed_input)
# Format JSON for display
output_data = json.dumps(json_object, indent=4, sort_keys=True)
elif conversion_type == 'to_string':
# Process for converting JSON to string
# Convert the input string to a JSON object to ensure it's valid JSON
json_object = json.loads(input_data)
# Convert the JSON object back to a string with special escaping
json_string = json.dumps(json_object)
escaped_json_string = json_string.replace('"', '\\"')
# Wrap the escaped string in additional quotes
output_data = f'"{escaped_json_string}"'
else:
error = "Invalid conversion type specified."
except Exception as e:
logger.error(f"Error during conversion: {str(e)}")
error = "Error during conversion: " + str(e)
return render_template('json_converter.html',
input_data=input_data,
output_data=output_data,
error=error,
conversion_type=conversion_type)
except Exception as e:
logger.error(f"An error occurred. {str(e)}")
# Base64 Encoder Decoder
@app.route('/base64', methods=['GET', 'POST'])
def base64_encode_decode():
try:
output = ""
input_text = ""
operation = "encode" # Default operation
if request.method == 'POST':
input_text = request.form.get('input_text', '')
operation = request.form.get('operation', 'encode')
try:
if operation == 'encode':
output = base64.b64encode(input_text.encode()).decode()
elif operation == 'decode':
output = base64.b64decode(input_text.encode()).decode()
except Exception as e:
output = f"Error: {str(e)}"
return render_template('base64_encoder_decoder.html',
output=output,
input_text=input_text,
operation=operation)
except Exception as e:
logger.error(f"An error occurred. {str(e)}")
# Word/Character counter
@app.route('/string-tools/counter', methods=['GET', 'POST'])
def counter():
text_input = ''
count = 0
output = ''
filter_option = request.form.get('filter_option', 'Character')
custom_delimiter = request.form.get('custom_delimiter', '')
if request.method == 'POST':
text_input = request.form.get('text_input', '')
if filter_option == 'Character':
count = len(text_input)
output = '\n'.join(text_input)
elif filter_option == 'Word':
count = len(text_input.split())
output = '\n'.join(text_input.split())
elif filter_option == 'Line':
count = len(text_input.split('\n'))
output = text_input
elif filter_option == 'Custom Delimiter' and custom_delimiter:
count = len(text_input.split(custom_delimiter))
output = custom_delimiter.join(text_input.split(custom_delimiter))
return render_template('counter.html', text_input=text_input, count=count, output=output, filter_option=filter_option, custom_delimiter=custom_delimiter)
# Time Converter
@app.route('/time-converter', methods=['GET', 'POST'])
def time_converter():
converted_time = ""
output = {}
if request.method == 'POST':
time_input = request.form.get('time_input', '')
try:
# Initialize input_time as None for other time formats handling
input_time = None
# Specific handling for PostgreSQL timestamp format
if " " in time_input and "-" in time_input and ":" in time_input:
# Parse PostgreSQL timestamp
input_time = datetime.strptime(time_input, '%Y-%m-%d %H:%M:%S')
# Localize to Eastern Time considering DST
eastern = pytz.timezone('America/New_York')
input_time = eastern.localize(input_time, is_dst=None)
# Format the output, appending 'Z' to denote Eastern Time handling
converted_time = input_time.strftime('%Y-%m-%dT%H:%M:%S') + 'Z'
# Handling for ISO 8601 format
if input_time is None:
try:
input_time = datetime.fromisoformat(time_input)
# Convert to Eastern Time
input_time = input_time.astimezone(pytz.timezone('America/New_York'))
converted_time = input_time.strftime('%Y-%m-%dT%H:%M:%S') + 'Z'
except ValueError:
pass # If parsing fails, proceed to next format
# Handling for Epoch time (seconds then milliseconds)
if input_time is None:
try:
# First, try assuming seconds
input_time = datetime.fromtimestamp(float(time_input), tz=pytz.utc)
except ValueError:
try:
# Then try milliseconds
input_time = datetime.fromtimestamp(float(time_input) / 1000, tz=pytz.utc)
except ValueError:
pass
if input_time:
# Convert to Eastern Time
input_time = input_time.astimezone(pytz.timezone('America/New_York'))
converted_time = input_time.strftime('%Y-%m-%dT%H:%M:%S') + 'Z'
if not converted_time:
raise ValueError("Input time format not recognized.")
if input_time:
et_time = input_time.astimezone(pytz.timezone('America/New_York'))
output['Local Time'] = et_time.strftime('%m/%d/%Y, %I:%M:%S %p')
output['UTC Time'] = et_time.astimezone(pytz.utc).strftime('%a, %d %b %Y %H:%M:%S GMT')
# output['Relative Time'] = "Use JavaScript for live relative time"
output['UNIX Time'] = str(int(et_time.timestamp()))
output['Day of week'] = et_time.strftime('%A')
output['Day of year'] = str(et_time.timetuple().tm_yday)
output['Is leap year?'] = "Yes" if et_time.year % 4 == 0 and (et_time.year % 100 != 0 or et_time.year % 400 == 0) else "No"
output['Other date formats (local time)'] = [
et_time.strftime('%Y-%m-%d'),
et_time.strftime('%m-%d-%Y'),
et_time.strftime('%Y/%m/%d'),
et_time.strftime('%m/%d/%Y'),
et_time.strftime('%a %B %d, %Y'),
et_time.strftime('%A %B %d, %Y'),
et_time.strftime('%a %b %d, %Y'),
et_time.strftime('%A %b %d, %Y'),
]
# Append original format
output['ISO Format'] = input_time.strftime('%Y-%m-%dT%H:%M:%S') + 'Z'
except Exception as e:
converted_time = f"Error converting time: {str(e)}"
return render_template('time_converter.html', output=output)
# JSON Parser - beautify JSON
@app.route('/json-tools/json-parser', methods=['GET', 'POST'])
def json_parser():
input_json = ""
output_json = ""
error = None
if request.method == 'POST':
input_json = request.form.get('input_json', '')
try:
# Parse the input JSON to ensure it's valid
parsed_json = json.loads(input_json)
# Beautify the JSON
output_json = json.dumps(parsed_json, indent=4)
except json.JSONDecodeError as e:
error = f"Invalid JSON: {e.msg} at line {e.lineno}, column {e.colno}"
return render_template('json_parser.html',
input_json=input_json,
output_json=output_json,
error=error)
# Generate JSON from JSON schema
@app.route('/json-tools/json-sample-generator', methods=['GET', 'POST'])
# @limiter.limit("15 per minute") # Rate limiting
def json_sample_generator():
sample_data = None
schema_input = ""
if request.method == 'POST':
schema_input = request.form.get('schema_input', '').strip()
if schema_input:
try:
sample_data = json.dumps(generate_sample_data(json.loads(schema_input)), indent=4)
except Exception as e:
sample_data = f"Error generating sample data: {str(e)}"
return render_template('json_sample_generator.html', schema_input=schema_input, sample_data=sample_data or "")
# CRON Expression generator
@app.route('/schedule_cron', methods=['GET', 'POST'])
def schedule_cron():
if request.method == 'POST':
# Process the form data and generate CRON expression
minute = request.form.get('minute')
hour = request.form.get('hour')
day_of_month = request.form.get('day_of_month')
month = request.form.get('month')
day_of_week = request.form.get('day_of_week')
cron_expression = f"{minute} {hour} {day_of_month} {month} {day_of_week}"
# You can then save this expression, display it, or use it as needed
return make_response(f"CRON Expression: {escape(cron_expression)}")
else:
# Display the form
return render_template('cron_scheduler.html')
# Random string generator
@app.route('/string-tools/random-string-generator', methods=['GET', 'POST'])
def random_string_generator():
random_string = ''
original_text = ''
if request.method == 'POST':
length = int(request.form.get('length', 16))
characters = string.ascii_letters + string.digits + string.punctuation
random_string = ''.join(random.choice(characters) for _ in range(length))
original_text = request.form.get('length', '16')
return render_template('random_string_generator.html', random_string=random_string, original_text=original_text)
# Random number generator
@app.route('/string-tools/random-number-generator', methods=['GET', 'POST'])
def random_number_generator():
random_number = None
original_min = ''
original_max = ''
if request.method == 'POST':
min_val = int(request.form.get('min_val', 0))
max_val = int(request.form.get('max_val', 100))
random_number = random.randint(min_val, max_val)
original_min = request.form.get('min_val', '0')
original_max = request.form.get('max_val', '100')
return render_template('random_number_generator.html', random_number=random_number, original_min=original_min, original_max=original_max)
# Shuffle letters
@app.route('/string-tools/shuffle-letters', methods=['GET', 'POST'])
def shuffle_letters():
shuffled_text = ''
original_text = ''
if request.method == 'POST':
text = request.form.get('text', '')
shuffled_text = ''.join(random.sample(text, len(text)))
original_text = text
return render_template('shuffle_letters.html', shuffled_text=shuffled_text, original_text=original_text)
# Clean text
@app.route('/string-tools/clean-text', methods=['GET', 'POST'])
def clean_text():
cleaned_text = ''
original_text = ''
if request.method == 'POST':
text = request.form.get('text', '')
cleaned_text = re.sub(r'\s+', ' ', text).strip()
cleaned_text = re.sub(r'([.!?])\s*', r'\1 ', cleaned_text)
sentences = re.split(r'([.!?] )', cleaned_text)
cleaned_text = ''.join([s.capitalize() if i % 2 == 0 else s for i, s in enumerate(sentences)])
original_text = text
return render_template('clean_text.html', cleaned_text=cleaned_text, original_text=original_text)
# Text statistics
@app.route('/string-tools/text-statistics', methods=['GET', 'POST'])
def text_statistics():
stats = {}
original_text = ''
if request.method == 'POST':
text = request.form.get('text', '')
original_text = text
words = text.split()
num_words = len(words)
num_chars = len(text)
num_chars_no_space = len(text.replace(' ', ''))
num_lines = text.count('\n') + 1
num_sentences = len(re.findall(r'[.!?]', text))
word_lengths = [len(word) for word in words]
unique_words = set(words)
stats = {
'num_chars': num_chars,
'num_chars_no_space': num_chars_no_space,
'num_lines': num_lines,
'num_words': num_words,
'num_sentences': num_sentences,
'num_unique_words': len(unique_words),
'percent_unique_words': (len(unique_words) / num_words) * 100 if num_words > 0 else 0,
'length_shortest_word': min(word_lengths) if words else 0,
'length_longest_word': max(word_lengths) if words else 0,
'avg_word_length': sum(word_lengths) / num_words if num_words > 0 else 0
}
return render_template('text_statistics.html', stats=stats, original_text=original_text)
# Column extractor
@app.route('/string-tools/column-extractor', methods=['GET', 'POST'])
def column_extractor():
extracted_columns = []
original_text = ''
original_column_number = ''
original_delimiter = ''
if request.method == 'POST':
text = request.form.get('text', '')
column_number = int(request.form.get('column_number', 1)) - 1
delimiter = request.form.get('delimiter', ',')
lines = text.split('\n')
for line in lines:
columns = line.split(delimiter)
if len(columns) > column_number:
extracted_columns.append(columns[column_number].strip())
original_text = text
original_column_number = request.form.get('column_number', '1')
original_delimiter = request.form.get('delimiter', ',')
return render_template('column_extractor.html', extracted_columns=extracted_columns, original_text=original_text, original_column_number=original_column_number, original_delimiter=original_delimiter)
@app.route('/jwt_viewer', methods=['GET', 'POST'])
def jwt_viewer():
decoded_jwt = None
error = None
if request.method == 'POST':
jwt_token = request.form.get('jwt_token')
secret_key = request.form.get('secret_key')
try:
decoded_jwt = jwt.decode(jwt_token, secret_key, algorithms=["HS256"])
except jwt.ExpiredSignatureError:
error = "Token has expired"
except jwt.InvalidTokenError:
error = "Invalid token"
return render_template('jwt_viewer.html', decoded_jwt=decoded_jwt, error=error)
@app.route('/fake-data-generator', methods=['GET', 'POST'])
def fake_data_generator():
preview_data = None
field_names = []
field_types = []
num_records = 10
if request.method == 'POST':
field_names = request.form.getlist('field_names[]')
field_types = request.form.getlist('field_types[]')
num_records = int(request.form.get('num_records', 10))
preview_data = generate_fake_data(field_names, field_types, num_records)
return render_template('fake_data_generator.html',
preview_data=preview_data,
field_names=field_names,
field_types=field_types,
num_records=num_records)
@app.route('/export-fake-data', methods=['POST'])
def export_fake_data():
try:
# Get generated data directly from the session
field_names = request.form.getlist('field_names[]')
field_types = request.form.getlist('field_types[]')
num_records = int(request.form.get('num_records', 10))
# Generate fresh data
data = generate_fake_data(field_names, field_types, num_records)
export_format = request.form.get('format', 'json')
if export_format == 'json':
response = make_response(json.dumps(data, indent=2))
response.headers['Content-Type'] = 'application/json'
response.headers['Content-Disposition'] = 'attachment; filename=fake_data.json'
else:
si = StringIO()
writer = csv.DictWriter(si, fieldnames=field_names)
writer.writeheader()
writer.writerows(data)
response = make_response(si.getvalue())
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = 'attachment; filename=fake_data.csv'
return response
except Exception as e:
logger.error(f"Export error: {str(e)}")
return "An error occurred while generating the export. Please try again.", 400
@app.route('/markdown-pdf-converter', methods=['GET', 'POST'])
def markdown_pdf_converter():
if request.method == 'POST':
conversion_type = request.form.get('conversion_type')
try:
if conversion_type == 'md_to_pdf':
markdown_text = request.form.get('markdown_text', '')
html = markdown.markdown(markdown_text)
# pdf_path = tempfile.mktemp(suffix='.pdf')
pdf_file = tempfile.NamedTemporaryFile(suffix='.pdf', delete=False)
pdf_path = pdf_file.name
pdf_file.close()
doc = SimpleDocTemplate(pdf_path, pagesize=letter)
styles = getSampleStyleSheet()
# Custom styles
# styles.add(ParagraphStyle(
# 'Bullet',
# parent=styles['Normal'],
# leftIndent=20,
# spaceAfter=10
# ))
story = []
lines = markdown_text.split('\n')
current_list_items = []
in_code_block = False
code_buffer = []
for line in lines:
if line.strip().startswith('```'):
in_code_block = not in_code_block
if not in_code_block and code_buffer:
# Add code block to story
code_style = ParagraphStyle(
'Code',
parent=styles['Code'],
fontName='Courier',
fontSize=8,
leading=10,
leftIndent=20,
backgroundColor=colors.lightgrey
)
code_text = '\n'.join(code_buffer)
story.append(Paragraph(code_text, code_style))
code_buffer = []
continue
if in_code_block:
code_buffer.append(line)
continue
if line.strip():
if line.startswith('# '):
if current_list_items:
story.append(ListFlowable(
current_list_items,
bulletType='bullet',
leftIndent=35,
spaceBefore=10,
spaceAfter=10
))
current_list_items = []
story.append(Paragraph(line[2:], styles['Title']))
elif line.startswith('## '):
if current_list_items:
story.append(ListFlowable(current_list_items,
bulletType='bullet',
leftIndent=35,
spaceBefore=10,
spaceAfter=10
))
current_list_items = []
story.append(Paragraph(line[3:], styles['Heading2']))
elif line.startswith('### '):
if current_list_items:
story.append(ListFlowable(current_list_items,
bulletType='bullet',
leftIndent=35,
spaceBefore=10,
spaceAfter=10
))
current_list_items = []
story.append(Paragraph(line[4:], styles['Heading3']))
elif line.startswith('- ') or line.startswith('* '):
current_list_items.append(
ListItem(Paragraph(line[2:], styles['BodyText']))
)
else:
if current_list_items:
story.append(ListFlowable(
current_list_items,
bulletType='bullet',
leftIndent=35,
spaceBefore=10,
spaceAfter=10
))
current_list_items = []
story.append(Paragraph(line, styles['Normal']))
story.append(Spacer(1, 6))
# Add any remaining list items
if current_list_items:
story.append(ListFlowable(
current_list_items,
bulletType='bullet',
leftIndent=35,
spaceBefore=10,
spaceAfter=10
))
doc.build(story)
session['pdf_path'] = pdf_path
return render_template('markdown_pdf.html',
output_text="PDF generated successfully",
markdown_text=markdown_text,
conversion_type=conversion_type
)
else: # pdf_to_md
if 'pdf_file' not in request.files:
raise ValueError("No file uploaded")
pdf_file = request.files['pdf_file']
pdf_reader = PdfReader(pdf_file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
return render_template('markdown_pdf.html',
output_text=text,
conversion_type=conversion_type
)
except Exception as e:
logger.error(f"Conversion error: {str(e)}")
return render_template('markdown_pdf.html',
error="An error occurred during conversion",
markdown_text=markdown_text if conversion_type == 'md_to_pdf' else '',
conversion_type=conversion_type
)
return render_template('markdown_pdf.html')
@app.route('/download-pdf')
def download_pdf():
try:
pdf_path = session.get('pdf_path')
if not pdf_path:
raise ValueError("No PDF file generated")
with open(pdf_path, 'rb') as f:
pdf_data = f.read()
# Clean up temp file
os.unlink(pdf_path)
session.pop('pdf_path', None)
response = make_response(pdf_data)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'attachment; filename=output.pdf'
return response
except Exception as e:
logger.error(f"Download error: {str(e)}")
return "Error downloading PDF", 400
if __name__ == '__main__':
app.run(port=5001)