From 1d71c27a34de01693ad3c3a8e93b3e204a2a1895 Mon Sep 17 00:00:00 2001
From: Mitchell Hanberg <mitch@mitchellhanberg.com>
Date: Fri, 22 Nov 2019 22:48:20 -0500
Subject: [PATCH] Handle HTML fragments in temple.convert task (#35)

* Handle HTML fragments in temple.convert task

* Add GH CI action

* Remove travis config
---
 .github/workflows/ci.yml               | 62 ++++++++++++++++++++++++++
 .travis.yml                            | 16 -------
 lib/temple/html_to_temple.ex           |  4 +-
 test/mix/tasks/html_to_temple_test.exs | 55 +++++++++++++++++++++++
 4 files changed, 120 insertions(+), 17 deletions(-)
 create mode 100644 .github/workflows/ci.yml
 delete mode 100644 .travis.yml

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 0000000..65fc1b6
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,62 @@
+name: CI
+on:
+  pull_request:
+  push:
+    branches: master
+
+jobs:
+  tests:
+    runs-on: ubuntu-latest
+    name: Test (${{matrix.elixir}}/${{matrix.otp}})
+
+    strategy:
+      matrix:
+        otp: [21.x]
+        elixir: [1.7.x, 1.8.x, 1.9.x]
+
+    steps:
+    - uses: actions/checkout@v1
+    - uses: actions/setup-elixir@v1.0.0
+      with:
+        otp-version: ${{matrix.otp}}
+        elixir-version: ${{matrix.elixir}}
+    - uses: actions/cache@v1
+      id: cache
+      with:
+        path: deps
+        key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
+        restore-keys: |
+          ${{ runner.os }}-mix-
+
+    - name: Install Dependencies
+      if: steps.cache.outputs.cache-hit != 'true'
+      run: mix deps.get
+
+    - name: Run Tests
+      run: mix test
+
+  formatter:
+    runs-on: ubuntu-latest
+    name: Formatter (1.9.x/21.x)
+
+    steps:
+    - uses: actions/checkout@v1
+    - uses: actions/setup-elixir@v1.0.0
+      with:
+        otp-version: 21.x
+        elixir-version: 1.9.x
+    - uses: actions/cache@v1
+      id: cache
+      with:
+        path: deps
+        key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
+        restore-keys: |
+          ${{ runner.os }}-mix-
+
+    - name: Install Dependencies
+      if: steps.cache.outputs.cache-hit != 'true'
+      run: mix deps.get
+
+    - name: Run Formatter
+      run: mix format --check-formatted
+
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 4d54968..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,16 +0,0 @@
-language: elixir
-elixir:
-  - 1.7.4
-  - 1.8.2
-  - 1.9.0
-otp_release:
-  - 20.3
-  - 21.3
-  - 22.0
-sudo: false
-cache:
-  directories:
-    - _build
-    - deps
-script:
-  - mix test
diff --git a/lib/temple/html_to_temple.ex b/lib/temple/html_to_temple.ex
index e8d6de7..ca09ea1 100644
--- a/lib/temple/html_to_temple.ex
+++ b/lib/temple/html_to_temple.ex
@@ -8,7 +8,9 @@ defmodule Temple.HtmlToTemple do
     result =
       doc
       |> Floki.parse()
-      |> do_parse(0)
+      |> List.wrap()
+      |> Enum.map(&do_parse(&1, 0))
+      |> Enum.join("\n")
 
     {:ok, result}
   end
diff --git a/test/mix/tasks/html_to_temple_test.exs b/test/mix/tasks/html_to_temple_test.exs
index 00809c5..a94f737 100644
--- a/test/mix/tasks/html_to_temple_test.exs
+++ b/test/mix/tasks/html_to_temple_test.exs
@@ -86,4 +86,59 @@ defmodule Mix.Tasks.HtmlToTempleTest do
            end
            """
   end
+
+  test "parses HTML fragments" do
+    html = """
+    <section class="phx-hero">
+      <h1><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
+      <p>A productive web framework that<br/>
+        does not compromise speed or maintainability.</p>
+    </section>
+    <section class="row">
+      <article class="column">
+        <h2>Resources</h2>
+        <ul>
+          <li>
+            <a href="https://hexdocs.pm/phoenix/overview.html">Guides &amp; Docs</a>
+          </li>
+        </ul>
+      </article>
+    </section>
+    """
+
+    {:ok, result} = Temple.HtmlToTemple.parse(html)
+
+    assert result === """
+           section class: "phx-hero" do
+             h1 do
+               text "<%= gettext \"Welcome to %{name}!\", name: \"Phoenix\" %>"
+             end
+
+             p do
+               text "A productive web framework that"
+
+               br()
+
+               text "
+               does not compromise speed or maintainability."
+             end
+           end
+
+           section class: "row" do
+             article class: "column" do
+               h2 do
+                 text "Resources"
+               end
+
+               ul do
+                 li do
+                   a href: "https://hexdocs.pm/phoenix/overview.html" do
+                     text "Guides & Docs"
+                   end
+                 end
+               end
+             end
+           end
+           """
+  end
 end