Skip to content
Snippets Groups Projects
html_md.sh 3 KiB
Newer Older
  • Learn to ignore specific revisions
  • Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    #!/bin/sh
    
    if [ "$1" = "-d" ]; then
    	# delete all .md files
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    	if [ "$2" = "-html" ]; then
    		# delete all .html files
    		find . -name "*.ht*" | 
    		while read i; 
    		do
    			rm "$i"; 	
    			echo "$i deleted"; 
    		done
    	fi
    	if [ "$2" = "-md" ]; then
    		# delete all .md files
    		find . -name "*.md" | 
    		while read i; 
    		do
    			rm "$i"; 		
    			echo "$i deleted"; 
    		done
    	fi
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    fi
    if [ "$1" = "-w" ]; then
    	# download html pages 
    	wget -bqc --mirror --convert-links --adjust-extension --page-requisites  --no-parent https://docs.it4i.cz;
    	pid=$(pgrep wget);
    	
    	FILE="docs.it4i.cz/@@search?sort_on=sortable_title&.html"
    
    	# test exists file -> yes - kill process wget
    
    	until test -f $FILE
    	do
    	if [ ! -f "$FILE" ]; then
    		echo "Download..."			 
    	fi
    	sleep 5
    	done
    	
    	echo "Download complete..."
    	kill $pid
    
    	find . -name "@@s*" | 
    	while read i; 
    	do
    		rm "$i"; 
    	done
    	
    fi
    if [ "$1" = "-c" ]; then
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    	# delete all .md files
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    	find . -name "*.md" | 
    	while read i; 
    	do
    		rm "$i"; 
    		echo "$i deleted"; 
    	done
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    
    	# create folder info
    	mkdir info;
    
    	find . -name "*.png" | 
    	while read i; 
    	do		
    		echo "$i" >> ./info/list_png.txt; 
    	done
    
    	find . -name "*.jpg" | 
    	while read i; 
    	do		
    		echo "$i" >> ./info/list_jpg.txt; 
    	done
    	
    
    	# html -> md
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    	find . -name "*.ht*" | 
    	while read i; 
    	do 
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    		# filtering html
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    		echo "$i"; 
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    		echo "\t\tfiltering html files...";
    		
    		HEAD=$(cat "$i" | grep -n -m1 '<h1' |cut -f1 -d: | tr --delete '\n')
    		END=$(cat "$i" | grep -n -m1 '<!-- <div tal:content=' |cut -f1 -d: | tr --delete '\n')
    		LAST=$(cat "$i" | wc -l | tr --delete '\n')
    		DOWN=$((LAST-END+2))
    
    		cat "$i" | sed '1,'"$((HEAD-1))"'d' | sed -n -e :a -e '1,'"$DOWN"'!{P;N;D;};N;ba' > "${i%.*}TMP.html"	
    
    		# converted .html to .md
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    		echo "\t\t.html -> .md"
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    		pandoc -f html -t markdown+pipe_tables-grid_tables "${i%.*}TMP.html" -o "${i%.*}.md"; 
    		rm "${i%.*}TMP.html";
    
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    		# folder info, file strukture, list of all files and his addres into folders
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    		echo "${i%.*}" >> ./info/files_md.txt;
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    
    		# create filter_auto
    		cat "${i%.*}.md" | grep -o -P '(?<={).*(?=})' | sort -u | sed '/{/d' | sed '/\$/d' >> filter_auto;
    		sort -u filter_auto -o filter_auto; 
    
    		# exceptions filter_auto
    		cat exceptions_filter_auto | 
    		while read y; 
    		do 
    			# search and delete according with filter_auto
    			cat filter_auto | sed -e 's/'"$y"'//g' > filter_autoTMP;
    			cat filter_autoTMP > filter_auto;
    		done
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    	
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    		# text filtering of html, css, ...
    		echo "\t\tautomatic filter..."
    		cat filter_auto | 
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    		while read y; 
    		do 
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    			# search and delete according with filter_auto
    			cat "${i%.*}.md" | sed -e 's/{'"$y"'}//g' | sed -e 's/\\//g' | sed -e 's/: //g' | sed -e 's/<\/div>//g' | sed '/^<div/d'  | sed '/^$/d' > "${i%.*}TMP.md";
    			cat "${i%.*}TMP.md" > "${i%.*}.md";
    		done
    
    		echo "\t\tother filter..."
    		cat filter_other | 
    		while read a; 
    		do 
    			# search and delete according with filter_other
    			cat "${i%.*}.md" | sed -e 's/'"$a"'//g'  > "${i%.*}TMP.md";
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    			cat "${i%.*}TMP.md" > "${i%.*}.md";
    		done
    
    		# delete temporary files
    		rm "${i%.*}TMP.md";
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    	done
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    	rm filter_autoTMP
    	rm filter_auto