[PYTHON] Qiita memo of my thoughts

Overview

Environment construction memo to satisfy the desire

Image of what you want to do

やりたいことイメージ図

  1. Make various notes with Markdown
  2. Build with Sphinx
  3. View Sphinx documents in your browser
  4. Post from Kobito to Qiita while linking files with Kobito
  5. Quickly search Sphinx documents from Alfred + Dash

environment

Preparation

Anyway, prepare a Sphinx project

$ mkvirtualenv qiitanote #I'm using virualenvwrapper

(qiitanote)$ pip install Sphinx
(qiitanote)$ mkdir qiitanote
(qiitanote)$ cd qiitanote
(qiitanote)$ sphinx-quickstarpt 

#You can hear various things in the dialogue, so if you answer a little, the creation is over

#try make html
(qiitanote)$ make html

# _build/html/HTML document is generated below
#Turn off Virtualhost with Apache and http://qiitanote.dev/so_build/I am trying to see below html

#Check the document(Chrome opens)
(qiitanote)$ open -a Google\ Chrome.app  http://qiitanote.dev

Initial directory structure

qiitanote
├── Makefile
├── _build
│   ├── doctrees
│   └── html <-HTML is generated here
├── _static
├── _templates
├── conf.py <-Sphinx config file
└── index.rst

Write in Markdown

Pandoc Insco

(qiitanote)$ brew install pandoc

Extension settings

# conf.Rewrite py

#Added the following contents
PROJECT_DIR = os.path.dirname(__file__)
sys.path.insert(0, PROJECT_DIR)
sys.path.insert(0, os.path.join(PROJECT_DIR, "libs"))  #Extension script installed under libs
extensions += ["sphinxcontrib_markdown"]

markdown_title = 'Qiita Note'
source_suffix = '.md'

Create a .md file and write it

(qiitanote)$ touch fisrtnote.md

contents

# My First Qiita Memo!!!

## 1. Hoge

hogehogehogehoge
hogehogehogehoge

## 2. Fuga

fugafugafugafuga
fugafugafugafuga

Build

(qiitanote)$ make html

result

Mifirst Qiita Memo

Work with Kobito

Make files work with Kobito for posting to Qiita

(qiitanote)$ open -a Kobito firstnote.md
# -*- coding: utf-8 -*-

import os
import time
import subprocess

TARGET_DIR = os.path.join(os.path.dirname(__file__), '..')
cmd = "open --hide -g -a Kobito {}"

# start Koibito.app
subprocess.call(cmd.format(""), shell=True)
time.sleep(3)

# associate .md file to Koibito.app
for root, dirs, files in os.walk(TARGET_DIR):
    for f in files:
        if f.endswith(".md"):
            f = os.path.abspath(os.path.join(root, f))
            subprocess.call(cmd.format(f), shell=True)

# hyde Kobito.app
subprocess.call(
    "osascript -e 'tell application \"Finder\"'"
    " -e 'set visible of process \"Kobito\" to false'"
    " -e 'end tell'",
    shell=True
)

Make this script run when you "make html"

html:
	$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
	python libs/associate_kobito.py  # <=add to
	@echo
	@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

When you build with this, a memo will appear in Kobito without permission.

Configuration so far

qiitanote
├── Makefile
├── _build
│   ├── doctrees
│   └── html
├── _static
├── _templates
├── conf.py
├── firstnote.md
├── index.md # <-add to
├── index.rst
└── libs
    ├── associate_kobito.py # <-add to
    └── sphinxcontrib_markdown.py # <-add to

Search with Alfred + Dash

It's a memo I wrote, so I want to search it easily at hand. Search using Alfred and Dash. I bought both Alfred and Dash for a fee.

sphinxcontrib-dashbuilder makes it easy to turn Sphinx documents into Docsets for Dash

Installation

(qiitanote)$ pip install sphinxcontrib-dashbuilder

Change configuration file

# conf.py
extensions += ["sphinxcontrib_markdown", "sphinxcontrib.dashbuilder"]
dash_name = 'QiitaNote'
dash_icon_file = '_static/qiita.png' # <-The favicon was properly brought from Qiita

Rewriting Makefile

# You can set these variables from the command line.
SPHINXOPTS    =
SPHINXBUILD   = sphinx-build
PAPER         =
BUILDDIR      = _build
DOCSETSDIR    = ~/Library/Application\ Support/Dash/DocSets/QiitaNote # <-add to

~abridgement~

# .Added dash to PHONY
.PHONY: dash help ... 

~abridgement~

#Add dash target
dash:
	$(SPHINXBUILD) -b dash $(ALLSPHINXOPTS) $(DOCSETSDIR)
	@echo
	@echo "Build finished. The Docset are in $(DOCSETSDIR)."

Build

(qiitanote)$ make dash

Check with Dash. Dash settings screen> Docsets> Press the Rescan button

ReScan

Qiita Note appears in Dash's Docsets

Dashの画面

It is troublesome to hit "make dash" every time, so build dash even when "make html".

html:
	$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
	$(SPHINXBUILD) -b dash $(ALLSPHINXOPTS) $(DOCSETSDIR) # <-add to
	python libs/associate_kobito.py  
	@echo
	@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

Cannot search with Alfred

Markdown-> reST Forces the heading to index during conversion.

## Hoge

## Fuga 

Suppose you have a headline like this. When converted to reST, do as follows.

.. index:: Hoge

Hoge
----

.. index:: Fuga

Fuga
----

Add the following process to libs / sphinxcontrib_markdown.py.

48  # insert index directive                                                                
49  newlines = []                                                                           
50  for line in source[0].split(u"\n"):                                                     
51      if self._is_heading_line(line):                                                     
52          prev = newlines[-1]                                                             
53          inedexline = u".. index:: {0}\n".format(prev)
54          newlines.insert(-1, inedexline)                                                 
55      newlines.append(line)
56  source[0] = "\n".join(newlines)

Now when you build again you can find the heading from Alfred

Alfred検索

Configuration so far

qiitanote
├── Makefile
├── _build
│   └── doctrees
├── _static
│   └── qiita.png # <-add to
├── _templates
├── conf.py
├── firstnote.md
├── index.md
├── index.rst
└── libs
    ├── associate_kobito.py
    └── sphinxcontrib_markdown.py

Change the look of Sphinx documents

Use my sphinx theme.

(qiitanote)$ pip install sphinxjp.themes.basicstrap

Change settings

# conf.py

extensions += ["sphinxcontrib_markdown", "sphinxcontrib.dashbuilder", 'sphinxjp.themes.basicstrap']
html_theme = 'basicstrap'
html_theme_options = {
    'noheader': True,

    'header_inverse': True,
    'relbar_inverse': True,

    'inner_theme': True,
    'inner_theme_name': 'bootswatch-sandstone',
}

Build

(qiitanote)$ make html

result

Qiita風

Deliverables

It's a little different from what is written, but I put it on Github. Please accept it.

https://github.com/tell-k/qiitanote

TODO

Summary

Postscript (2014/10/18)

Recommended Posts

Qiita memo of my thoughts
[My memo] python
My python environment memo
Transition of Qiita posts
[My memo] Preparing Django-Starting
[My memo] python -v / python -V
Python Tips (my memo)
LGTM outside of Qiita
[Personal memo] Auto-completion of bash
Features of programming languages [Memo]
[Memo] Construction of cygwin environment
My reverse numpy / scipy memo
[Python] Operation memo of pandas DataFrame
Get the number of views of Qiita
Confirmation of impulse response (personal memo)
Easy usage memo of Anaconda (conda)
Environment construction memo of pyenv + conda
Python3 compatible memo of "python start book"
[Memo] Small story of pandas, numpy
Understanding memo of collective intelligence programming
Separate display of Python graphs (memo)
Instant visualization of Qiita Advent Calender 2019
Operation memo of Conda virtual environment