Skip to content

Commit ecaf5b8

Browse files
committed
fokin github
1 parent aae2a9b commit ecaf5b8

File tree

1 file changed

+116
-120
lines changed

1 file changed

+116
-120
lines changed

fssg

Lines changed: 116 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ else
111111
RED=""; GREEN=""; YELLOW=""; BLUE=""; NC=""
112112
fi
113113

114-
# Prepare output directory
115-
rm -rf dist 2>/dev/null
116-
mkdir -p dist
117-
log_verbose "Created fresh 'dist' directory."
118-
119114
# Start local Mongoose server
120115
start_mongoose() {
121116
# Detect OS
@@ -1082,143 +1077,144 @@ process_post_directives() {
10821077

10831078
# Main processing loop for source files
10841079
build() {
1085-
while IFS= read -r file; do
1086-
JOB_COUNT=$((JOB_COUNT + 1))
1087-
wait_for_slot
1088-
(
1089-
ext="${file##*.}"
1090-
rel_path="${file#src/}"
1091-
out_file="dist/${rel_path%.*}.html"
1092-
mkdir -p "$(dirname "$out_file")"
1093-
log_verbose "Processing $file$out_file"
1094-
1095-
base_name=$(basename "$file" ."$ext")
1096-
dynamic_title=$(printf '%s\n' "$base_name" | tr '-' ' ' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2)); print}')
1097-
1098-
temp_raw_content="$out_file.tmp-raw"
1099-
case "$ext" in
1100-
md)
1101-
if ! parse_markdown <"$file" >"$temp_raw_content" 2>"$out_file.md-err"; then
1102-
log_warning "Custom Markdown conversion failed for $file. Error: $(cat "$out_file.md-err") Skipping."
1103-
rm -f "$temp_raw_content" "$out_file.md-err"
1104-
continue
1105-
fi
1106-
rm -f "$out_file.md-err"
1107-
;;
1108-
html) cp "$file" "$temp_raw_content" ;;
1109-
esac
1080+
find src \( -path src/includes -o -name template.html -o -path src/static \) -prune -o -type f \( -name '*.md' -o -name '*.html' \) -print |
1081+
while IFS= read -r file; do
1082+
JOB_COUNT=$((JOB_COUNT + 1))
1083+
wait_for_slot
1084+
(
1085+
ext="${file##*.}"
1086+
rel_path="${file#src/}"
1087+
out_file="dist/${rel_path%.*}.html"
1088+
mkdir -p "$(dirname "$out_file")"
1089+
log_verbose "Processing $file$out_file"
1090+
1091+
base_name=$(basename "$file" ."$ext")
1092+
dynamic_title=$(printf '%s\n' "$base_name" | tr '-' ' ' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2)); print}')
1093+
1094+
temp_raw_content="$out_file.tmp-raw"
1095+
case "$ext" in
1096+
md)
1097+
if ! parse_markdown <"$file" >"$temp_raw_content" 2>"$out_file.md-err"; then
1098+
log_warning "Custom Markdown conversion failed for $file. Error: $(cat "$out_file.md-err") Skipping."
1099+
rm -f "$temp_raw_content" "$out_file.md-err"
1100+
continue
1101+
fi
1102+
rm -f "$out_file.md-err"
1103+
;;
1104+
html) cp "$file" "$temp_raw_content" ;;
1105+
esac
11101106

1111-
if [ ! -f "$temp_raw_content" ]; then
1112-
log_warning "Temporary raw file $temp_raw_content not found for $file. Skipping."
1113-
continue
1114-
fi
1107+
if [ ! -f "$temp_raw_content" ]; then
1108+
log_warning "Temporary raw file $temp_raw_content not found for $file. Skipping."
1109+
continue
1110+
fi
11151111

1116-
current_content_for_directives="$out_file.tmp-directives-processed"
1117-
awk '
1118-
BEGIN {
1119-
in_comment = 0
1120-
disabled_open_brace = "{^{"
1121-
disabled_close_brace = "}^}"
1122-
}
1123-
{
1124-
current_line_text = $0
1125-
output_buffer = ""
1126-
while (length(current_line_text) > 0) {
1127-
if (in_comment) {
1128-
comment_end_pos = index(current_line_text, "-->")
1129-
if (comment_end_pos > 0) {
1130-
text_inside_comment = substr(current_line_text, 1, comment_end_pos - 1)
1131-
gsub(/\{\{/, disabled_open_brace, text_inside_comment)
1132-
gsub(/\}\}/, disabled_close_brace, text_inside_comment)
1133-
output_buffer = output_buffer text_inside_comment "-->"
1134-
current_line_text = substr(current_line_text, comment_end_pos + 3)
1135-
in_comment = 0
1136-
} else {
1137-
text_inside_comment = current_line_text
1138-
gsub(/\{\{/, disabled_open_brace, text_inside_comment)
1139-
gsub(/\}\}/, disabled_close_brace, text_inside_comment)
1140-
output_buffer = output_buffer text_inside_comment
1141-
current_line_text = ""
1142-
}
1143-
} else {
1144-
comment_start_pos = index(current_line_text, "<!--")
1145-
if (comment_start_pos > 0) {
1146-
text_before_comment = substr(current_line_text, 1, comment_start_pos - 1)
1147-
output_buffer = output_buffer text_before_comment "<!--"
1148-
current_line_text = substr(current_line_text, comment_start_pos + 4)
1149-
in_comment = 1
1112+
current_content_for_directives="$out_file.tmp-directives-processed"
1113+
awk '
1114+
BEGIN {
1115+
in_comment = 0
1116+
disabled_open_brace = "{^{"
1117+
disabled_close_brace = "}^}"
1118+
}
1119+
{
1120+
current_line_text = $0
1121+
output_buffer = ""
1122+
while (length(current_line_text) > 0) {
1123+
if (in_comment) {
1124+
comment_end_pos = index(current_line_text, "-->")
1125+
if (comment_end_pos > 0) {
1126+
text_inside_comment = substr(current_line_text, 1, comment_end_pos - 1)
1127+
gsub(/\{\{/, disabled_open_brace, text_inside_comment)
1128+
gsub(/\}\}/, disabled_close_brace, text_inside_comment)
1129+
output_buffer = output_buffer text_inside_comment "-->"
1130+
current_line_text = substr(current_line_text, comment_end_pos + 3)
1131+
in_comment = 0
1132+
} else {
1133+
text_inside_comment = current_line_text
1134+
gsub(/\{\{/, disabled_open_brace, text_inside_comment)
1135+
gsub(/\}\}/, disabled_close_brace, text_inside_comment)
1136+
output_buffer = output_buffer text_inside_comment
1137+
current_line_text = ""
1138+
}
11501139
} else {
1151-
output_buffer = output_buffer current_line_text
1152-
current_line_text = ""
1140+
comment_start_pos = index(current_line_text, "<!--")
1141+
if (comment_start_pos > 0) {
1142+
text_before_comment = substr(current_line_text, 1, comment_start_pos - 1)
1143+
output_buffer = output_buffer text_before_comment "<!--"
1144+
current_line_text = substr(current_line_text, comment_start_pos + 4)
1145+
in_comment = 1
1146+
} else {
1147+
output_buffer = output_buffer current_line_text
1148+
current_line_text = ""
1149+
}
11531150
}
11541151
}
1152+
print output_buffer
11551153
}
1156-
print output_buffer
1157-
}
1158-
' "$temp_raw_content" > "$current_content_for_directives"
1159-
rm -f "$temp_raw_content"
1154+
' "$temp_raw_content" > "$current_content_for_directives"
1155+
rm -f "$temp_raw_content"
11601156

1161-
title_override=$(awk '
1162-
{
1163-
prefix_regex = "\\{\\{[[:space:]]*[Tt][Ii][Tt][Ll][Ee]:[[:space:]]*"
1157+
title_override=$(awk '
1158+
{
1159+
prefix_regex = "\\{\\{[[:space:]]*[Tt][Ii][Tt][Ll][Ee]:[[:space:]]*"
11641160
1165-
if (match($0, prefix_regex)) {
1166-
content_and_suffix = substr($0, RSTART + RLENGTH)
1161+
if (match($0, prefix_regex)) {
1162+
content_and_suffix = substr($0, RSTART + RLENGTH)
11671163
1168-
if (match(content_and_suffix, "\\}\\}")) {
1169-
title_val = substr(content_and_suffix, 1, RSTART - 1)
1170-
gsub(/^[[:space:]]+|[[:space:]]+$/, "", title_val)
1171-
print title_val
1172-
exit
1164+
if (match(content_and_suffix, "\\}\\}")) {
1165+
title_val = substr(content_and_suffix, 1, RSTART - 1)
1166+
gsub(/^[[:space:]]+|[[:space:]]+$/, "", title_val)
1167+
print title_val
1168+
exit
1169+
}
11731170
}
11741171
}
1175-
}
1176-
' "$current_content_for_directives")
1172+
' "$current_content_for_directives")
11771173

1178-
temp_after_title_removal="$out_file.tmp-title-removed"
1179-
sed 's/{{[[:space:]]*[Tt][Ii][Tt][Ll][Ee]:[^}]*}}//g' "$current_content_for_directives" > "$temp_after_title_removal"
1180-
mv "$temp_after_title_removal" "$current_content_for_directives"
1174+
temp_after_title_removal="$out_file.tmp-title-removed"
1175+
sed 's/{{[[:space:]]*[Tt][Ii][Tt][Ll][Ee]:[^}]*}}//g' "$current_content_for_directives" > "$temp_after_title_removal"
1176+
mv "$temp_after_title_removal" "$current_content_for_directives"
11811177

1182-
page_id="${rel_path%.*}.html"
1183-
current_title="${title_override:-$dynamic_title}"
1184-
log_verbose "Title for $page_id -> \"$current_title\""
1178+
page_id="${rel_path%.*}.html"
1179+
current_title="${title_override:-$dynamic_title}"
1180+
log_verbose "Title for $page_id -> \"$current_title\""
11851181

1186-
temp_after_template="$out_file.tmp-template"
1187-
apply_template "$current_content_for_directives" "$temp_after_template" "$current_title"
1188-
rm -f "$current_content_for_directives"
1182+
temp_after_template="$out_file.tmp-template"
1183+
apply_template "$current_content_for_directives" "$temp_after_template" "$current_title"
1184+
rm -f "$current_content_for_directives"
11891185

1190-
temp_after_conditionals="$out_file.tmp-cond"
1191-
process_conditionals "$temp_after_template" "$temp_after_conditionals" "$page_id" "$ext"
1192-
rm -f "$temp_after_template"
1186+
temp_after_conditionals="$out_file.tmp-cond"
1187+
process_conditionals "$temp_after_template" "$temp_after_conditionals" "$page_id" "$ext"
1188+
rm -f "$temp_after_template"
11931189

1194-
if [ ! -f "$temp_after_conditionals" ]; then
1195-
log_error "Conditional processing failed for $file. Output file $temp_after_conditionals not created."
1196-
rm -f "$current_content_for_directives" "$temp_after_template"
1197-
continue
1198-
fi
1190+
if [ ! -f "$temp_after_conditionals" ]; then
1191+
log_error "Conditional processing failed for $file. Output file $temp_after_conditionals not created."
1192+
rm -f "$current_content_for_directives" "$temp_after_template"
1193+
continue
1194+
fi
11991195

1200-
temp_after_includes="$out_file.tmp-includes"
1201-
process_includes "$temp_after_conditionals" "$temp_after_includes"
1202-
rm -f "$temp_after_conditionals"
1196+
temp_after_includes="$out_file.tmp-includes"
1197+
process_includes "$temp_after_conditionals" "$temp_after_includes"
1198+
rm -f "$temp_after_conditionals"
12031199

1204-
temp_after_posts="$out_file.tmp-posts"
1205-
process_post_directives "$temp_after_includes" "$temp_after_posts"
1206-
rm -f "$temp_after_includes"
1200+
temp_after_posts="$out_file.tmp-posts"
1201+
process_post_directives "$temp_after_includes" "$temp_after_posts"
1202+
rm -f "$temp_after_includes"
12071203

1208-
temp_after_styles="$out_file.tmp-styles"
1209-
process_styles "$temp_after_posts" "$temp_after_styles"
1210-
rm -f "$temp_after_posts"
1204+
temp_after_styles="$out_file.tmp-styles"
1205+
process_styles "$temp_after_posts" "$temp_after_styles"
1206+
rm -f "$temp_after_posts"
12111207

1212-
temp_no_comments="$out_file.tmp-no-comments"
1213-
sed '/<!--.*-->/d;/^[[:space:]]*$/d' "$temp_after_styles" > "$temp_no_comments"
1208+
temp_no_comments="$out_file.tmp-no-comments"
1209+
sed '/<!--.*-->/d;/^[[:space:]]*$/d' "$temp_after_styles" > "$temp_no_comments"
12141210

1215-
intended_final_filename="dist/${rel_path%.*}.html"
1216-
process_scripts "$temp_no_comments" "$intended_final_filename"
1217-
rm -f "$temp_after_styles" "$temp_no_comments"
1211+
intended_final_filename="dist/${rel_path%.*}.html"
1212+
process_scripts "$temp_no_comments" "$intended_final_filename"
1213+
rm -f "$temp_after_styles" "$temp_no_comments"
12181214

1219-
log "Created $intended_final_filename"
1220-
) &
1221-
done < <(find src \( -path src/includes -o -name template.html \) -prune -o -type f \( -name '*.md' -o -name '*.html' \) -print)
1215+
log "Created $intended_final_filename"
1216+
) &
1217+
done
12221218

12231219
wait # Wait for all background jobs to finish
12241220
JOB_COUNT=0

0 commit comments

Comments
 (0)