(defun fic-ao3 ()
  "Format fic for posting to AO3 and export it"
  (interactive)
  ;; (org-format)
  (save-buffer)
  (goto-char (point-min))
  (while (re-search-forward "\\[fn:[^•]*•\\]" nil t)
    (replace-match ""))
  (let ((org-export-with-title 'nil)
	(org-export-with-author 'nil)
	(org-export-with-creator 'nil)
	(org-export-with-date 'nil)
	(org-export-with-section-numbers 'nil)
	(org-export-time-stamp-file 'nil)
	(org-export-with-toc 'nil)
	(org-html-validation-link 'nil))
    (org-html-export-to-html))
  (revert-buffer t t)
  (setq oldname (concat (file-name-base) ".org"))
  (setq newname (concat (file-name-base) ".html"))
  (find-file-noselect newname)
  (with-current-buffer newname
    (goto-char (point-min))
    (setq delpoint-one (search-forward "<body>\n"))
    (delete-region (point-min) delpoint-one)
    (goto-char (point-max))
    (setq delpoint-two (search-backward "</body>"))
    (delete-region delpoint-two (point-max))
    (goto-char (point-min))
    (while (re-search-forward "<div.*>" nil t)
      (replace-match ""))
    (goto-char (point-min))
    (while (re-search-forward "</div>" nil t)
      (replace-match ""))
    (goto-char (point-min))
    (while (re-search-forward "<h2.*>" nil t)
      (replace-match ""))
    (goto-char (point-min))
    (while (re-search-forward "\n\n" nil t)
      (replace-match "\n"))
    (write-file newname)
    (kill-ring-save (point-min) (point-max)))
  (kill-buffer newname)
  (with-current-buffer oldname
    (delete-file newname)))

(unless (string-equal system-type "android")
  (defun fic-export ()
    "Export fic to HTML, EPUB, PDF"
    (interactive)
    (shell-command "cp ~/.emacs.d/fic-export-files/FFXYevon.ttf .")
    (setq asal (y-or-n-p "ASAL?"))
    (if asal
	(progn
	  (setq title t)
	  (setq multichapter t)
	  (setq chaptertitles t)
	  (setq ffx t))
      (progn
	(setq title (y-or-n-p "Use the fic title in exports?"))
	(setq multichapter (y-or-n-p "Multichapter?"))
	(if multichapter
	    (setq chaptertitles (y-or-n-p "Titled chapters?")))
	(setq ffx (y-or-n-p "FFX fic?"))))
    ;; (setq draftmode (y-or-n-p "Edit LaTeX file manually?"))
    ;; (org-format)
    (save-buffer)
    (goto-char (point-min))
    (while (re-search-forward "\\[fn:\\([0-9]*\\):\\([^•]*\\)•\\]" nil t)
      (replace-match "@@html:<label for=\"\\1\" class=\"margin-toggle sidenote-number\"></label><input type=\"checkbox\" id=\"\\1\" class=\"margin-toggle\"/><span class=\"sidenote\">@@@@latex:\\\\footnote{@@\\2@@html:</span>@@@@latex:}@@"))
    (let ((user-full-name "“Tré”")
	  (org-html-head "<link rel=\"stylesheet\" href=\"/fic/tufte.css\"/>\n<link rel=\"stylesheet\" href=\"/home/mdd/Documents/proj/fic-archive/build/tufte.css\"/>\n<meta name=\"theme-color\" media=\"(prefers-color-scheme: light)\" content=\"#fffff8\" />\n<meta name=\"theme-color\" media=\"(prefers-color-scheme: dark)\" content=\"#151515\" />\n<meta property=\"og:title\" content=\"Tré’s fanfiction\">\n<meta property=\"og:type\" content=\"article\">\n<meta property=\"og:image\" content=\"https://tre.praze.net/ab.png\">\n<meta name=\"description\" property=\"og:description\" content=\"Fanfiction on tre.praze.net\">\n<meta property=\"og:locale\" content=\"en_GB\">\n<meta property=\"og:site_name\" content=\"tre.praze.net\">\n<meta name=\"fediverse:creator\" content=\"@tre@ple.praze.net\">")
	  (org-export-with-author t)
	  (org-latex-default-class "memoir"))
      (org-html-export-to-html)
      (org-latex-export-to-latex))
    (revert-buffer t t)
    (setq htmlfile (concat (file-name-base) ".html"))
    (find-file-noselect htmlfile)
    (with-current-buffer htmlfile
      (goto-char (point-min))
      (while (re-search-forward "<div id=\"content\"" nil t)
	(replace-match "<article id=\"content\""))
      (goto-char (point-min))
      (while (re-search-forward "<div id=\"outline-container-" nil t)
	(replace-match "<section id=\"outline-container-"))
      (goto-char (point-min))
      (while (re-search-forward "</div>\n</div>" nil t)
	(replace-match "</div>\n</section>"))
      (goto-char (point-min))
      (while (re-search-forward "<hr />\n\n<p>" nil t)
	(replace-match "</section>\n<section>\n<p class=\"newthought\">"))
      (goto-char (point-min))
      (while (re-search-forward "<hr>\n\n<p>" nil t)
	(replace-match "</section>\n<section>\n<p class=\"newthought\">"))
      (goto-char (point-min))
      (while (re-search-forward "<hr>\n<p>" nil t)
	(replace-match "</section>\n<section>\n<p class=\"newthought\">"))
      (goto-char (point-min))
      (while (re-search-forward "<div class=\"outline-text-2\" id=\".*\">" nil t)
	(replace-match ""))
      (goto-char (point-min))
      (while (re-search-forward "<div id=\"postamble\" class=\"status\">" nil t)
	(replace-match ""))
      (goto-char (point-min))
      (while (re-search-forward "<p class=\"validation\"><a href=.*>Validate</a></p>" nil t)
	(replace-match ""))
      (goto-char (point-min))
      (while (re-search-forward "</div>" nil t)
	(replace-match ""))
      (goto-char (point-min))
      (while (re-search-forward "section-number-2\">\\([0-9]*\\)<" nil t)
	(replace-match "section-number-2\">\\1\.<"))
      (goto-char (point-min))
      (while (re-search-forward "<p class=\"author.*</p>" nil t)
	(replace-match ""))
      (goto-char (point-min))
      (while (re-search-forward "\n\n</body>" nil t)
	(replace-match "\n</article>\n</body>"))
      (if (not title)
	  (progn
	    (goto-char (point-min))
	    (while (re-search-forward "<h1.*" nil t)
	      (replace-match ""))))
      (if multichapter
	  (if (not chaptertitles)
	      (progn
		(goto-char (point-min))
		(while (re-search-forward "<span class=\"section-number-.*</span>" nil t)
		  (replace-match ""))))
	(progn
	  (goto-char (point-min))
	  (while (re-search-forward "<h2.*" nil t)
	    (replace-match ""))))
      (save-buffer)
      (kill-current-buffer))
    (setq texfile (concat (file-name-base) ".tex"))
    (find-file-noselect texfile)
    (with-current-buffer texfile
      (if asal
          (progn
            (goto-char (point-min))
            (search-forward "\\begin{document}")
            (while (re-search-forward "\n" nil t)
              (replace-match "\n\n"))
            (save-buffer)
            (shell-command 
             (format "pandoc %s -o %s.epub --epub-cover-image=/home/mdd/Documents/drive/the\\ walk/cover.png" 
	             (shell-quote-argument (buffer-file-name))
	             (shell-quote-argument (file-name-base)))))
        (shell-command 
         (format "pandoc %s -o %s.epub" 
	         (shell-quote-argument (buffer-file-name))
	         (shell-quote-argument (file-name-base)))))
      (goto-char (point-min))
      (while (re-search-forward "label{\\(.*\\)}\n\n" nil t)
        (replace-match "label{\\1}\n"))
      (goto-char (point-min))
      (while (re-search-forward "label{\\(.*\\)}\n\\([A-Z]\\)\\([a-z]*\\)" nil t)
	(replace-match "label{\\1}\n\\\\lettrine{\\2}{\\3}"))
      (goto-char (point-min))
      (while (re-search-forward "label{\\(.*\\)}\n“\\([A-Z]\\)\\([a-z]*\\)" nil t)
	(replace-match "label{\\1}\n\\\\lettrine{“\\2}{\\3}"))
      (goto-char (point-min))
      (while (re-search-forward "label{\\(.*\\)}\n«~\\([A-Z]\\)\\([a-z]*\\)" nil t)
	(replace-match "label{\\1}\n\\\\lettrine{«~\\2}{\\3}"))
      (goto-char (point-min))
      (while (re-search-forward "~–”" nil t)
        (replace-match "\\\\mbox{~–”}"))
      (if ffx
	  (progn
	    (goto-char (point-min))
	    (while (re-search-forward "\\\\noindent\\\\rule{\\\\textwidth}{0.5pt}" nil t)
	      (replace-match "\\sepffx" nil t)))
	(progn
	  (goto-char (point-min))
	  (while (re-search-forward "\\\\noindent\\\\rule{\\\\textwidth}{0.5pt}" nil t)
	    (replace-match "\\sephim" nil t))))
      (if multichapter
	  (if title
	      (if asal
		  (progn
		    (goto-char (point-min))
		    (while (re-search-forward "\\\\maketitle" nil t)
		      (replace-match "\\begin{titlingpage}\n\\titleasal\n\\end{titlingpage}" nil t)))
		(progn
		  (goto-char (point-min))
		  (while (re-search-forward "\\\\maketitle" nil t)
		    (replace-match "\\begin{titlingpage}\n\\titlegen\n\\end{titlingpage}" nil t))))
	    (progn
	      (goto-char (point-min))
	      (while (re-search-forward "\\\\maketitle" nil t)
		(replace-match ""))))
	(progn
	  (goto-char (point-min))
	  (while (re-search-forward "\\\\maketitle" nil t)
	    (replace-match ""))))
      (if multichapter
	  (if (not chaptertitles)
	      (progn
		(goto-char (point-min))
		(while (re-search-forward "namechapters" nil t)
		  (replace-match "nonamechapters"))))
	(progn
	  (goto-char (point-min))
	  (while (re-search-forward "\\\\chapter.*" nil t)
	    (replace-match ""))
	  (if title
	      (progn
		(goto-char (point-min))
		(while (re-search-forward "\\\\begin{document}" nil t)
		  (replace-match "\\\\begin{document}\n\\\\chapter\*{\\\\thetitle}"))))))
      (if asal
          (progn
            (goto-char (point-min))
	    (while (re-search-forward "\\\\begin{document}" nil t)
	      (replace-match "\\begin{document}\n\\pagestyle{empty}" nil t))
	    (goto-char (point-min))
	    (while (re-search-forward "\\\\end{titlingpage}" nil t)
	      (replace-match "\\end{titlingpage}\n\\pagestyle{multititlechapters}\n\\tableofcontents\*" nil t)))
        (if multichapter
	    (if title
	        (if chaptertitles
		    (progn
		      (goto-char (point-min))
		      (while (re-search-forward "\\\\begin{document}" nil t)
		        (replace-match "\\begin{document}\n\\pagestyle{multititlechapters}" nil t))
		      (goto-char (point-min))
		      (while (re-search-forward "\\\\end{titlingpage}" nil t)
		        (replace-match "\\end{titlingpage}\n\\tableofcontents\*" nil t)))
		  (progn
		    (goto-char (point-min))
		    (while (re-search-forward "\\\\begin{document}" nil t)
		      (replace-match "\\begin{document}\n\\pagestyle{multititlenochapters}" nil t))))
	      (if chaptertitles
		  (progn
		    (goto-char (point-min))
		    (while (re-search-forward "\\\\begin{document}" nil t)
		      (replace-match "\\begin{document}\n\\pagestyle{multinotitlechapters}\n\\tableofcontents\*" nil t)))
	        (progn
		  (goto-char (point-min))
		  (while (re-search-forward "\\\\begin{document}" nil t)
		    (replace-match "\\begin{document}\n\\pagestyle{multinotitlenochapters}" nil t)))))
	  (if title
	      (progn
	        (goto-char (point-min))
	        (while (re-search-forward "\\\\begin{document}" nil t)
		  (replace-match "\\begin{document}\n\\pagestyle{singletitle}" nil t)))
	    (progn
	      (goto-char (point-min))
	      (while (re-search-forward "\\\\begin{document}" nil t)
	        (replace-match "\\begin{document}\n\\pagestyle{plain}" nil t))))))
      (goto-char (point-max))
      (save-buffer)
      ;; (unless draftmode
        (shell-command 
         (format "xelatex -interaction=batchmode %s" 
        	 (shell-quote-argument (buffer-file-name))))
        (shell-command 
         (format "xelatex -interaction=batchmode %s" 
        	 (shell-quote-argument (buffer-file-name))))
        ;; )
      (kill-current-buffer))
    ;; (unless draftmode
      (delete-file "FFXYevon.ttf")
      (if (file-exists-p (concat (file-name-base) ".aux"))
          (delete-file (concat (file-name-base) ".aux")))
      (if (file-exists-p (concat (file-name-base) ".log"))
          (delete-file (concat (file-name-base) ".log")))
      (if (file-exists-p (concat (file-name-base) ".out"))
          (delete-file (concat (file-name-base) ".out")))
      (if (file-exists-p (concat (file-name-base) ".toc"))
          (delete-file (concat (file-name-base) ".toc")))
      (if (file-exists-p "texput.log")
          (delete-file "texput.log"))
      (delete-file texfile)
      ;; )
    (message "Done")))