Utilisateur:Telperion/mediawiki-simple.el

Une page de Wikipédia, l'encyclopédie libre.
Capture d'écran

Le mode mediawiki-simple pour GNU Emacs est un mode majeur basé sur mediawiki-mode.el et disponible sous licence GNU GPL version 3.

Par-rapport au mediawiki-mode d'origine, seule la coloration syntaxique a été conservée. Ce mode majeur est donc destiné à être utilisé avec une extension du navigateur telle que « It's All Text! » pour Firefox, ou tout simplement en copiant/collant le texte à éditer entre une page de Wikipédia et GNU Emacs.

Installation[modifier | modifier le code]

Copiez-collez le code fourni dans la section suivante dans un fichier nommé mediawiki-simple.el. Puis insérez les lignes suivantes dans votre fichier .emacs :

(autoload 'mediawiki-mode "mediawiki-simple.el"
 "Major mode for editing MediaWiki pages." t)

Pour l'utilisation avec l'extension « It's All Text! », une ligne supplémentaire est nécessaire :

(add-to-list 'auto-mode-alist '("\\.wikipedia\\.org.*\\.txt\\'" . mediawiki-mode)

Code source[modifier | modifier le code]

;;; mediawiki-simple.el --- Simple major mode for editing MediaWiki pages

;; This file is NOT part of GNU Emacs.

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This is a stripped-down version of mediawiki-el (GPLv3), which can
;; be found at the following URL:
;; https://launchpad.net/mediawiki-el
;;
;; This major mode does only syntax highlighting, all other functions
;; (e.g. remote http connection) have been removed. It is intended to
;; be used either with a Firefox extension such as "It's all text!",
;; or by copy/pasting MediaWiki code into/from GNU Emacs by hand.

;;; Code:

(defgroup mediawiki-simple nil
  "A mode for editing MediaWiki pages."
  :tag "MediaWiki-simple"
  :group 'applications)

(defgroup mediawiki-simple-faces nil
  "Faces used by mediawiki-simple."
  :tag "MediaWiki-simple faces"
  :group 'mediawiki-simple)

(defvar font-mediawiki-italic-face 'italic
  "Face to use for italic text.")
(defvar font-mediawiki-bold-face 'bold
  "Face to use for bold text.")
(defvar font-mediawiki-link-face 'font-mediawiki-link-face
  "Face to use for link labels.")
(defvar font-mediawiki-link-brackets-face 'font-mediawiki-link-brackets-face
  "Face to use for link brackets and separators.")
(defvar font-mediawiki-url-face 'link
  "Face to use for external URLs.")
(defvar font-mediawiki-title1-face 'font-mediawiki-title1-face
  "Face to use for level-1 titles.")
(defvar font-mediawiki-title2-face 'font-mediawiki-title2-face
  "Face to use for level-2 titles.")
(defvar font-mediawiki-title3-face 'font-mediawiki-title3-face
  "Face to use for level-3 titles.")
(defvar font-mediawiki-title4-face 'font-mediawiki-title4-face
  "Face to use for level-4 titles.")
(defvar font-mediawiki-title5-face 'font-mediawiki-title5-face
  "Face to use for level-5 titles.")
(defvar font-mediawiki-math-face 'font-lock-keyword-face
  "Face to use for the math environment contents.")
(defvar font-mediawiki-tags-face 'font-mediawiki-tags-face
  "Face to use for tags.")

(defface font-mediawiki-bold-face
  '((t (:inherit 'bold)))
  "Face used to highlight text to be typeset in bold."
  :group 'mediawiki-simple-faces)

(defface font-mediawiki-italic-face
  '((t (:inherit 'italic)))
  "Face used to highlight text to be typeset in italic."
  :group 'mediawiki-simple-faces)

(defface font-mediawiki-link-face
  '((((background dark))  (:foreground "#5f87ff"))
    (((background light)) (:foreground "#005fff")))
  "Face used to highlight link labels."
  :group 'mediawiki-simple-faces)

(defface font-mediawiki-link-brackets-face
  '((((background dark))  (:foreground "#5f87ff"))
    (((background light)) (:foreground "#005fff")))
  "Face used to highlight link brackets and separators."
  :group 'mediawiki-simple-faces)

(defface font-mediawiki-title1-face
  '((t (:height 1.7 :inherit 'bold)))
  "Face used to highlight level-1 titles."
  :group 'mediawiki-simple-faces)

(defface font-mediawiki-title2-face
  '((t (:height 1.45 :inherit 'bold)))
  "Face used to highlight level-2 titles."
  :group 'mediawiki-simple-faces)

(defface font-mediawiki-title3-face
  '((t (:height 1.25 :inherit 'bold)))
  "Face used to highlight level-3 titles."
  :group 'mediawiki-simple-faces)

(defface font-mediawiki-title4-face
  '((t (:height 1.1 :inherit 'bold)))
  "Face used to highlight level-4 titles."
  :group 'mediawiki-simple-faces)

(defface font-mediawiki-title5-face
  '((t (:height 1.0 :inherit 'bold)))
  "Face used to highlight level-5 titles."
  :group 'mediawiki-simple-faces)

(defface font-mediawiki-tags-face
  '((t (:foreground "#d7875f")))
  "Face used to highlight tags."
  :group 'mediawiki-simple-faces)

(defvar mediawiki-url-protocols
  '("ftp" "gopher" "http" "https" "mailto" "news")
  "Valid protocols for URLs in Wikipedia articles.")

;; Syntax highlighting
(defvar mediawiki-font-lock-keywords
  (list
   ;; Apostrophe-style text markup
   ;; (bold)
   (cons "'''\\([^']\\|[^']'\\)*?\\('''\\|\n\n\\)"
         'font-mediawiki-bold-face)
   ;; (italic)
   (cons "''\\([^']\\|[^']'\\)*?\\(''\\|\n\n\\)"
         'font-mediawiki-italic-face)

   ;; Headers
   ;; (level-1)
   '("^\\(=\\)\\([^=\n]\\(?:[^\n]*[^=\n]\\)?\\)\\(=\\)$"
     (1 font-mediawiki-title1-face t t)
     (2 font-mediawiki-title1-face t t)
     (3 font-mediawiki-title1-face t t))
   ;; (level-2)
   '("^\\(==\\)\\([^=\n]\\(?:[^\n]*[^=\n]\\)?\\)\\(==\\)$"
     (1 font-mediawiki-title2-face t t)
     (2 font-mediawiki-title2-face t t)
     (3 font-mediawiki-title2-face t t))
   ;; (level-3)
   '("^\\(===\\)\\([^=\n]\\(?:[^\n]*[^=\n]\\)?\\)\\(===\\)$"
     (1 font-mediawiki-title3-face t t)
     (2 font-mediawiki-title3-face t t)
     (3 font-mediawiki-title3-face t t))
   ;; (level-4)
   '("^\\(====\\)\\([^=\n]\\(?:[^\n]*[^=\n]\\)?\\)\\(====\\)$"
     (1 font-mediawiki-title4-face t t)
     (2 font-mediawiki-title4-face t t)
     (3 font-mediawiki-title4-face t t))
   ;; (level-5)
   '("^\\(=====\\)\\([^=\n]\\(?:[^\n]*[^=\n]\\)?\\)\\(=====\\)$"
     (1 font-mediawiki-title5-face t t)
     (2 font-mediawiki-title5-face t t)
     (3 font-mediawiki-title5-face t t))

   ;; Bare URLs and ISBNs
   (cons (concat "\\(^\\| \\)" (regexp-opt mediawiki-url-protocols t)
                 "://[-A-Za-z0-9._\/~%+&#?!=()@]+")
         'font-lock-variable-name-face)
   (cons "\\(^\\| \\)ISBN [-0-9A-Z]+" 'font-lock-variable-name-face)

   ;; Colon indentation, lists, definitions, and tables
   (cons "^\\(:+\\|[*#]+\\||[}-]?\\|{|\\)" 'font-lock-builtin-face)
   (list "^\\(;\\)\\([^:\n]*\\)\\(:?\\)"
         '(1 font-lock-builtin-face)
         '(2 font-lock-keyword-face)
         '(3 font-lock-builtin-face))

   ;; Tags
   '("\\(</?\\)\\([^>]+\\)\\(>\\)"
     (1 font-mediawiki-tags-face t t)
     (2 font-mediawiki-tags-face t t)
     (3 font-mediawiki-tags-face t t))

   ;; Comments
   (cons (concat "<!-- ?\\([^->]\\|>\\|-\\([^-]\\|-[^>]\\)\\)*-->")
         '(0 font-lock-comment-face t t))

   ;; External Links
   (list (concat "\\(\\[\\)\\(\\(?:"
                 (regexp-opt mediawiki-url-protocols)
                 "\\)://[^ \n]+\\)\\(\\(?: [^]\n]*\\)?\\)\\(\\]\\)")
         '(1 font-mediawiki-link-brackets-face t t)
         '(2 font-mediawiki-url-face t t)
         '(3 font-mediawiki-link-face t t)
         '(4 font-mediawiki-link-brackets-face t t))

   ;; Wiki links
   '("\\(\\[\\[\\)\\([^]:]+:\\)?\\([^]|:]*\\(|\\)\\)?\\([^]]*\\)\\(\\]\\]\\)"
     (1 font-mediawiki-link-brackets-face t t)
     (2 font-lock-builtin-face t t)
     (3 font-mediawiki-link-face t t)
     (4 font-mediawiki-link-brackets-face t t)
     (5 font-mediawiki-link-face t t)
     (6 font-mediawiki-link-brackets-face t t))

   ;; Templates
   '("\\({{\\)\\(.+?\\)\\(}}\\)"
     (1 font-lock-builtin-face t t)
     (2 font-lock-builtin-face t t)
     (3 font-lock-builtin-face t t))

   ;; Parameters
   '("\\({{{\\)\\(.+?\\)\\(}}}\\)"
     (1 font-lock-builtin-face t t)
     (2 font-lock-variable-name-face t t)
     (3 font-lock-builtin-face t t))

   ;; Character entity references
   (cons "&#?[a-zA-Z0-9]+;" '(0 font-lock-type-face t t))

   ;; Preformatted text
   (cons "^ .*$" '(0 font-lock-constant-face t t))

   ;; Math environment (uniform highlight only, no TeX markup)
   (list "<math>\\(\\(\n?.\\)*\\)</math>"
         '(1 font-mediawiki-math-face t t))))

(define-derived-mode mediawiki-mode text-mode "MediaWiki"
  "Major mode for editing articles written in the markup language
used by Mediawiki."

  ;; MediaWiki text should not contain hard line breaks
  (visual-line-mode t)
  (auto-fill-mode nil)

  (set (make-local-variable 'adaptive-fill-regexp) "[ ]*")
  (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?-- *")
  (set (make-local-variable 'comment-end-skip) " *--\\([ \n]*>\\)?")
  (set (make-local-variable 'comment-start) "<!-- ")
  (set (make-local-variable 'comment-end) " -->")
  (set (make-local-variable 'paragraph-start)
       "\\*\\| \\|#\\|;\\|:\\||\\|!\\|$")
  (set (make-local-variable 'sentence-end-double-space) nil)
  (set (make-local-variable 'font-lock-multiline) t)
  (set (make-local-variable 'font-lock-defaults)
       '(mediawiki-font-lock-keywords t nil nil nil))
  (set (make-local-variable 'auto-fill-inhibit-regexp)
       "^[ *#:|;=]") ; FIXME: headers
  )

(provide 'mediawiki-simple)

;;; mediawiki-simple.el ends here