2 Commits dc242ac5fa ... f634241dff

Author SHA1 Message Date
  Farooq Karimi Zadeh f634241dff Using mustache & adding files section 3 years ago
  Farooq Karimi Zadeh e6ddf025c1 Updating templates to use mustache 3 years ago
5 changed files with 187 additions and 76 deletions
  1. 89 59
      fobil.lisp
  2. 61 0
      templates/files.html
  3. 22 8
      templates/index.html
  4. 7 4
      templates/page.html
  5. 8 5
      templates/post.html

+ 89 - 59
fobil.lisp

@@ -4,10 +4,25 @@
 (ql:quickload :str)
 (ql:quickload :3bmd)
 (ql:quickload :3bmd-ext-code-blocks)
-(ql:quickload :cl-ppcre)
+(ql:quickload :cl-mustache)
+(ql:quickload :osicat)
+(ql:quickload :jonathan)
+
 (setf 3bmd:*smart-quotes* t)
 (setf 3bmd-code-blocks:*code-blocks* t)
 
+(defun random-color ()
+  ;; A list of W3.css colors... change this if you
+  ;; use some other CSS framework...
+  (nth (random 28)
+       '("red" "purple" "indigo" "light-blue" "aqua"
+	 "green" "lime" "khaki" "amber" "deep-orange"
+	 "brown" "gray" "pale-red" "pale-green"
+	 "pink" "deep-purple" "blue" "cyan" "teal"
+	 "light-green" "sand" "yellow" "orange"
+	 "blue-gray" "light-gray" "dark-gray"
+	 "pale-yellow" "pale-blue")))
+
 (defun convert-to-html (md)
   (with-output-to-string (stream)
     (3bmd:parse-string-and-print-to-stream md stream)))
@@ -31,30 +46,26 @@
 (defun join-links (links)
   (str:join " | " links))
 
-(defun replace-multi (s &rest olds-news)
-  (loop for old-new in olds-news
-	do (setf s (str:replace-all (first old-new) (second old-new) s)))
-  s)
-
-(defun find-summary-template (html)
-  (first
-   (cl-ppcre:all-matches-as-strings
-    "(\\$\\$FOREACH\\$\\$(\\n(\\s*((?!\\$\\$END\\$\\$).)*\\n?)*)\\$\\$END\\\$\\$)"
-    html)))
+(defun get-file-size (path)
+  (let ((stat (osicat-posix:stat path)))
+    (osicat-posix:stat-size stat)))
 
-(defun extract-index-post-template (html)
-  (replace-multi (find-summary-template html)
-   (list "$$FOREACH$$" "")
-   (list "$$END$$" "")))
+(defun get-human-readable-size (size)
+  (let ((units '("B" "KB" "MB" "GB" "TB"))
+	(unit 0))
+    (loop when (< size 1024)
+	    return (str:concat
+		    (with-output-to-string (out) (format out "~a" (round size)))
+		    (nth unit units))
+	  do (setf size (/ size 1024))
+	     (incf unit))))
 
 (defun fobil ()
   (let* ((posts (uiop:directory-files #P"./posts/"))
 	 (pages (uiop:directory-files #P"./pages/"))
-	 (post-template (uiop:read-file-string #P"./templates/post.html"))
-	 (page-template (uiop:read-file-string #P"./templates/page.html"))
-	 (index-template (uiop:read-file-string #P"./templates/index.html"))
-	 (post-summary-template (extract-index-post-template
-				 index-template))
+	 (post-template (mustache:compile-template #P"./templates/post.html"))
+	 (page-template (mustache:compile-template #P"./templates/page.html"))
+	 (index-template (mustache:compile-template #P"./templates/index.html"))
 	 (pages-html (loop for page in pages
 			   when (markdown? page)
 			     collect (convert-to-html
@@ -64,21 +75,20 @@
 			     collect (convert-to-html
 				      (uiop:read-file-string post))))
 	 (pages-names (loop for page in pages
-			    collect (str:concat
-				     (pathname-name page)
-				     ".html")))
+			    when (markdown? page)
+			      collect (str:concat
+				       (pathname-name page) ".html")))
 	 (pages-titles (loop for page-html in pages-html
 			     collect (get-title page-html)))
 	 (posts-titles (loop for post-html in posts-html
 			     collect (get-title post-html)))
-	 (index-posts "")
 	 (links-for-index '())
 	 (links-for-posts '())
 	 (links-for-pages '())
 	 (posts-names (loop for post in posts
-			    collect (str:concat
-				     (pathname-name post)
-				     ".html"))))
+			    when (markdown? post)
+			      collect (str:concat
+				       (pathname-name post) ".html"))))
     (ensure-directories-exist "./out/")
     (ensure-directories-exist "./out/pages/")
     (ensure-directories-exist "./out/posts/")
@@ -100,41 +110,61 @@
 	  with links = (join-links links-for-pages)
 	  do (str:to-file
 	      (str:concat "./out/pages/" page-name)
-	      (replace-multi page-template
-			     (list "$$TITLE$$" page-title)
-			     (list "$$CONTENT$$" page-html)
-			     (list "$$LINKS$$" links))))
+	      (with-output-to-string (out)
+		(funcall page-template (list
+					(cons "title" page-title)
+					(cons "content" page-html)
+					(cons "links" links))
+			 out))))
     (loop for post-name in posts-names
 	  for post-html in posts-html
 	  for post-title in posts-titles
 	  with links = (join-links links-for-posts)
 	  do (str:to-file
 	      (str:concat "./out/posts/" post-name)
-	      (replace-multi post-template
-			     (list "$$TITLE$$" post-title)
-			     (list "$$CONTENT$$" post-html)
-			     (list "$$LINKS$$" links))))
-    (loop for post-title in posts-titles
-	  for post-html in posts-html
-	  for post-summary = (get-summary post-html)
-	  for post-name in posts-names
-	  for post-link = (make-link
-			   (str:concat "./posts/" post-name)
-			   "Read more")
-	  do
-	     (setf index-posts
-		   (str:concat index-posts
-			       (replace-multi post-summary-template
-					      (list "$$POST-TITLE$$"
-						    post-title)
-					      (list "$$POST-SUMMARY$$"
-						    post-summary)
-				              (list "$$POST-LINK$$"
-						    post-link)))))
-    (str:to-file #P"./out/index.html"
-		 (replace-multi index-template
-				(list (find-summary-template index-template)
-				      index-posts)
-				(list "$$LINKS$$" (join-links
-						   links-for-index))))))
-	      
+	      (with-output-to-string (out)
+		(funcall post-template (list
+					(cons "title"  post-title)
+					(cons "content" post-html)
+					(cons "links" links))
+			 out))))
+    (let* ((posts-data (loop for post-title in posts-titles
+			     for post-html in posts-html
+			     for post-summary = (get-summary post-html)
+			     for post-name in posts-names
+			     for post-link = (make-link
+					      (str:concat "./posts/" post-name)
+					      "Read more")
+			     collect (list (cons "post-title" (get-title post-html))
+					   (cons "post-summary" post-summary)
+					   (cons "post-link" post-link)))))
+      (str:to-file #P"./out/index.html"
+		   (with-output-to-string (out)
+		     (funcall index-template
+			      (list (cons "links" (join-links links-for-index))
+				    (cons "foreach" posts-data))
+			      out))))
+    (when (probe-file #P"./files")
+      (let* ((files-template (mustache:compile-template #P"./templates/files.html"))
+	     (files-list (jonathan:parse
+			  (uiop:read-file-string #P"./files/list.json")))
+	     (files-list- (loop for file in files-list
+				for addr = (getf file :|addr|)
+				collect (list (cons "color"
+						    (random-color))
+					      (cons "addr"
+						    (file-namestring addr))
+					      (cons "title"
+						    (file-namestring addr))
+					      (cons "description"
+						    (getf file :|description|))
+					      (cons "size"
+						    (get-human-readable-size
+						     (get-file-size addr)))))))
+	(ensure-directories-exist #P"./out/files/")
+	(str:to-file #P"./out/files/index.html"
+		     (with-output-to-string (out)
+		       (funcall files-template
+				(list (cons "links" (join-links links-for-posts))
+				      (cons "foreach" files-list-))
+				out)))))))

+ 61 - 0
templates/files.html

@@ -0,0 +1,61 @@
+<!DOCTYPE html>
+<!-- hi, is my code pretty? -->
+<html>
+   <head>
+      <title>Farooq's Website | Files</title>
+      <link rel="stylesheet" href="../css/def.css" type="text/css">
+      <link rel="stylesheet" href="../css/w3.css" type="text/css">
+      <meta name="viewport" content="width=device-width,initial-scale=1">
+      <meta name="keywords"
+      content="Farooq,FarooqKZ,Farooq's website,Farooq Karimi Zadeh">
+   </head>
+   <body>
+     <header class="w3-center w3-teal w3-container">
+       <h1>Farooq's Website</h1>
+
+       <a href="index.html">Home</a> |
+       <a href="https://notabug.org/farooqkz">Projects</a>
+       |
+       <a href="http://blinuxshell.ir/users/farooqkz">
+         Files on bitcoinshell
+       </a>
+         
+     </header>
+     <h3>Farooq's files</h3>
+     {{#foreach}}
+     <div class="w3-container">
+       <div class="w3-container w3-{{color}}">
+	 <div><a href="{{addr}}">{{title}}</a></div>
+	 <div><p>Description: {{{description}}}</p></div>
+	 <div>Size: {{{size}}}</div>
+       </div>
+     </div>
+     {{/foreach}}
+     <footer class="w3-container w3-teal w3-center">
+          {{{links}}}
+            <br>Content is available under
+            <a href="https://creativecommons.org/publicdomain/zero/1.0/legalcode">
+              CC0</a> unless something else was specified.<br>
+              This website has been 
+              generated using <a href="https://notabug.org/farooqkz/fobil">
+              fobil</a>.
+	    &nbsp;
+     </footer>
+   </body>
+</html>
+<!--
+کلمة الله التی ألقاها الی مریم
+ابن الله، صلی الیه و امه و سلم
+سیرسله    الله   قبل    الساعة
+سیقتل الدجال بیده قبل  القیامة
+***
+دین ندارم ایمانم کجاست
+عشق ندارم احسانم کجاست
+حرفی نیست یار نظر نخواهد کرد
+جایگاه مارا در نار آماده خواهد کرد
+***
+کان ربی رحمن رحیم
+الآن انا من أصحاب شیطان رجیم
+لا أفعل إلا فعل شیطان
+لا أقول إلا أن کفرت برحمن
+-->

+ 22 - 8
templates/index.html

@@ -9,24 +9,38 @@
       <meta name="keywords"
       content="Farooq,FarooqKZ,Farooq's website,Farooq Karimi Zadeh">
    </head>
-   <body class="w3-margin-top">
+   <body>
      <header class="w3-center w3-teal w3-container">
        <h1>Farooq's Website</h1>
 
        <a href="index.html">Home</a> |
        <a href="https://notabug.org/farooqkz">Projects</a>
+       |
+       <a href="http://blinuxshell.ir/users/farooqkz">
+         Files on bitcoinshell
+       </a>
          
      </header>
-     <div style="text-align: center">
-       <img height="200vw" width="200vw" src="favicon.png">
+     <div class="about">
+        <div style="text-align: center">
+            <img height="200vw" width="200vw" src="favicon.png">
+        </div>
+        <p>
+            Hello :) I am Farooq Karimi Zadeh a young programmer and student.
+            I study Computer Engineering in Hormozgan University(which is in
+            Hormozgan province in Iran). I really(or not really?) like
+            Mathematics(I know what you're gonna say DotMan, and you know my
+            reaction so let's end it right here!), Computers, Code and Electronics.
+            That's it all for now :) Have fun in my site ;)
+        </p>
      </div>
-     $$FOREACH$$
-     <h3>$$POST-TITLE$$</h3>
-     <p>$$POST-SUMMARY$$</p> $$POST-LINK$$
+     {{#foreach}}
+     <h3>{{{post-title}}}</h3>
+     <p>{{{post-summary}}}</p> {{{post-link}}}
      <hr>
-     $$END$$
+     {{/foreach}}
      <footer class="w3-container w3-teal w3-center">
-          $$LINKS$$
+          {{{links}}}
             <br>Content is available under
             <a href="https://creativecommons.org/publicdomain/zero/1.0/legalcode">
               CC0</a> unless something else was specified.<br>

+ 7 - 4
templates/page.html

@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <html>
    <head>
-      <title>Farooq's Website | $$TITLE$$</title>
+      <title>Farooq's Website | {{{title}}}</title>
       <link rel="stylesheet" href="../css/def.css" type="text/css">
       <link rel="stylesheet" href="../css/w3.css" type="text/css">
       <meta name="viewport" content="width=device-width,initial-scale=1">
@@ -17,11 +17,14 @@
          <h1>Farooq's Website</h1>
             
          <a href="../index.html">Home</a> |
-         <a href="https://notabug.org/farooqkz">Projects</a> 
+         <a href="https://notabug.org/farooqkz">Projects</a> |
+         <a href="http://blinuxshell.ir/users/farooqkz/">
+           Files on bitcoinshell
+         </a>
        </header>
-       $$CONTENT$$ 
+       {{{content}}}
        <footer class="w3-container w3-teal w3-center">
-            $$LINKS$$
+            {{{links}}}
             <br>Content is available under
             <a href="https://creativecommons.org/publicdomain/zero/1.0/legalcode">
             CC0</a> unless something else was specified.<br>

+ 8 - 5
templates/post.html

@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <html>
    <head>
-      <title>Farooq's Website | $$TITLE$$</title>
+      <title>Farooq's Website | {{{title}}}</title>
       <link rel="stylesheet" href="../css/def.css" type="text/css">
       <link rel="stylesheet" href="../css/w3.css" type="text/css">
       <style>
@@ -18,13 +18,17 @@
          <h1>Farooq's Website</h1>
             
          <a href="../index.html">Home</a> |
-         <a href="https://notabug.org/farooqkz">Projects</a>
+         <a href="https://notabug.org/farooqkz">Projects</a> |
+         <a href="http://blinuxshell.ir/users/farooqkz/">
+           Files on bitcoinshell
+         </a>
+
        </header>
         <section class="w3-container">
-        $$CONTENT$$ 
+         {{{content}}}
         </section>
         <footer class="w3-container w3-teal w3-center">
-            $$LINKS$$
+            {{{links}}}
             <br>Content is available under
             <a href="https://creativecommons.org/publicdomain/zero/1.0/legalcode">
             CC0</a> unless something else was specified.<br>
@@ -35,6 +39,5 @@
 
              &nbsp;
         </footer>
-
     </body>
 </html>