Guest User

Untitled

a guest
Mar 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. #|
  2. | Event extract script
  3. |
  4. | Copyright (c) 2018, LdBeth Wang
  5. | All rights reserved.
  6. |
  7. | Redistribution and use in source and binary forms, with or without
  8. | modification, are permitted provided that the following conditions are
  9. | met:
  10. |
  11. | Redistribution of source code must retain the above copyright notice,
  12. | this list of conditions and the following disclaimer. Redistribution
  13. | in binary form must reproduce the above copyright notice, this list of
  14. | conditions and the following disclaimer in the documentation and/or
  15. | other materials provided with the distribution. THIS SOFTWARE IS
  16. | PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  17. | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.)
  27. |
  28. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|#
  29.  
  30. (ql:quickload :cl21)
  31. (in-package :cl21-user)
  32. (use-package :cl21.re)
  33.  
  34. ;;(defvar *file*)
  35.  
  36. (defparameter *index-output* #P"~/Desktop/foo.txt")
  37.  
  38. (defun explain ()
  39. (format t "~{# ~A~%~}"
  40. '("这是自动生成的文件,请不要把格式玩坏。"
  41. "``#'' 号开头的行是参考文本。"
  42. "请把对应的翻译文本填入两行 ``@'' 号之间。"
  43. "控制字符请照抄。"
  44. "整段重复的文本只要填一次,其它地方可直接留空。"
  45. "有问題找工作群的 @東方記者"))
  46. (dotimes (i 2)
  47. (princ #"\n")))
  48.  
  49. (defmacro safe-1st (array)
  50. `(if ,array
  51. (elt (nth-value 1 ,array) 0)
  52. nil))
  53.  
  54. (declaim (inline find-name print-name print-name*
  55. read-dialog print-dialog
  56. search-sep))
  57.  
  58. (defun find-name (string)
  59. (safe-1st (#/\s?◆文章 :(.+):/ string)))
  60.  
  61. (defun print-name (name)
  62. (format t "\"~A\"=\"\"~%" name))
  63.  
  64. (defun print-name* (name)
  65. (format t "# -- ~A~%" name))
  66. (defun read-dialog (string)
  67. (safe-1st (#/^[ \| ]{2,}:([^◇]\S+)\s*/ string)))
  68.  
  69.  
  70. (defun print-dialog (stream)
  71. (format t ":\"~A\"~%@~%@~%~%" (get-output-stream-string stream)))
  72. (defun search-sep (string)
  73. (safe-1st (#/地图(ID: \d+)\s/ string)))
  74. (defun index (filename)
  75. (declare (inline find))
  76. (with-open-file (in filename
  77. :direction :input)
  78. (with-open-file (*standard-output* *index-output*
  79. :direction :output
  80. :if-does-not-exist :create
  81. :if-exists :supersede)
  82.  
  83. ;; Explain first.
  84. (explain)
  85.  
  86. (let ((name-dic (make-hash-table :test 'equal :size 220))
  87. *string*
  88. *state*)
  89. ;; Make dynamic binding.
  90. (declare (special *string* *state*))
  91.  
  92. (loop for field = (read-line in nil)
  93. while field
  94. do (let* ((name (if *state*
  95. nil
  96. (find-name field)))
  97. (dialog (if name
  98. nil
  99. (read-dialog field)))
  100. (sep (if (not (or name dialog))
  101. (search-sep field))))
  102.  
  103. (cond (sep (format t "~A~%------~%" sep))
  104. (name (if (gethash name-dic name)
  105. (print-name* name)
  106. (progn (setf (gethash name-dic name) t)
  107. (print-name name))))
  108. ;; set enter dialog state
  109. ((and dialog (not *state*))
  110. (setf *state* t)
  111. (setf *string* (make-string-output-stream)))
  112. ((and *state* (not dialog))
  113. (setf *state* nil)
  114. (print-dialog *string*)))
  115. (when (and *state* dialog)
  116. (princ #"${dialog}\n" *string*))))))))
  117.  
  118.  
  119. (defparameter *final-output* #P"~/Desktop/pp.txt")
  120.  
  121. (defparameter *line-width* 40)
  122.  
  123. (defvar *buffer*)
  124. (defvar *replacement*)
  125. #|
  126. (defclass replace ()
  127. ((match :initarg :match)
  128. (content :initarg :content)))
  129.  
  130. (defclass name-replace (replace) nil)
  131.  
  132. (defclass content-replace (replace) nil)
  133.  
  134. (defclass poem-replace (replace) nil)
  135. |#
  136. (defparameter *name-regexp* "◆文章 :(\\S+):")
  137. (defparameter *name-match-regexp* #/\"(.+)\"=\"(.+)\"/ )
  138. (defparameter *field-match-regexp* #/:\"(.{1,5}).*\"\n=\"(.+)\"/ )
  139. ;; (defparameter *field-base* "(?m)^([ \|    ]):~A\\S*[\\n[\\s\| ]+:\\S+]*")
  140. (defparameter *field-base* "( ):~A\\S*[\\n :\\S+]*?\\n◆")
  141. ;; (defconstant +context-regexp+ "\\S+[\\n[\\s\| ]+:\\S+]*")
  142. ;;(defmethod )
  143. ;; (re-replace ":那个莫名其\\S+[\\n[\\s\| ]+:\\S+]*" *buffer* ":ss" :global t)
  144.  
  145. ;; (defconstant)
  146.  
  147. (defun print-content (indent list)
  148. (format nil #"~{${indent}:~A~%~}◆" list))
  149.  
  150. (defun read-input (infile)
  151. (with-open-file (in infile
  152. :direction :input)
  153. (let ((content (make-string (file-length in))))
  154. (read-sequence content in)
  155. (setf *buffer* (string-right-trim '(#\Null) content))
  156. (values))))
  157.  
  158. (defun binary-find-entry (item vector
  159. &key
  160. (predicate #'<) (test #'eql))
  161. "Return found entry and index number."
  162. (declare (simple-vector vector))
  163. (labels ((find-elt (low up)
  164. (declare (fixnum low up))
  165. (let* ((med (ash (+ up low) -1))
  166. (cur (elt vector med)))
  167. (cond
  168. ((funcall test item cur)
  169. (values cur med))
  170. ((= low up)
  171. (values nil med))
  172. ((funcall predicate item cur)
  173. (find-elt low med))
  174. (t
  175. (find-elt (1+ med) up))))))
  176. (find-elt 0 (1- (length vector)))))
  177.  
  178. (defun greedy-wrap (str width)
  179. (setq str (concatenate 'string str " ")) ; add sentinel
  180. (do* ((len (length str))
  181. (lines nil)
  182. (begin-curr-line 0)
  183. (prev-space 0 pos-space)
  184. (pos-space (position #\Space str)
  185. (when (< (1+ prev-space)
  186. len)
  187. (position #\Space str :start (1+ prev-space)))))
  188.  
  189. ((null pos-space)
  190. (progn (push (subseq str begin-curr-line (1- len))
  191. lines)
  192. (nreverse lines)))
  193. (when (> (- pos-space begin-curr-line) width)
  194. (push (subseq str begin-curr-line prev-space) lines)
  195. (setq begin-curr-line (1+ prev-space)))))
  196.  
  197. (defun vector-to-list (vector)
  198. (cl:map 'list #'identity vector))
  199.  
  200. (defun strip-comments (str)
  201. (re-replace #/#.*$/mg str ""))
  202.  
  203. (defun parse-dialog-entry (str)
  204. (let ((table (make-hash-table :test 'equal)))
  205. (cl:mapc ^(let ((x (nth-value 1 (funcall *field-match-regexp* %))))
  206. (setf (gethash table (elt x 0)) (elt x 1)))
  207. (ppcre:all-matches-as-strings
  208. ":\".*\"\n=\".+\"" str))
  209. table))
  210.  
  211. (defun parse-name-entry (str)
  212. (apply #'hash-table 'equal
  213. (apply #'(compose vector-to-list concatenate) 'vector
  214. (cl:map 'list ^(nth-value 1 (funcall *name-match-regexp* %))
  215. (ppcre:all-matches-as-strings
  216. "\".+\"=\".+\"" str)))))
  217.  
  218. (defun do-name-replace (str table)
  219. (ppcre:regex-replace-all *name-regexp*
  220. str
  221. ^(format nil "◆文章 :~A:" (gethash table %2))
  222. :simple-calls t))
  223.  
  224. (defun do-dialog-replace (str table)
  225. (let ((var (copy-array str)))
  226. (cl:maphash
  227. #'(lambda (key val)
  228. (setf var (ppcre:regex-replace-all
  229. (format nil *field-base* (ppcre:quote-meta-chars key))
  230. var
  231. ^(print-content %2 (greedy-wrap val *line-width*))
  232. :simple-calls t)))
  233. table)
  234. var))
  235.  
  236. #|
  237. (defun intellectual-wrap-english (str)
  238. (let ((proper-list ()))
  239. ;;(setq str (concatenate 'string str " "))
  240. #|first match if control code, if true then make a index to record property.
  241. then arrange with greedy-wrap, then recover property, then print|#))
  242.  
  243. (defun wrap-with-control (string)
  244. nil)
  245. |#
  246.  
  247. (defun inject (input source)
  248. (read-input input)
  249. (let ((name-table (parse-name-entry *buffer*))
  250. (dialog-table (parse-dialog-entry *buffer*)))
  251. (read-input source)
  252. (setf *buffer* (do-name-replace *buffer* name-table))
  253. (setf *buffer* (do-dialog-replace *buffer* dialog-table))
  254. (with-open-file (*standard-output* *final-output*
  255. :direction :output
  256. :if-does-not-exist :create
  257. :if-exists :supersede)
  258. (princ *buffer*)))
  259. nil)
  260.  
  261. ;; LocalWords: binding
Add Comment
Please, Sign In to add comment