@@ -1,4 +1,4 @@
1−{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
1+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, OverloadedStrings #-}
22{-
33Copyright (C) 2016-17 John MacFarlane <jgm@berkeley.edu>
44
@@ -32,6 +32,7 @@ and info messages.
3232module Text.Pandoc.Logging (
3333 Verbosity(..)
3434 , LogMessage(..)
35+ , showLogMessage
3536 , messageVerbosity
3637 ) where
3738
@@ -39,18 +40,25 @@ import Text.Parsec.Pos
3940import Data.Data (Data)
4041import Data.Generics (Typeable)
4142import GHC.Generics (Generic)
43+import qualified Data.Text as Text
44+import Data.Aeson
45+import Text.Pandoc.Definition
4246
4347-- | Verbosity level.
4448data Verbosity = ERROR | WARNING | INFO | DEBUG
4549 deriving (Show, Read, Eq, Data, Enum, Ord, Bounded, Typeable, Generic)
4650
51+instance ToJSON Verbosity where
52+ toJSON x = toJSON (show x)
53+
4754data LogMessage =
4855 SkippedInput String SourcePos
49− | NotRendered String
5056 | YamlSectionNotAnObject SourcePos
5157 | DuplicateLinkReference String SourcePos
5258 | DuplicateNoteReference String SourcePos
5359 | ParsingUnescaped String SourcePos
60+ | InlineNotRendered Inline
61+ | BlockNotRendered Block
5462 | DocxCommentWillNotRetainFormatting String
5563 | CouldNotFetchResource String String
5664 | CouldNotDetermineImageSize String
@@ -58,15 +66,100 @@ data LogMessage =
5866 | CouldNotConvertTeXMath String
5967 deriving (Show, Eq, Data, Ord, Typeable, Generic)
6068
69+instance ToJSON LogMessage where
70+ toJSON x = object $ "verbosity" .= toJSON (messageVerbosity x) :
71+ case x of
72+ SkippedInput s pos ->
73+ ["type" .= String "SkippedInput",
74+ "contents" .= Text.pack s,
75+ "source" .= Text.pack (sourceName pos),
76+ "line" .= sourceLine pos,
77+ "column" .= sourceColumn pos]
78+ YamlSectionNotAnObject pos ->
79+ ["type" .= String "YamlSectionNotAnObject",
80+ "source" .= Text.pack (sourceName pos),
81+ "line" .= toJSON (sourceLine pos),
82+ "column" .= toJSON (sourceColumn pos)]
83+ DuplicateLinkReference s pos ->
84+ ["type" .= String "DuplicateLinkReference",
85+ "contents" .= Text.pack s,
86+ "source" .= Text.pack (sourceName pos),
87+ "line" .= toJSON (sourceLine pos),
88+ "column" .= toJSON (sourceColumn pos)]
89+ DuplicateNoteReference s pos ->
90+ ["type" .= String "DuplicateNoteReference",
91+ "contents" .= Text.pack s,
92+ "source" .= Text.pack (sourceName pos),
93+ "line" .= toJSON (sourceLine pos),
94+ "column" .= toJSON (sourceColumn pos)]
95+ ParsingUnescaped s pos ->
96+ ["type" .= String "ParsingUnescaped",
97+ "contents" .= Text.pack s,
98+ "source" .= Text.pack (sourceName pos),
99+ "line" .= toJSON (sourceLine pos),
100+ "column" .= toJSON (sourceColumn pos)]
101+ InlineNotRendered il ->
102+ ["type" .= String "InlineNotRendered",
103+ "contents" .= toJSON il]
104+ BlockNotRendered bl ->
105+ ["type" .= String "BlockNotRendered",
106+ "contents" .= toJSON bl]
107+ DocxCommentWillNotRetainFormatting s ->
108+ ["type" .= String "DocxCommentWillNotRetainFormatting",
109+ "commentId" .= Text.pack s]
110+ CouldNotFetchResource fp s ->
111+ ["type" .= String "CouldNotFetchResource",
112+ "path" .= Text.pack fp,
113+ "message" .= Text.pack s]
114+ CouldNotDetermineImageSize fp ->
115+ ["type" .= String "CouldNotDetermineImageSize",
116+ "path" .= Text.pack fp]
117+ CouldNotDetermineMimeType fp ->
118+ ["type" .= String "CouldNotDetermineMimeType",
119+ "path" .= Text.pack fp]
120+ CouldNotConvertTeXMath s ->
121+ ["type" .= String "CouldNotConvertTeXMath",
122+ "contents" .= Text.pack s]
123+
124+showLogMessage :: LogMessage -> String
125+showLogMessage msg =
126+ case msg of
127+ SkippedInput s pos ->
128+ "Skipped '" ++ s ++ "' at " ++ show pos
129+ YamlSectionNotAnObject pos ->
130+ "YAML metadata section is not an object at " ++ show pos
131+ DuplicateLinkReference s pos ->
132+ "Duplicate link reference '" ++ s ++ "' at " ++ show pos
133+ DuplicateNoteReference s pos ->
134+ "Duplicate note reference '" ++ s ++ "' at " ++ show pos
135+ ParsingUnescaped s pos ->
136+ "Parsing unescaped '" ++ s ++ "' at " ++ show pos
137+ InlineNotRendered il ->
138+ "Not rendering " ++ show il
139+ BlockNotRendered bl ->
140+ "Not rendering " ++ show bl
141+ DocxCommentWillNotRetainFormatting s ->
142+ "Docx comment with id '" ++ s ++ "' will not retain formatting"
143+ CouldNotFetchResource fp s ->
144+ "Could not fetch resource '" ++ fp ++ "'" ++
145+ if null s then "" else (": " ++ s)
146+ CouldNotDetermineImageSize fp ->
147+ "Could not determine image size for '" ++ fp ++ "'"
148+ CouldNotDetermineMimeType fp ->
149+ "Could not determine mime type for '" ++ fp ++ "'"
150+ CouldNotConvertTeXMath s ->
151+ "Could not convert TeX math '" ++ s ++ "', rendering as TeX"
152+
61153messageVerbosity:: LogMessage -> Verbosity
62154messageVerbosity msg =
63155 case msg of
64156 SkippedInput{} -> INFO
65− NotRendered{} -> INFO
66157 YamlSectionNotAnObject{} -> WARNING
67158 DuplicateLinkReference{} -> WARNING
68159 DuplicateNoteReference{} -> WARNING
69160 ParsingUnescaped{} -> INFO
161+ InlineNotRendered{} -> INFO
162+ BlockNotRendered{} -> INFO
70163 DocxCommentWillNotRetainFormatting{} -> INFO
71164 CouldNotFetchResource{} -> WARNING
72165 CouldNotDetermineImageSize{} -> WARNING