REBOL [
    Title: "Create file/folder index card"
    Purpose: {Create an index card for a specified file and store
    the index card in a well-known place.}
]

;; [---------------------------------------------------------------------------]
;; [ This is a program for making a personal "index card" collection for       ]
;; [ help in finding files.  Basically, you select a file or folder            ]
;; [ you want to index, and the program makes a text file containing           ]
;; [ indexing information in a REBOL-readable format.  Other programs          ]
;; [ can read the index cards and make s primitive "card catalog"              ]
;; [ so to speak.                                                              ]
;; [---------------------------------------------------------------------------]

;; [---------------------------------------------------------------------------]
;; [ This is an object that can be included in other programs and be used      ]
;; [ to create an "index card" for a file or directory.                        ]
;; [ To use it, assign values to the words below, and call the function        ]
;; [ CREATE-CARD.  The needed values for the index card are:                   ]
;; [                                                                           ]
;; [ ICVERSION: This is a hard-coded literal that might be used in some        ]
;; [     future plan for multiple versions of the index card.                  ]
;; [ ITEM-ID: The actual name of the file or folder being indexed.             ]
;; [     This should be a REBOL "file" data type.                              ]
;; [ ITEM-TYPE: The string "File" or "Folder."                                 ]
;; [ ITEM-LOC:  The full path, in REBOL format, to the ITEM-ID.                ]
;; [ ITEM-TITLE: A one-line string of any descriptive title-like text.         ]
;; [ KEWORDS: A block of strings which are keywords for searching.             ]
;; [ DESCRIPTION:  Multi-line string of any useful descriptive text.           ]
;; [                                                                           ]
;; [ Another needed value to make the function work is:                        ]
;; [                                                                           ]
;; [ ITEM-ID-BASE:  This is any string that will be used as part of the        ]
;; [     name of the index card. Usually, this would be the name of the file   ]
;; [     or folder being indexed, so it would be the same as ITEM-ID.          ]
;; [     However, this should be a string and not a "file" data type.          ]
;; [---------------------------------------------------------------------------]

INDEXCARD: make object! [

;; Home of the catalog.  Modify for your own situation. 
    CARD-CATALOG: %/COB-ISAPPS/COMMON/StevesCardCatalog/
    INDEXITEM-ID: none

;; Items we put in an index card.
    ICVERSION: "ICV201801"
    ITEM-ID: none
    ITEM-TYPE: none
    ITEM-LOC: none
    ITEM-TITLE: none
    KEYWORDS: none
    DESCRIPTION: none
    ITEM-ID-BASE: none

;; Items we provide in case the caller wants to use them for something
;; like making a GUI window.  This is a list of all index cards,
;; in a block and a string.
    CARD-LIST: []
    CARD-LIST-STRING: ""

;; Template for an index card.
;; Don't know why but the "INDEXCARD/" seems to be required.
CARD-TEMPLATE: {ICVERSION: <% mold INDEXCARD/ICVERSION %>
ITEM-ID: <% mold INDEXCARD/ITEM-ID %>
ITEM-TYPE: <% mold INDEXCARD/ITEM-TYPE %>
ITEM-LOC: <% mold INDEXCARD/ITEM-LOC %> 
ITEM-TITLE: <% mold INDEXCARD/ITEM-TITLE %>
KEYWORDS: <% mold INDEXCARD/KEYWORDS %>
DESCRIPTION: <% mold INDEXCARD/DESCRIPTION %>
}

;; Create an index card out of data items provided.    
    CREATE-CARD: does [
        INDEXITEM-ID: to-file rejoin [
            CARD-CATALOG
            ITEM-ID-BASE
            ".indexcard"
        ] 
        write INDEXITEM-ID build-markup copy CARD-TEMPLATE 
    ]

;; Make a list of all existing cards, in block and string form. 
    LOAD-CARD-LIST: does [
        CARD-LIST: copy []
        CARD-LIST: read CARD-CATALOG
        CARD-LIST-STRING: copy ""
        foreach CARDNAME CARD-LIST [
            append CARD-LIST-STRING rejoin [
                to-string CARDNAME
                newline
            ]
        ]
    ]
]

;; -- End of index card object 

;; Starting folder for picking file.  Change this for your own situation. 
STARTING-FOLDER: %/I/
CURRENT-FILE: none

;; We can modify the starting point so that the request-dir operation
;; is not so laborious.
SET-STARTING-FOLDER: does [
    if WS-START: request-dir [
        if dir? WS-START [
            change-dir WS-START
            set-face MAIN-START to-string WS-START
        ]
    ]
]

;; Choose a file and load data about it into the window.
PICK-FILE: does [
    CURRENT-FILE: request-file/only 
    if not CURRENT-FILE [
        alert "No file requested"
        exit
    ]
    INDEXCARD/ITEM-TYPE: "File"
;;  set [INDEXCARD/ITEM-LOC INDEXCARD/ITEM-ID] split-path CURRENT-FILE 
    INDEXCARD/ITEM-LOC: first split-path CURRENT-FILE
    INDEXCARD/ITEM-ID: second split-path CURRENT-FILE
    INDEXCARD/ITEM-ID-BASE: INDEXCARD/ITEM-ID
    MAIN-FILE/text: to-string CURRENT-FILE
    show MAIN-FILE
    MAIN-CARDID/text: INDEXCARD/ITEM-ID-BASE
    show MAIN-CARDID 
;;  -- Clear rest of form for data entry
    MAIN-TITLE/text: copy ""
    show MAIN-TITLE
    MAIN-DESCRIPTION/text: copy ""
    MAIN-DESCRIPTION/line-list: none
    show MAIN-DESCRIPTION
    MAIN-KEYWORDS/text: copy ""
    MAIN-KEYWORDS/line-list: none
    show MAIN-KEYWORDS
]

;; Choose a folder and load data about it into the window.
PICK-FOLDER: does [
    CURRENT-FILE: request-dir
    if not CURRENT-FILE [
        alert "No folder requested"
        exit
    ]
    INDEXCARD/ITEM-TYPE: "Folder"
;;  set [INDEXCARD/ITEM-LOC INDEXCARD/ITEM-ID] split-path CURRENT-FILE
    INDEXCARD/ITEM-LOC: first split-path CURRENT-FILE
    INDEXCARD/ITEM-ID: second split-path CURRENT-FILE
    INDEXCARD/ITEM-ID-BASE: copy INDEXCARD/ITEM-ID
    replace INDEXCARD/ITEM-ID-BASE "/" "" 
    MAIN-FILE/text: to-string CURRENT-FILE
    show MAIN-FILE
    MAIN-CARDID/text: INDEXCARD/ITEM-ID-BASE
    show MAIN-CARDID 
;;  -- Clear rest of form for data entry
    MAIN-TITLE/text: copy ""
    show MAIN-TITLE
    MAIN-DESCRIPTION/text: copy ""
    MAIN-DESCRIPTION/line-list: none
    show MAIN-DESCRIPTION
    MAIN-KEYWORDS/text: copy ""
    MAIN-KEYWORDS/line-list: none
    show MAIN-KEYWORDS
] 

;; Make an index card for the file or folder we have chosen.
MAKE-CARD: does [
    if not CURRENT-FILE [
        alert "No file requested"
        exit
    ]
    INDEXCARD/ITEM-TITLE: get-face MAIN-TITLE
    INDEXCARD/KEYWORDS: parse MAIN-KEYWORDS/text none
    INDEXCARD/DESCRIPTION: MAIN-DESCRIPTION/text
    INDEXCARD/CREATE-CARD
    REFRESH-CARD-LIST 
    alert "Done." 
]

;; Copy the index card we are working with into the clipboard. 
COPY-CARD: does [
    if not INDEXCARD/INDEXITEM-ID [
        alert "No card created"
        exit
    ]
    write clipboard:// read INDEXCARD/INDEXITEM-ID 
] 

;; Refresh the list of all index cards.  We have this list so that
;; we can check to see if we already have created an index card
;; for an item we are about to index.
REFRESH-CARD-LIST: does [
    INDEXCARD/LOAD-CARD-LIST
    set-face FILE-LIST INDEXCARD/CARD-LIST-STRING
    FILE-LIST/para/scroll/y: 0
    FILE-LIST/line-list: none
    FILE-LIST/user-data: second size-text FILE-LIST
    FILE-SCROLLER/data: 0
    FILE-SCROLLER/redrag FILE-LIST/size/y / FILE-LIST/user-data
]

MAIN-PANE: [
    across
    label 100 "Working in"
    MAIN-START: info 400 font [style: 'bold] (to-string STARTING-FOLDER)
    return 
    label 100 "File/Folder ID"
    MAIN-FILE: info 400 font [style: 'bold]
    return
    label 100 "Card ID" 
    MAIN-CARDID: field 400 font [style: 'bold]
    return
    label 100 "Title"
    MAIN-TITLE: field 400 font [style: 'bold]
    return
    label 100 "Description"
    MAIN-DESCRIPTION: area 400x180 wrap font [style: 'bold]
    return
    label 100 "Keywords"
    MAIN-KEYWORDS: area 200x180 as-is font [style: 'bold]
    return
    button 100 "Starting folder" [SET-STARTING-FOLDER]
    button 100 "Pick a file" [PICK-FILE] 
    button 100 "Pick a folder" [PICK-FOLDER]
    button 100 "Make card" [MAKE-CARD]
    button 100 "To clipboard" [COPY-CARD]
    return
    button "Quit" [quit]  
    button "Debug" [halt] 
]

FILE-PANE: [
    across
    FILE-LIST: info 300x600
    FILE-SCROLLER: scroller 20x600 [scroll-para FILE-LIST FILE-SCROLLER]
]

MAIN-WINDOW: layout [
    across 
    panel 550x600 MAIN-PANE
    panel 380x600 FILE-PANE 
]

change-dir STARTING-FOLDER
;; Generating the layout did not load the index card list.
REFRESH-CARD-LIST
view center-face MAIN-WINDOW