俺を支えた専門書 「プログラミング言語」

今までに読んだプログラミング言語に関する専門書を書いていきます。

言語

今現在で書ける言語は以下のとおりです。

C

苦しんで覚えるC言語

苦しんで覚えるC言語

Cの絵本 第2版 C言語が好きになる新しい9つの扉

Cの絵本 第2版 C言語が好きになる新しい9つの扉

C言語ポインタ完全制覇 (標準プログラマーズライブラリ)

C言語ポインタ完全制覇 (標準プログラマーズライブラリ)

C++

独習C++ 第4版

独習C++ 第4版

C++ の絵本

C++ の絵本

Java

明快入門Java (林晴比古実用マスターシリーズ)

明快入門Java (林晴比古実用マスターシリーズ)

パーフェクトJava (PERFECT SERIES) (PERFECT SERIES 2)

パーフェクトJava (PERFECT SERIES) (PERFECT SERIES 2)

HTML/CSS

スラスラわかるHTML&CSSのきほん

スラスラわかるHTML&CSSのきほん

JavaScript

パーフェクトJavaScript (PERFECT SERIES 4)

パーフェクトJavaScript (PERFECT SERIES 4)

PHP

独習PHP 第3版

独習PHP 第3版

MySQL

基礎からのMySQL 改訂版 (基礎からシリーズ)

基礎からのMySQL 改訂版 (基礎からシリーズ)

Python

みんなのPython 第3版

みんなのPython 第3版

パーフェクトPython (PERFECT SERIES 5)

パーフェクトPython (PERFECT SERIES 5)

Ruby

たのしいRuby 第5版

たのしいRuby 第5版

パーフェクトRuby (PERFECT SERIES 6)

パーフェクトRuby (PERFECT SERIES 6)

Golang

みんなのGo言語【現場で使える実践テクニック】

みんなのGo言語【現場で使える実践テクニック】

Go言語入門

Go言語入門

指定した単語を含むパワポファイル出力ツールを作ったよ

作ったきっかけ

Golang系のスライドどこいった...?」 っていうことが度々起こったため作りました。

slideshareのスライドを電車の中で読むために、いっぱいダウンロードするんですよね。 で、何日か経つと「あのときのあのスライドどこいった〜?」ってなるわけ。

作ったもの

-w word で指定するとwordを含むパワポファイルのフルパスを表示。

$ ./pptgrep -w GOOGLE
/home/k2la/test.pptx

-p startpoint で指定するとstartpoint内のファイルから探してくれる。

$ ./pptgrep -w APPLE -p ~
/home/k2la/test1.pptx
/home/k2la/test2.pptx
/home/k2la/test3.pptx

-r をつけて再帰的に探すこともできます

$ ./pptgrep -w Docker -r -p ~/Downloads
/home/k2la/Downloads/docker/test.pptx
/home/k2la/Downloads/docker/test02.pptx
/home/k2la/Downloads/slide0706/test.pptx

実装

パワポファイル(.pptx)

file コマンドで調べてみると

$ file test.pptx
test.pptx: Microsoft PowerPoint 2007+

こんな感じに出力されます。

で、これを unzip コマンドで解凍してみると

$ unzip test.pptx -d test
Archive:  test.pptx
  inflating: test/[Content_Types].xml
  inflating: test/_rels/.rels
  inflating: test/ppt/slides/_rels/slide1.xml.rels
  inflating: test/ppt/slides/_rels/slide2.xml.rels
  inflating: test/ppt/slides/_rels/slide3.xml.rels
  inflating: test/ppt/slides/_rels/slide4.xml.rels
  inflating: test/ppt/_rels/presentation.xml.rels
  inflating: test/ppt/presentation.xml
  inflating: test/ppt/slides/slide4.xml
  inflating: test/ppt/slides/slide3.xml
  inflating: test/ppt/slides/slide2.xml
  inflating: test/ppt/slides/slide1.xml
  inflating: test/ppt/slideLayouts/_rels/slideLayout3.xml.rels
  inflating: test/ppt/slideLayouts/_rels/slideLayout6.xml.rels
  inflating: test/ppt/slideLayouts/_rels/slideLayout8.xml.rels
  inflating: test/ppt/slideLayouts/_rels/slideLayout9.xml.rels
  inflating: test/ppt/slideLayouts/_rels/slideLayout7.xml.rels
  inflating: test/ppt/slideLayouts/_rels/slideLayout4.xml.rels
  inflating: test/ppt/slideLayouts/_rels/slideLayout5.xml.rels
  inflating: test/ppt/slideLayouts/_rels/slideLayout1.xml.rels
  inflating: test/ppt/slideLayouts/_rels/slideLayout2.xml.rels
  inflating: test/ppt/slideMasters/_rels/slideMaster1.xml.rels
  inflating: test/ppt/slideLayouts/_rels/slideLayout10.xml.rels
  inflating: test/ppt/slideLayouts/slideLayout10.xml
  inflating: test/ppt/slideLayouts/slideLayout2.xml
  inflating: test/ppt/slideLayouts/slideLayout1.xml
  inflating: test/ppt/slideLayouts/_rels/slideLayout11.xml.rels
  inflating: test/ppt/slideMasters/slideMaster1.xml
  inflating: test/ppt/slideLayouts/slideLayout3.xml
  inflating: test/ppt/slideLayouts/slideLayout4.xml
  inflating: test/ppt/slideLayouts/slideLayout5.xml
  inflating: test/ppt/slideLayouts/slideLayout9.xml
  inflating: test/ppt/slideLayouts/slideLayout11.xml
  inflating: test/ppt/slideLayouts/slideLayout8.xml
  inflating: test/ppt/slideLayouts/slideLayout6.xml
  inflating: test/ppt/slideLayouts/slideLayout7.xml
  inflating: test/ppt/theme/theme1.xml
 extracting: test/ppt/media/image1.jpeg
  inflating: test/ppt/theme/_rels/theme1.xml.rels
 extracting: test/docProps/thumbnail.jpeg
  inflating: test/ppt/presProps.xml
  inflating: test/ppt/tableStyles.xml
  inflating: test/ppt/viewProps.xml
  inflating: test/docProps/app.xml
  inflating: test/docProps/core.xml

こんな感じに解凍されます。

スライドのタイトルや内容の文字列は ppt/slides/slide*.xml に含まれています。

この中を調べていくことになります。

ソースコードの一部

以下のコードは

  • isPpt(ファイル): パワポファイルかどうかの判定や
  • containWord(パワポファイル, 検索語): unzipと文字列の検索

で構成されています。

package main

import (
    "archive/zip"
    "path/filepath"
    "regexp"
)

var slide = regexp.MustCompile(filepath.Join("ppt", "slides", "slide"))

func isPpt(path string) bool {
    e := filepath.Ext(path)
    if e == ".ppt" || e == ".pptx" {
        return true
    }
    return false
}

func containWord(archive, word string) bool {
    reader, err := zip.OpenReader(archive)
    if err != nil {
        return false
    }

    for _, file := range reader.File {
        if slide.MatchString(file.Name) {
            fileReader, err := file.Open()
            if err != nil {
                return false
            }
            var p = make([]byte, file.FileInfo().Size())
            fileReader.Read(p)
            defer fileReader.Close()
            r := regexp.MustCompile(word)
            if r.MatchString(string(p)) {
                return true
            }
        }
    }
    return false
}

パワポファイルかどうかの判定は、あまりよろしくないですが拡張子で判定しています。

文字列の検索部分は、正規表現で含んでいるかどうかを見ています。

リポジトリはこちら

GitHub - k2la/pptgrep