#+TITLE: Welcome!
#+DATE: 2019-07-04
#+KEYWORDS: programming
#+AUTHOR: Yann Esposito
#+EMAIL: yann.esposito@gmail.com
#+DESCRIPTION:
#+LANGUAGE: en
#+LANG: en
#+OPTIONS: H:6

This is a new take on my personal blog.
With a lot more minimalism in mind.

With [[http://orgmode.org][org-mode]]

* Code magic                                                                            :noexport:
#+begin_src elisp :results none
  (require 'org)
  (require 'ox-publish)
  (require 'ox-html)
  (require 'org-element)
  (require 'ox-rss)

  (defun org-blog-prepare (project-plist)
    "With help from `https://github.com/howardabrams/dot-files'.
     Touch `index.org' to rebuilt it.
     Argument `PROJECT-PLIST' contains information about the current project."
    (let* ((base-directory (plist-get project-plist :base-directory))
           (buffer (find-file-noselect (expand-file-name "index.org" base-directory) t)))
      (with-current-buffer buffer
        (set-buffer-modified-p t)
        (save-buffer 0))))

  (defvar org-blog-head
    (concat
     "<link rel=\"stylesheet\" type=\"text/css\" href=\"/assets/css/minimalist.css\"/>"
     "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
     "<link rel=\"alternative\" type=\"application/rss+xml\" title=\"Subscribe to articles\" href=\"/archives.xml\" />"
     "<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/favicon.ico\">"))

  (defun org-blog-preamble (info)
    "Pre-amble for whole blog."
    (concat
     "<strong><code>Her.esy.fun"
     (when-let ((date (get-from-info info :date)))
       (format " - <span class=\"article-date\">%s</span>" date))
     "</code></strong>"))

  (defun menu ()
    "Blog menu"
    (concat
     "<navigation>"
     "<a href=\"/\">Home</a>"
     "<a href=\"/archive.html\">Posts</a>"
     "</navigation>"))

  (defun get-from-info (info k)
    (let ((i (car (plist-get info k))))
      (when (and i (stringp i))
        i)))

  (defun org-blog-postamble (info)
    "Post-amble for whole blog."
    (concat "<footer>"
            (when-let ((author (get-from-info info :author)))
              (if-let ((email (plist-get info :email)))
                  (format "<div class=\"author\">Author: <a href=\"mailto:%s\">%s</a></div>" email author)
                (format "<div class=\"author\">Author: %s</div>" author)))
            (when-let ((date (get-from-info info :date)))
              (format "<div class=\"date\">Created: %s</div>" date))
            (when-let ((keywords (plist-get info :keywords)))
              (format "<div class=\"keywords\">Keywords: <code>%s</code></div>" keywords))
            (format "<div class=\"date\">Generated: %s</div>"
                    (format-time-string "%Y-%m-%d %H:%M:%S"))
            (format (concat "<div class=\"creator\"> Generated with "
                            "<a href=\"https://www.gnu.org/software/emacs/\" target=\"_blank\" rel=\"noopener noreferrer\">Emacs %s</a>, "
                            "<a href=\"http://spacemacs.org\" target=\"_blank\" rel=\"noopener noreferrer\">Spacemacs %s</a>, "
                            "<a href=\"http://orgmode.org\" target=\"_blank\" rel=\"noopener noreferrer\">Org Mode %s</a>"
                            "</div>")
                    emacs-version spacemacs-version org-version)
            "</footer>"
            "<hr/>"
            (menu)))

  (defun org-blog-sitemap-format-entry (entry _style project)
    "Return string for each ENTRY in PROJECT."
    (when (s-starts-with-p "posts/" entry)
      (format "@@html:<span class=\"archive-item\"><span class=\"archive-date\">@@ %s: @@html:</span>@@ [[file:%s][%s]] @@html:</span>@@"
              (format-time-string "%Y-%m-%d"
                                  (org-publish-find-date entry project))
              entry
              (org-publish-find-title entry project))))

  (defun org-blog-sitemap-function (title list)
    "Return sitemap using TITLE and LIST returned by `org-blog-sitemap-format-entry'."
    (concat "#+TITLE: " title "\n"
            "#+AUTHOR: Yann Esposito\n"
            "#+EMAIL: yann.esposito@gmail.com\n"
            "\n#+begin_archive\n"
            (mapconcat (lambda (li)
                         (format "@@html:<li>@@ %s @@html:</li>@@" (car li)))
                       (seq-filter #'car (cdr list))
                       "\n")
            "\n#+end_archive\n"))

  (setq base-dir (concat (projectile-project-root) "src"))
  (setq publish-dir (concat (projectile-project-root) "_site"))
  (setq assets-dir (concat base-dir "/assets"))
  (setq publish-assets-dir (concat publish-dir "/assets"))
  (setq rss-dir base-dir)
  (setq publish-rss-dir publish-dir)
  (setq domainname "https://her.esy.fun")
  (setq org-publish-project-alist
        `(("orgfiles"
           :base-directory ,base-dir
           :exclude ".*drafts/.*"
           :base-extension "org"

           :publishing-directory ,publish-dir

           :recursive t
           :preparation-function org-blog-prepare
           :publishing-function org-html-publish-to-html

           :with-toc nil
           :with-title t
           :with-date t
           :section-numbers nil
           :html-doctype "html5"
           :html-html5-fancy t
           :html-head-include-default-style nil
           :html-head-include-scripts nil
           :htmlized-source t
           :html-head-extra ,org-blog-head
           :html-preamble org-blog-preamble
           :html-postamble org-blog-postamble

           :auto-sitemap t
           :sitemap-filename "archive.org"
           :sitemap-title "Blog Posts"
           :sitemap-style list
           :sitemap-sort-files anti-chronologically
           :sitemap-format-entry org-blog-sitemap-format-entry
           :sitemap-function org-blog-sitemap-function)

          ("assets"
           :base-directory ,assets-dir
           :base-extension ".*"
           :publishing-directory ,publish-assets-dir
           :publishing-function org-publish-attachment
           :recursive t)

          ("rss"
           :base-directory ,rss-dir
           :base-extension "org"
           :html-link-home ,domainname
           :html-link-use-abs-url t
           :rss-extension "xml"
           :publishing-directory ,publish-rss-dir
           :publishing-function (org-rss-publish-to-rss)
           :exclude ".*"
           :include ("archive.org")
           :section-numbers nil
           :table-of-contents nil)

          ("blog" :components ("orgfiles" "assets" "rss"))))

  ;; add target=_blank and rel="noopener noreferrer" to all links by default
  (defun my-org-export-add-target-blank-to-http-links (text backend info)
    "Add target=\"_blank\" to external links."
    (when (and
           (org-export-derived-backend-p backend 'html)
           (string-match "href=\"http[^\"]+" text)
           (not (string-match "target=\"" text))
           (not (string-match (concat "href=\"" domainname "[^\"]*") text)))
      (string-match "<a " text)
      (replace-match "<a target=\"_blank\" rel=\"noopener noreferrer\" " nil nil text)))

  (add-to-list 'org-export-filter-link-functions
               'my-org-export-add-target-blank-to-http-links)

#+end_src

* Template Testing

Text can be *bold*, /italic/, +strikethrough+ or =keyword=.

[[/index.html][Link to another page]].

There should be whitespace between paragraphs.

* Header 2

There should be whitespace between paragraphs.
GitHub is a code hosting platform for version control and collaboration.
It lets you and others work together on projects from anywhere.

** Header 3

#+begin_quote
This is a blockquote folling a header.

When something is important enough, you do it even if the odds are not in your
favor.
#+end_quote

*** Header 4

#+begin_src javascript
// Javascript code with syntax highlighting.
var fun = function lang(l) {
  dateformat.i18n = require('./lang/' + l)
  return true;
}
#+end_src

#+begin_src ruby
# Ruby code with syntax highlighting
GitHubPages::Dependencies.gems.each do |gem, version|
  s.add_dependency(gem, "= #{version}")
end
#+end_src

#+begin_src clojure
(defn clj-fn
  "A clojure function with syntax highlighting"
  [arg]
  (clojure.pprint/pprint arg))
#+end_src

#+begin_src haskell
-- main hello world
main :: IO ()
main = do
  putStrLn "What is your name?"
  name <- getLine
  putStrLn $ "Hello " <> name <> "!"
#+end_src

**** Header 5

- this is an unordered list following a header.
- this is an unordered list following a header.
- this is an unordered list following a header.

***** Header 6

1. this is an ordered list following a header.
2. this is an ordered list following a header.
   - sub unordered
     - sub sub unordered
     - xxx
   - unordered
     1. ordered again
     2. yep :)
3. this is an ordered list following a header.
   1. sub ordered
   2. second
      1. sub sub ordered
      2. still
   3. here
4. this is an ordered list following a header.

| head1        | head two          |
|--------------+-------------------|
| ok           | good swedish fish |
| out of stock | good and plenty   |
| ok           | good =oreos=      |
| ok           | good =zoute= drop |

There's a horizontal rule below this

------

** Here is an unordered list:

- level 1 item
  - level 2 item
  - level 2 item
  - level 2 item
    - level 3 item
    - level 3 item
- level 1 item
  - level 2 item
    - level 3 item
    - level 3 item
  - level 2 item
  - level 2 item
- level 1 item
  - level 2 item
  - level 2 item
  - level 2 item
