Commits

Luke Plant  committed 6d39361

Added slug generation to migration script

  • Participants
  • Parent commits 0c02f16

Comments (0)

Files changed (3)

 if [ ! "$TARGETSTEM" == "$TARGET" ]
 then
 	mv $TARGET /home/luke/httpd/lukeplant.me.uk/web/cgi-bin/$TARGET
+else
+	mv $TARGET ..
 fi

File src/Migrate.hs

+import qualified Data.ByteString.Char8 as B
+import qualified Data.ByteString.UTF8 as UTF8
 import qualified Category as C
 import qualified Post as P
 import qualified Settings
 import qualified Data.Map as Map
 import Data.Maybe (fromJust)
 import Data.Ord (comparing)
+import GHC.Unicode (toLower)
 import Monad (liftM)
+import Utils (regexReplace)
 -- Migration script for the old data
 
 -- Read a table of newline/tab delimited data,
                      toSql $ C.name c]
                     >> return c
 
-makeSlug = id -- TODO
+slugFromTitle title = map toLower $ UTF8.toString $
+                      regexReplace (B.pack "-+$") (B.pack "") $
+                      regexReplace (B.pack "[^A-Za-z0-9]+") (B.pack "-") (B.pack title)
+
+makeSlug cn p = return $ slugFromTitle (P.title p)
 
 readPosts = makeItems "posts.txt" mkPost
             >>= mapM addFullText
                              return p { P.post_raw = f, P.post_formatted = f }
 
 addPost cn p = do { slug <- makeSlug cn p;
-                    let p = p { P.slug = slug } in
+                    p2 <- return $ p { P.slug = slug };
                     DB.doInsert cn "posts"
                     ["title",
                      "slug",
                      "format_id",
                      "timestamp",
                      "comments_open"]
-                    [toSql $ P.title p,
-                     toSql $ P.slug p,
-                     toSql $ P.post_raw p,
-                     toSql $ P.post_formatted p,
-                     toSql $ P.summary_raw p,
-                     toSql $ P.summary_formatted p,
-                     toSql $ P.format_id p,
-                     toSql $ P.timestamp p,
-                     toSql $ P.comments_open p];
+                    [toSql $ P.title p2,
+                     toSql $ P.slug p2,
+                     toSql $ P.post_raw p2,
+                     toSql $ P.post_formatted p2,
+                     toSql $ P.summary_raw p2,
+                     toSql $ P.summary_formatted p2,
+                     toSql $ P.format_id p2,
+                     toSql $ P.timestamp p2,
+                     toSql $ P.comments_open p2];
                     [[newid]] <- quickQuery cn "SELECT last_insert_rowid();" [];
-                    return p { P.id = fromSql $ newid } ;
+                    return p2 { P.id = fromSql $ newid } ;
                   }
 
 readPostCategories = makeItems "postcategories.txt" mkPostCategory

File src/Utils.hs

+{-# OPTIONS_GHC -fbang-patterns  #-}
+module Utils where
+
+import qualified Data.ByteString.Char8 as B
+import System.Environment(getArgs)
+import Text.Regex.PCRE
+import Text.Regex.Base
+import Data.Char
+
+regexReplace !re !rep !source = go source []
+ where go str res =
+         if B.null str
+             then B.concat . reverse $ res
+             else case (str =~~ re) :: Maybe (B.ByteString, B.ByteString, B.ByteString) of
+               Nothing -> B.concat . reverse $ (str:res)
+               Just (bef, _ , aft) -> go aft (rep:bef:res)