Last active
December 12, 2015 06:19
-
-
Save tetsuok/4728630 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Compilation: | |
| // $ go build merge.go | |
| // | |
| // Build: | |
| // $ ./merge FILE | |
| package main | |
| import ( | |
| "bufio" | |
| "flag" | |
| "fmt" | |
| "io" | |
| "log" | |
| "os" | |
| "path/filepath" | |
| "regexp" | |
| "strings" | |
| ) | |
| func checkFatal(err error) { | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| } | |
| func matched(pat *regexp.Regexp, b []byte) string { | |
| res := []byte{} | |
| for _, s := range pat.FindAllSubmatchIndex(b, -1) { | |
| res = pat.Expand(res, []byte("$file"), b, s) | |
| } | |
| return string(res) | |
| } | |
| func openTex(file string, pat *regexp.Regexp) { | |
| f, err := os.Open(file) | |
| checkFatal(err) | |
| defer f.Close() | |
| dir := filepath.Dir(file) | |
| rd := bufio.NewReader(f) | |
| for { | |
| line, err := rd.ReadString('\n') | |
| if err == io.EOF { | |
| break | |
| } | |
| checkFatal(err) | |
| if strings.HasPrefix(line, "%") { | |
| continue | |
| } | |
| if pat.Match([]byte(line)) { | |
| m := matched(pat, []byte(line)) | |
| openTex(filepath.Join(dir, m+".tex"), pat) | |
| } else { | |
| fmt.Printf(line) | |
| } | |
| } | |
| } | |
| func main() { | |
| flag.Parse() | |
| if flag.NArg() == 1 { | |
| pat, err := regexp.Compile(`\\(input|include){(?P<file>\w+)}`) | |
| checkFatal(err) | |
| openTex(flag.Arg(0), pat) | |
| } else { | |
| fmt.Printf("Usage %s FILE\n", os.Args[0]) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # coding: utf-8 | |
| """ | |
| Reduce tex files in order to submit final thesis. | |
| usage: | |
| $ ./merge_thesis.py main.tex | |
| """ | |
| import os | |
| import re | |
| import sys | |
| regexp = re.compile(r'\\(input|include)\{(\w+)\}') | |
| def open_tex(filename): | |
| dirname = os.path.dirname(filename) | |
| with open(filename) as f: | |
| for line in f: | |
| if line.startswith('%'): | |
| continue | |
| m = regexp.match(line) | |
| if m: | |
| if len(m.groups()) == 2: | |
| open_tex(os.path.join(dirname, m.groups()[1] + '.tex')) | |
| else: | |
| print 'Error:', line | |
| sys.exit() | |
| else: | |
| print line, | |
| def main(): | |
| if len(sys.argv) != 2: | |
| print 'Usage: %s FILE' % sys.argv[0] | |
| sys.exit() | |
| else: | |
| master = sys.argv[1] | |
| open_tex(master) | |
| if __name__ == '__main__': | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| def open_tex(file) | |
| dir = File.dirname(file) | |
| File.open(file, "r"){ |f| | |
| while line = f.gets | |
| case line | |
| when /^%/ | |
| next | |
| when /^\\(input|include)\{(\w+)\}$/ | |
| open_tex(dir + "/" + $2 + ".tex") | |
| else | |
| puts line | |
| end | |
| end | |
| } | |
| end | |
| def main | |
| if ARGV.length == 1 | |
| open_tex ARGV[0] | |
| else | |
| puts "Usage: #{$0} FILE" | |
| end | |
| end | |
| if __FILE__ == $0 | |
| main | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env gosh | |
| ; Merge multiple tex files into a single file. | |
| ; | |
| ; Usage: | |
| ; $ ./merge.scm FILE | |
| ; | |
| ; where FILE is the "master file" in latex. | |
| (define (usage) | |
| (format (current-error-port) | |
| "Usage: ~a FILE\n" *program-name*) | |
| (exit 2)) | |
| (define (merge-tex port) | |
| (with-input-from-port port | |
| (lambda () | |
| (port-for-each | |
| (lambda (line) | |
| (let ((m (rxmatch->string #/^\\(input|include)\{(\w+)\}$/ line 2))) | |
| (cond [(rxmatch #/^%/ line) #t] | |
| [(eq? m #f) (format #t "~a\n" line)] | |
| [else | |
| (call-with-input-file (string-append m ".tex") | |
| (lambda (f) (merge-tex f)))]))) | |
| read-line)))) | |
| (define (main args) | |
| (if (null? (cdr args)) | |
| (usage) | |
| (let ((tex-master (cadr args))) | |
| (call-with-input-file tex-master | |
| (lambda (f) (merge-tex f)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment