Commit 8c37dc08 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[CodeType] Adding Python for reading

parent 09e2e9d6
...@@ -109,6 +109,10 @@ ...@@ -109,6 +109,10 @@
font-family: Fira code, Fira Mono, Consolas, Menlo, Courier, monospace; font-family: Fira code, Fira Mono, Consolas, Menlo, Courier, monospace;
white-space: pre; white-space: pre;
} }
.code-editor .editor .html.language-python {
font-family: Fira code, Fira Mono, Consolas, Menlo, Courier, monospace;
white-space: pre;
}
.code-editor .editor .html.language-json { .code-editor .editor .html.language-json {
font-family: Fira code, Fira Mono, Consolas, Menlo, Courier, monospace; font-family: Fira code, Fira Mono, Consolas, Menlo, Courier, monospace;
white-space: pre; white-space: pre;
......
...@@ -89,6 +89,9 @@ ...@@ -89,6 +89,9 @@
&.language-haskell &.language-haskell
font-family: Fira code,Fira Mono,Consolas,Menlo,Courier,monospace font-family: Fira code,Fira Mono,Consolas,Menlo,Courier,monospace
white-space: pre white-space: pre
&.language-python
font-family: Fira code,Fira Mono,Consolas,Menlo,Courier,monospace
white-space: pre
&.language-json &.language-json
font-family: Fira code,Fira Mono,Consolas,Menlo,Courier,monospace font-family: Fira code,Fira Mono,Consolas,Menlo,Courier,monospace
white-space: pre white-space: pre
......
...@@ -28,7 +28,8 @@ type Code = String ...@@ -28,7 +28,8 @@ type Code = String
type Html = String type Html = String
type Error = String type Error = String
data CodeType = Haskell | JSON | Markdown data CodeType = Haskell | JSON | Markdown | Python
derive instance genericCodeType :: Generic CodeType _ derive instance genericCodeType :: Generic CodeType _
instance eqCodeType :: Eq CodeType where instance eqCodeType :: Eq CodeType where
eq = genericEq eq = genericEq
...@@ -56,6 +57,7 @@ codeNlFix _ c = if endsWith "\n" c then (c <> " ") else c ...@@ -56,6 +57,7 @@ codeNlFix _ c = if endsWith "\n" c then (c <> " ") else c
render :: CodeType -> Code -> Either Error Html render :: CodeType -> Code -> Either Error Html
render Haskell code = Right $ renderHaskell $ codeNlFix Haskell code render Haskell code = Right $ renderHaskell $ codeNlFix Haskell code
render Python code = Right $ renderPython $ codeNlFix Python code
render JSON code = result render JSON code = result
where where
parsedE = jsonParser code parsedE = jsonParser code
...@@ -67,8 +69,13 @@ render Markdown code = Right $ renderMd $ codeNlFix Markdown code ...@@ -67,8 +69,13 @@ render Markdown code = Right $ renderMd $ codeNlFix Markdown code
previewPostProcess :: CodeType -> Element -> Effect Unit previewPostProcess :: CodeType -> Element -> Effect Unit
previewPostProcess Haskell htmlEl = do previewPostProcess Haskell htmlEl = do
HLJS.highlightBlock htmlEl HLJS.highlightBlock htmlEl
previewPostProcess Python htmlEl = do
HLJS.highlightBlock htmlEl
previewPostProcess JSON htmlEl = do previewPostProcess JSON htmlEl = do
HLJS.highlightBlock htmlEl HLJS.highlightBlock htmlEl
previewPostProcess Markdown _ = pure unit previewPostProcess Markdown _ = pure unit
-- TODO Replace with markdown-it? -- TODO Replace with markdown-it?
...@@ -84,6 +91,10 @@ renderMd = renderMd' MD.defaultToMarkupOptions ...@@ -84,6 +91,10 @@ renderMd = renderMd' MD.defaultToMarkupOptions
renderHaskell :: String -> String renderHaskell :: String -> String
renderHaskell s = s renderHaskell s = s
renderPython :: String -> String
renderPython s = s
codeEditor :: Record Props -> R.Element codeEditor :: Record Props -> R.Element
codeEditor p = R.createElement codeEditorCpt p [] codeEditor p = R.createElement codeEditorCpt p []
...@@ -144,6 +155,7 @@ codeEditorCpt = R.hooksComponent "G.C.CE.CodeEditor" cpt ...@@ -144,6 +155,7 @@ codeEditorCpt = R.hooksComponent "G.C.CE.CodeEditor" cpt
langClass Haskell = " language-haskell" langClass Haskell = " language-haskell"
langClass JSON = " language-json" langClass JSON = " language-json"
langClass Markdown = " language-md" langClass Markdown = " language-md"
langClass Python = " language-python"
previewHidden :: ViewType -> String previewHidden :: ViewType -> String
previewHidden Preview = "" previewHidden Preview = ""
...@@ -244,7 +256,7 @@ codeTypeSelectorCpt = R.hooksComponent "G.C.CE.CodeTypeSelector" cpt ...@@ -244,7 +256,7 @@ codeTypeSelectorCpt = R.hooksComponent "G.C.CE.CodeTypeSelector" cpt
, on: { change: onSelectChange codeType onChange } , on: { change: onSelectChange codeType onChange }
, style: { width: "150px" } , style: { width: "150px" }
} }
(option <$> [Haskell, JSON, Markdown]) (option <$> [JSON, Markdown, Haskell, Python])
option :: CodeType -> R.Element option :: CodeType -> R.Element
option value = H.option { value: show value } [ H.text $ show value ] option value = H.option { value: show value } [ H.text $ show value ]
...@@ -255,6 +267,7 @@ codeTypeSelectorCpt = R.hooksComponent "G.C.CE.CodeTypeSelector" cpt ...@@ -255,6 +267,7 @@ codeTypeSelectorCpt = R.hooksComponent "G.C.CE.CodeTypeSelector" cpt
"Haskell" -> Haskell "Haskell" -> Haskell
"JSON" -> JSON "JSON" -> JSON
"Markdown" -> Markdown "Markdown" -> Markdown
"Python" -> Python
_ -> Markdown _ -> Markdown
setCodeType $ const codeType setCodeType $ const codeType
onChange codeType onChange codeType
......
...@@ -20,7 +20,7 @@ import Gargantext.Prelude ...@@ -20,7 +20,7 @@ import Gargantext.Prelude
import Gargantext.Components.CodeEditor as CE import Gargantext.Components.CodeEditor as CE
import Gargantext.Components.Node (NodePoly(..), HyperdataList) import Gargantext.Components.Node (NodePoly(..), HyperdataList)
import Gargantext.Components.Nodes.Corpus.Types (CorpusData, FTField, Field(..), FieldType(..), Hash, Hyperdata(..), defaultField, defaultHaskell', defaultJSON', defaultMarkdown') import Gargantext.Components.Nodes.Corpus.Types (CorpusData, FTField, Field(..), FieldType(..), Hash, Hyperdata(..), defaultField, defaultHaskell', defaultPython', defaultJSON', defaultMarkdown')
import Gargantext.Data.Array as GDA import Gargantext.Data.Array as GDA
import Gargantext.Hooks.Loader (useLoader) import Gargantext.Hooks.Loader (useLoader)
import Gargantext.Routes (SessionRoute(NodeAPI, Children)) import Gargantext.Routes (SessionRoute(NodeAPI, Children))
...@@ -312,25 +312,45 @@ fieldCodeEditorCpt = R.hooksComponent "G.C.N.C.fieldCodeEditorCpt" cpt ...@@ -312,25 +312,45 @@ fieldCodeEditorCpt = R.hooksComponent "G.C.N.C.fieldCodeEditorCpt" cpt
where where
cpt {field: Field {typ: typ@(Haskell {haskell})}, onChange} _ = do cpt {field: Field {typ: typ@(Haskell {haskell})}, onChange} _ = do
pure $ CE.codeEditor {code: haskell, defaultCodeType: CE.Haskell, onChange: changeCode onChange typ} pure $ CE.codeEditor {code: haskell, defaultCodeType: CE.Haskell, onChange: changeCode onChange typ}
cpt {field: Field {typ: typ@(Python {python})}, onChange} _ = do
pure $ CE.codeEditor {code: python, defaultCodeType: CE.Python, onChange: changeCode onChange typ}
cpt {field: Field {typ: typ@(JSON j)}, onChange} _ = do cpt {field: Field {typ: typ@(JSON j)}, onChange} _ = do
pure $ CE.codeEditor {code, defaultCodeType: CE.JSON, onChange: changeCode onChange typ} pure $ CE.codeEditor {code, defaultCodeType: CE.JSON, onChange: changeCode onChange typ}
where where
code = R2.stringify (encodeJson j) 2 code = R2.stringify (encodeJson j) 2
cpt {field: Field {typ: typ@(Markdown {text})}, onChange} _ = do cpt {field: Field {typ: typ@(Markdown {text})}, onChange} _ = do
pure $ CE.codeEditor {code: text, defaultCodeType: CE.Markdown, onChange: changeCode onChange typ} pure $ CE.codeEditor {code: text, defaultCodeType: CE.Markdown, onChange: changeCode onChange typ}
-- Perofrms the matrix of code type changes -- Performs the matrix of code type changes
-- (FieldType -> Effect Unit) is the callback function for fields array -- (FieldType -> Effect Unit) is the callback function for fields array
-- FieldType is the current element that we will modify -- FieldType is the current element that we will modify
-- CE.CodeType is the editor code type (might have been the cause of the trigger) -- CE.CodeType is the editor code type (might have been the cause of the trigger)
-- CE.Code is the editor code (might have been the cause of the trigger) -- CE.Code is the editor code (might have been the cause of the trigger)
changeCode :: (FieldType -> Effect Unit) -> FieldType -> CE.CodeType -> CE.Code -> Effect Unit changeCode :: (FieldType -> Effect Unit) -> FieldType -> CE.CodeType -> CE.Code -> Effect Unit
changeCode onc (Haskell hs) CE.Haskell c = onc $ Haskell $ hs { haskell = c } changeCode onc (Haskell hs) CE.Haskell c = onc $ Haskell $ hs { haskell = c }
changeCode onc (Haskell {haskell}) CE.JSON c = onc $ JSON $ defaultJSON' { desc = haskell } changeCode onc (Haskell hs) CE.Python c = onc $ Python $ defaultPython' { python = c }
changeCode onc (Haskell {haskell}) CE.JSON c = onc $ JSON $ defaultJSON' { desc = haskell }
changeCode onc (Haskell {haskell}) CE.Markdown c = onc $ Markdown $ defaultMarkdown' { text = haskell } changeCode onc (Haskell {haskell}) CE.Markdown c = onc $ Markdown $ defaultMarkdown' { text = haskell }
changeCode onc (Python hs) CE.Python c = onc $ Python $ hs { python = c }
changeCode onc (Python hs) CE.Haskell c = onc $ Haskell $ defaultHaskell' { haskell = c }
changeCode onc (Python {python}) CE.JSON c = onc $ JSON $ defaultJSON' { desc = python }
changeCode onc (Python {python}) CE.Markdown c = onc $ Markdown $ defaultMarkdown' { text = python }
changeCode onc (Markdown md) CE.Haskell c = onc $ Haskell $ defaultHaskell' { haskell = c }
changeCode onc (Markdown md) CE.Python c = onc $ Python $ defaultPython' { python = c }
changeCode onc (Markdown md) CE.JSON c = onc $ Markdown $ defaultMarkdown' { text = c }
changeCode onc (Markdown md) CE.Markdown c = onc $ Markdown $ md { text = c }
changeCode onc (JSON j@{desc}) CE.Haskell c = onc $ Haskell $ defaultHaskell' { haskell = haskell } changeCode onc (JSON j@{desc}) CE.Haskell c = onc $ Haskell $ defaultHaskell' { haskell = haskell }
where where
haskell = R2.stringify (encodeJson j) 2 haskell = R2.stringify (encodeJson j) 2
changeCode onc (JSON j@{desc}) CE.Python c = onc $ Python $ defaultPython' { python = toCode }
where
toCode = R2.stringify (encodeJson j) 2
changeCode onc (JSON j) CE.JSON c = do changeCode onc (JSON j) CE.JSON c = do
case jsonParser c of case jsonParser c of
Left err -> log2 "[fieldCodeEditor'] cannot parse json" c Left err -> log2 "[fieldCodeEditor'] cannot parse json" c
...@@ -340,9 +360,9 @@ changeCode onc (JSON j) CE.JSON c = do ...@@ -340,9 +360,9 @@ changeCode onc (JSON j) CE.JSON c = do
changeCode onc (JSON j) CE.Markdown c = onc $ Markdown $ defaultMarkdown' { text = text } changeCode onc (JSON j) CE.Markdown c = onc $ Markdown $ defaultMarkdown' { text = text }
where where
text = R2.stringify (encodeJson j) 2 text = R2.stringify (encodeJson j) 2
changeCode onc (Markdown md) CE.Haskell c = onc $ Haskell $ defaultHaskell' { haskell = c }
changeCode onc (Markdown md) CE.JSON c = onc $ Markdown $ defaultMarkdown' { text = c }
changeCode onc (Markdown md) CE.Markdown c = onc $ Markdown $ md { text = c }
type LoadProps = type LoadProps =
( nodeId :: Int ( nodeId :: Int
......
...@@ -52,6 +52,10 @@ data FieldType = ...@@ -52,6 +52,10 @@ data FieldType =
Haskell { haskell :: HaskellCode Haskell { haskell :: HaskellCode
, tag :: Tag , tag :: Tag
} }
| Python { python :: HaskellCode
, tag :: Tag
}
| JSON { authors :: Author | JSON { authors :: Author
, desc :: Description , desc :: Description
, query :: Query , query :: Query
...@@ -103,6 +107,12 @@ instance decodeFTField :: DecodeJson (Field FieldType) where ...@@ -103,6 +107,12 @@ instance decodeFTField :: DecodeJson (Field FieldType) where
haskell <- data_ .: "haskell" haskell <- data_ .: "haskell"
tag <- data_ .: "tag" tag <- data_ .: "tag"
pure $ Haskell {haskell, tag} pure $ Haskell {haskell, tag}
"Python" -> do
python <- data_ .: "python"
tag <- data_ .: "tag"
pure $ Python {python, tag}
"JSON" -> do "JSON" -> do
authors <- data_ .: "authors" authors <- data_ .: "authors"
desc <- data_ .: "desc" desc <- data_ .: "desc"
...@@ -124,8 +134,9 @@ instance encodeFTField :: EncodeJson (Field FieldType) where ...@@ -124,8 +134,9 @@ instance encodeFTField :: EncodeJson (Field FieldType) where
~> "type" := typ' typ ~> "type" := typ' typ
~> jsonEmptyObject ~> jsonEmptyObject
where where
typ' (Haskell _) = "Haskell" typ' (Haskell _) = "Haskell"
typ' (JSON _) = "JSON" typ' (Python _) = "Python"
typ' (JSON _) = "JSON"
typ' (Markdown _) = "Markdown" typ' (Markdown _) = "Markdown"
instance encodeFieldType :: EncodeJson FieldType where instance encodeFieldType :: EncodeJson FieldType where
...@@ -133,6 +144,12 @@ instance encodeFieldType :: EncodeJson FieldType where ...@@ -133,6 +144,12 @@ instance encodeFieldType :: EncodeJson FieldType where
"haskell" := haskell "haskell" := haskell
~> "tag" := "HaskellField" ~> "tag" := "HaskellField"
~> jsonEmptyObject ~> jsonEmptyObject
encodeJson (Python {python}) =
"python" := python
~> "tag" := "PythonField"
~> jsonEmptyObject
encodeJson (JSON {authors, desc, query, tag, title}) = encodeJson (JSON {authors, desc, query, tag, title}) =
"authors" := authors "authors" := authors
~> "desc" := desc ~> "desc" := desc
...@@ -145,6 +162,14 @@ instance encodeFieldType :: EncodeJson FieldType where ...@@ -145,6 +162,14 @@ instance encodeFieldType :: EncodeJson FieldType where
~> "text" := text ~> "text" := text
~> jsonEmptyObject ~> jsonEmptyObject
defaultPython :: FieldType
defaultPython = Python defaultPython'
defaultPython' :: { python :: String, tag :: String }
defaultPython' = { python: "import Foo"
, tag : "PythonField"
}
defaultHaskell :: FieldType defaultHaskell :: FieldType
defaultHaskell = Haskell defaultHaskell' defaultHaskell = Haskell defaultHaskell'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment